You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.1 KiB
80 lines
2.1 KiB
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
const int N = 1e5 + 10, M = N << 2;
|
|
|
|
int n, m;
|
|
int ts, root, dfn[N], low[N], stk[N], top;
|
|
vector<int> bcc[N];
|
|
int bcc_cnt;
|
|
|
|
int e[M], h[N], idx, w[M], ne[M];
|
|
void add(int a, int b, int c = 0) {
|
|
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
|
|
}
|
|
|
|
void tarjan(int u) {
|
|
printf(" Enter : #%d\n", u);
|
|
dfn[u] = low[u] = ++ts;
|
|
stk[++top] = u;
|
|
if (u == root && h[u] == -1) {
|
|
bcc[++bcc_cnt].push_back(u);
|
|
return;
|
|
}
|
|
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]);
|
|
|
|
if (low[v] >= dfn[u]) {
|
|
bcc_cnt++;
|
|
printf(" Found a New BCC #%d.\n", bcc_cnt);
|
|
|
|
int bcc_id = bcc_cnt + n;
|
|
int x;
|
|
do {
|
|
x = stk[top--];
|
|
bcc[bcc_cnt].push_back(x);
|
|
// 将x向圆方树方格建边
|
|
add(x, bcc_id);
|
|
printf(" BCC #%d has vertex #%d\n", bcc_cnt, x);
|
|
} while (x != v);
|
|
bcc[bcc_cnt].push_back(u);
|
|
// 将u向圆方树方格建边
|
|
add(u, bcc_id);
|
|
printf(" BCC #%d has vertex #%d\n", bcc_cnt, u);
|
|
}
|
|
} else
|
|
low[u] = min(low[u], dfn[v]);
|
|
|
|
printf(" Exit : #%d : low = %d\n", u, low[u]);
|
|
printf(" Stack:\n ");
|
|
for (int i = 1; i <= top; ++i) printf("%d, ", stk[i]);
|
|
puts("");
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
#ifndef ONLINE_JUDGE
|
|
freopen("YFS.in", "r", stdin);
|
|
freopen("YFS_2.out", "w", stdout);
|
|
#endif
|
|
memset(h, -1, sizeof h);
|
|
cin >> n >> m;
|
|
for (int i = 0; i < m; i++) {
|
|
int a, b;
|
|
cin >> a >> b;
|
|
add(a, b), add(b, a);
|
|
}
|
|
|
|
for (root = 1; root <= n; root++)
|
|
if (!dfn[root]) tarjan(root);
|
|
|
|
// for (int i = 1; i <= bcc_cnt; i++) {
|
|
// for (int j = 0; j < bcc[i].size(); j++)
|
|
// printf("%d ", bcc[i][j]);
|
|
// puts("");
|
|
// }
|
|
return 0;
|
|
}
|