洛谷P2863[USACO06JAN]牛的舞会The Cow Prom——强连通分量
#include #include #include #include #include using namespace std;const int maxn = 1e5 + 10;vector G[maxn];int n, m;int deep, dfn[maxn], low[maxn], vis[maxn], stack[maxn], top;int color[maxn], C, color_cnt[maxn];void tarjan(int u) { dfn[u] = ++deep; low[u] = deep; vis[u] = 1; stack[++top] = u; for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if (!dfn[v]) { tarjan(v); low[u] = min(low[u], low[v]); } else { if (vis[v]) { low[u] = min(low[u], low[v]); } } } if (dfn[u] == low[u]) { color[u] = ++C; vis[u] = 0; while (stack[top] != u) { color[stack[top]] = C; vis[stack[top--]] = 0; } top--; }}int main() { scanf("%d%d", &n, &m); int u, v; for (int i = 1; i <= m; i++) { scanf("%d%d", &u, &v); G[u].push_back(v); } for (int i = 1; i <= n; i++) { if (!dfn[i]) tarjan(i); } for (int i = 1; i <= n; i++) { color_cnt[color[i]]++; } int ans = 0; for (int i = 1; i <= C; i++) { if (color_cnt[i] > 1) ans++; } printf("%d\n", ans); return 0;}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~