Give a direction graph, find the largest node set, any two points u,v. U can reach V or V to reach U.
A strongly connected component must be selected together. So first find out all the SCC, and then pinch to run the DP on the DAG later.
Note 0,0 This set of data
#include <bits/stdc++.h>using namespacestd;Const intMAXN =1005, MAXM = 5e5+1;intHEAD[MAXN],TO[MAXM],NXT[MAXM];voidAddedge (intUintVinti) {To[i]=v; Nxt[i]=Head[u]; Head[u]=i;}intPre[maxn],low[maxn],sccno[maxn],dfs_clock,scc_cnt;stack<int>Stk;voidTarjan (intu) {Pre[u]= Low[u] = + +Dfs_clock; Stk.push (U); for(inti = Head[u]; ~i; i =Nxt[i]) { intv =To[i]; if(!Pre[v]) {Tarjan (v); Low[u]=min (low[u],low[v]); }Else if(!Sccno[v]) {Low[u]=min (low[u],pre[v]); } } if(Pre[u] = =Low[u]) {scc_cnt++; while(Stk.size ()) {intx =stk.top (); Stk.pop (); SCCNO[X]=scc_cnt; if(x = = u) Break; } }}voidFIND_SCC (intN) {memset (PRE,0,sizeof(pre)); memset (Sccno,0,sizeof(SCCNO)); Dfs_clock= SCC_CNT =0; for(inti =0; I < n; i++){ if(!Pre[i]) Tarjan (i); }}vector<int>G[MAXN];intWEI[MAXN], DEG[MAXN];voidBuilddag (intN) { for(inti =1; I <= scc_cnt; i++) G[i].clear (), deg[i] = wei[i] =0; for(intU =0; U < n; u++){ intV0 = Sccno[u]; wei[v0]++; for(inti = Head[u]; ~i; i =Nxt[i]) { intV1 =Sccno[to[i]]; if(V0! = v1) G[v0].push_back (v1), deg[v1]++; } }}intDP[MAXN];inttopo () {memset (DP,-1,sizeof(DP)); Queue<int>Q; for(inti =1; I <= scc_cnt; i++){ if(!deg[i]) Q.push (i), dp[i] =Wei[i]; } while(Q.size ()) {intU =Q.front (); Q.pop (); for(inti =0; I < (int) G[u].size (); i++){ intv =G[u][i]; DP[V]= Max (dp[v],dp[u]+Wei[v]); if(--deg[v] = =0) Q.push (v); } } intAns =0; for(inti =1; I <= scc_cnt; i++) {ans=Max (Dp[i],ans); } returnans;}intMain () {//freopen ("In.txt", "R", stdin); intT scanf"%d",&T); while(t--){ intn,m; memset (Head,-1,sizeof(head)); scanf ("%d%d",&n,&m); for(inti =0; I < m; i++){ intU,v; scanf"%d%d",&u,&v); Addedge (U-1, V-1, i); } FIND_SCC (n); Builddag (n); printf ("%d\n", Topo ()); } return 0;}
UVA 11324 the largest clique