There is a direction graph G to find the node set with the largest number of nodes where any 2 points u and v either u can be to V or V can be to u or to each other to reach
First, each of the strongly connected components is accessible, and the weight of each point on a path path is the maximum number of nodes of the strongly connected component, which can be written by a memory search.
#include <cstdio> #include <cstring> #include <vector> #include <stack> using namespace std;
const int MAXN = 1010;
Vector <int> G[MAXN];
Stack <int> s;
int PRE[MAXN];
int LOW[MAXN];
int CNT[MAXN];
int DEGREE[MAXN];
int sccno[maxn];//the scc_cnt int dfs_clock of the strong unicom component of each point;
int scc_cnt;
int n, m;
Vector <int> A[MAXN];
int D[MAXN];
void Dfs (int u) {pre[u] = low[u] = ++dfs_clock;
S.push (U);
for (int i = 0; i < g[u].size (); i++) {int v = g[u][i];
if (!pre[v]) {DFS (v);
Low[u] = min (Low[u], low[v]);
} else if (!sccno[v]) {Low[u] = min (Low[u], pre[v]);
}} if (low[u] = = Pre[u]) {scc_cnt++;
while (1) {int x = S.top ();
S.pop ();
SCCNO[X] = scc_cnt;
cnt[scc_cnt]++;
if (x = = u) break;
}}} void Find_scc () {dfs_clock = scc_cnt = 0;
memset (sccno, 0, sizeof (SCCNO));
memset (pre, 0, sizeof (pre));
for (int i = 1; I <= n; i++) if (!pre[i]) DFS (i); } int DP (int u) {if (D[u]! =-1) return D[u];
D[u] = Cnt[u];
int ans = 0;
for (int i = 0; i < a[u].size (); i++) {int v = a[u][i];
int temp = DP (v);
ans = max (ans, temp);
} D[u] + = ans;
return D[u];
} int main () {int T;
scanf ("%d", &t);
while (t--) {scanf ("%d%d", &n, &m);
for (int i = 1; I <= n; i++) {g[i].clear ();
A[i].clear ();
} for (int i = 1; I <= m; i++) {int u, v;
scanf ("%d%d", &u, &v);
G[u].push_back (v);
} memset (degree, 0, sizeof (degree));
memset (CNT, 0, sizeof (CNT));
memset (d,-1, sizeof (d));
FIND_SCC ();
for (int u = 1, u <= n; u++) {for (int i = 0; i < g[u].size (); i++) {int v = g[u][i];
if (sccno[u]! = Sccno[v]) {a[sccno[u]].push_back (sccno[v]);
degree[sccno[v]]++;
}}} int ans = 0;
for (int i = 1; I <= scc_cnt; i++) {if (!degree[i]) {int temp = DP (i);
ans = max (ans, temp);
}} printf ("%d\n", ans);
} return 0; }