這個題多種解法,我用tarjan又寫了一遍
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <stack>using namespace std;const int MAXN = 2000;int n,m,to[MAXN<<1],head[MAXN],cd[MAXN],blt[MAXN],nxt[MAXN<<1],tot,scc[MAXN],sz[MAXN],tim=0;bool vis[MAXN],flag = 0;struct Edge{ int from,to;}e[MAXN<<1];stack <int> s;void build(int f,int t){ to[++tot] = t; nxt[tot] = head[f]; head[f] = tot;}int low[MAXN],dfn[MAXN],scccnt = 0; void dfs(int u){ low[u] = dfn[u] = ++tim; s.push(u); int v; for(int i = head[u] ; i ; i = nxt[i]) { v = to[i]; if(!dfn[v]) { dfs(v); low[u] = min(low[u],low[v]); } else if(!scc[v]) { low[u] = min(low[u],dfn[v]); } } if(low[u] == dfn[u]) { int x; scccnt ++; while(!s.empty()) { x = s.top(); s.pop(); scc[x] = scccnt; sz[scccnt]++; if(u == x) break; } }}int main(){ scanf("%d%d",&n,&m); for(int i = 1; i <= m; i ++) { scanf("%d%d",&e[i].from ,& e[i].to); build(e[i].from ,e[i].to); } for(int i = 1; i <= n; i ++) if(!dfn[i]) dfs(i); int ans = 0,q = 0; int xx[5000]; for(int i = 1; i <= scccnt ; i ++) { if(sz[i]>1) { q ++; xx[q] = i; } } for(int j = 1; j <= q; j ++) for(int i = 1; i <= n; i ++) { if(scc[i] == xx[j]) { vis[i] = 1; } } for(int i = 1; i <= n; i ++) { if(vis[i]) puts("T"); else puts("F"); } return 0;}