Uva 10131-is bigger Smarter? (DP)

Source: Internet
Author: User

Topic Link: Click to open the linkThe longest road + print path on a DAG (directed acyclic graph) is simple, for two point a B, the condition that can be from A to B is w[a]<w[b]&&s[a]>s[b] attention is directed to the graph. Set Dp[i] to the length of the longest road starting with I, dp[i]= Max (DP[I],DP[J]+1) enumeration J (J is the point connected to i)
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #include < queue> #include <stack> #include <map> #include <set> #define MAXN 1010#define _ll __int64#define ll Long long#define INF 0x3f3f3f3f#define Mod 1000000007#define pp pair<int,int> #define ull unsigned long longusing nam Espace std;int p,w[maxn],s[maxn],dp[maxn];bool ma[maxn][maxn];void build () {memset (ma,0,sizeof (MA)); for (int i=1;i <p;i++) {for (int j=i+1;j<p;j++) {if (W[i]<w[j]&&s[i]>s[j]) ma[i][j]=1;else if (w[j]<w[i]& &s[j]>s[i]) ma[j][i]=1;}}} int dfs (int x) {int& ans=dp[x];if (ans>0) return ans;ans=1;for (int i=1;i<p;i++) if (Ma[x][i]) Ans=max (Ans,dfs (i ) +1); return ans;}    void output (int x) {printf ("%d\n", X), for (int i=1;i<p;i++) if (ma[x][i]&&dp[x]==dp[i]+1) {output (i); }}int Main () {P=1;while (scanf ("%d%d ", w+p,s+p)!=eof) ++p;build () memset (Dp,-1,sizeof (DP)); int ans=-inf,indx;for (int i=1;i<p;i++) if (Ans<dfs (    i)) {ans=dp[i];indx=i;    }printf ("%d\n", ans); output (indx); return 0;}


Uva 10131-is bigger Smarter? (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.