Files
python/TangDou/AcWing/SCC/1175_Prepare.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

64 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 100010, M = 2000010; // 因为要建新图,两倍的边
int n, m, mod; // 点数、边数、取模的数
int f[N], g[N];
int h[N], hs[N], e[M], ne[M], idx; // h: 原图;hs: 新图
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
// tarjan求强连通分量
int dfn[N], low[N], ts, in_stk[N], stk[N], top;
int id[N], scc_cnt, sz[N];
void tarjan(int u) {
dfn[u] = low[u] = ++ts;
stk[++top] = u;
in_stk[u] = 1;
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (in_stk[v])
low[u] = min(low[u], low[v]);
}
if (dfn[u] == low[u]) {
++scc_cnt;
int x;
do {
x = stk[top--];
in_stk[x] = 0;
id[x] = scc_cnt;
sz[scc_cnt]++;
} while (x != u);
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("1175_Preapre.in", "r", stdin);
#endif
memset(h, -1, sizeof h); // 原图
scanf("%d %d", &n, &m);
while (m--) {
int a, b;
scanf("%d %d", &a, &b);
add(a, b);
}
// (1) tarjan算法求强连通分量
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i);
for (int i = 1; i <= n; i++)
cout << "id[" << i << "]=" << id[i] << " ";
cout << endl;
cout << "scc_cnt=" << scc_cnt << endl;
return 0;
}