'commit'
This commit is contained in:
64
TangDou/AcWing/SCC/1175_Prepare.cpp
Normal file
64
TangDou/AcWing/SCC/1175_Prepare.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user