I thought it was a tree-like DP, where DFS is located according to the tree-like DP method. The result is wa, because it has a directed ring, not a tree, do not use the tree-like method in the case of loops.
The breakthrough point of the question is to use vertices to represent the maximum length of the end of an edge with this vertex after the edge is sorted, because the edge is sorted from small to large, therefore, the side appended must be smaller than the front side.
When the same side is used, a buffer is required, because operations on the same side may not affect the operation.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n,m;const int N = 100010*3;struct edge{ int u,v,w; bool operator < (const edge& rhs) const{ return w<rhs.w; }}E[N];int dp[N];int last[N];int main(){ while (scanf("%d%d",&n,&m)!=EOF) { for (int i=0;i<m;i++){ scanf("%d%d%d",&E[i].u,&E[i].v,&E[i].w); } sort(E,E+m); memset(dp,0,sizeof dp); memset(last,0,sizeof last); for (int i=0;i<m;){ int j=i; while (E[i].w==E[j].w && j<m) j++; for (int k=i;k<j;k++){ last[E[k].v]=max(last[E[k].v],dp[E[k].u]+1); } for (int k=i;k<j;k++){ dp[E[k].v]=max(dp[E[k].v],last[E[k].v]); } i=j; } int ans=0; for (int i=1;i<=n;i++){ ans=max(ans,dp[i]); } printf("%d\n",ans); } return 0;}
Codeforces 459e Roland and Rose