没做对
#include <bits/stdc++.h>#define N 50005using namespace std;stack <int> S;vector <int> E[N];int dfn[N],low[N];int p[N],h[N],s[N];int cnt = 0;int k = 0;int n,m;void tarjan(int x){ S.push(x); low[x] = dfn[x] = cnt++; for (int i = 0; i < E[x].size(); i++) { int v = E[x][i]; if (!dfn[v]) { tarjan(v); low[x] = min(low[x],low[v]); } else if(!s[v]) low[x] = min(low[x],dfn[v]); } if (dfn[x] == low[x]) { k++; while (true) { int now = S.top(); S.pop(); s[now] = k; h[k]++; if (now == x) break; } }}void rebuild(){ for (int i = 1; i <= n; i++) for (int j = 0; j < E[i].size(); j++) if (s[i] != s[E[i][j]]) p[s[i]]++;}int main(){ scanf("%d%d",&n,&m); for (int i = 1; i <= m; i++) { int x,y; scanf("%d%d",&x,&y); E[x].push_back(y); } for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i); rebuild(); int tot = 0,sav; for (int i = 1; i <= k; i++) if (!p[i]) tot++,sav=i; if (tot > 1) { PRintf("0/n"); } else printf("%d/n",h[sav]);}新闻热点
疑难解答