题目链接:

http://www.lydsy.com/JudgeOnline/problem.php?id=1051

题解:

强连通缩点得到DAG图,将图转置一下,对入度为零的点跑dfs看看能不能访问到所有的点。

代码:

#include<iostream>
#include<cstdio>
#include<vector>
#include<stack>
#include<algorithm>
#include<cstring>
using namespace std; const int maxn = + ;
const int INF = 0x3f3f3f3f; vector<int> G[maxn],G2[maxn];
int pre[maxn], lowlink[maxn], sccno[maxn], dfs_clock, scc_cnt;
int ind[maxn],siz[maxn],vis[maxn];
stack<int> S; int n,m; void dfs(int u) {
pre[u] = lowlink[u] = ++dfs_clock;
S.push(u);
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i];
if (!pre[v]) {
dfs(v);
lowlink[u] = min(lowlink[u], lowlink[v]);
}
else if (!sccno[v]) {
lowlink[u] = min(lowlink[u], pre[v]);
}
}
if (lowlink[u] == pre[u]) {
scc_cnt++;
int cnt = ;
for (;;) {
cnt++;
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if (x == u) break;
}
siz[scc_cnt] = cnt;
}
} void find_scc(int n) {
dfs_clock = scc_cnt = ;
memset(sccno, , sizeof(sccno));
memset(pre, , sizeof(pre));
for (int i = ; i < n; i++) if (!pre[i]) dfs(i);
//for (int i = 0; i < n; i++) printf("sccno[%d]:%d\n", i, sccno[i]);
} void dfs2(int u) {
if (vis[u]) return;
vis[u] = ;
for (int i = ; i < G2[u].size(); i++) {
int v = G2[u][i];
dfs2(v);
}
} void init() {
for (int i = ; i < n; i++) G[i].clear(), G2[i].clear();
memset(ind, ,sizeof(ind));
memset(vis, , sizeof(vis));
} int main() {
while (scanf("%d%d", &n,&m) == && n) {
init();
for (int i = ; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v); u--, v--;
G[u].push_back(v);
}
find_scc(n);
for (int i = ; i < n; i++) {
for (int j = ; j < G[i].size(); j++) {
int v = G[i][j];
if (sccno[i] != sccno[v]) {
G2[sccno[v]].push_back(sccno[i]);
ind[sccno[i]]++;
}
}
}
int rt = -;
for (int i = ; i <= scc_cnt; i++) {
if (ind[i] == ) rt = i;
}
dfs2(rt);
int su = ;
for (int i = ; i <= scc_cnt; i++) {
if (!vis[i]) {
su = ;
break;
}
}
if (su) {
printf("%d\n", siz[rt]);
}
else {
printf("0\n");
}
}
return ;
}
04-13 09:59