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)