标签:ide dfs ext style == ems clu spl pac
text 你好世界
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 const int maxn = 1e5 + 5, maxm = 5e5 + 5; 6 7 int N, M; 8 9 struct Edge { 10 int u, v, pre; 11 } e[maxm]; 12 int m, G[maxn]; 13 14 void init() { 15 memset(G, -1, sizeof(G)); 16 m = 0; 17 } 18 void add(int u, int v) { 19 e[m++] = (Edge){u, v, G[u]}; 20 G[u] = m-1; 21 } 22 23 stack<pair<int, int> > s; 24 vector<int> bcc[maxn]; 25 int bcc_cnt, bccno[maxn]; 26 int cut[maxn], pre[maxn], low[maxn], dfs_clock; 27 28 void dfs(int u, int fa) { 29 low[u] = pre[u] = ++dfs_clock; 30 int child = 0; 31 for (register int i = G[u]; ~i; i = e[i].pre) { 32 int v = e[i].v; 33 if (!pre[v]) { 34 child++; 35 s.push(make_pair(u, v)); 36 dfs(v, u); 37 if (low[v] >= pre[u]) { 38 cut[u] = 1; 39 bcc_cnt++; // 找到一个双连通分量 40 for (;;) { 41 pair<int, int> e = s.top(); s.pop(); 42 if (bccno[e.first] != bcc_cnt) bccno[e.first] = bcc_cnt, bcc[bcc_cnt].push_back(e.first); 43 if (bccno[e.second] != bcc_cnt) bccno[e.second] = bcc_cnt, bcc[bcc_cnt].push_back(e.second); 44 if (e.first == u) break; 45 } 46 } 47 low[u] = min(low[u], low[v]); 48 } else 49 if (v != fa) low[u] = min(low[u], pre[v]); 50 } 51 if (u == fa && child == 1) cut[u] = 0; 52 } 53 54 void work() { 55 memset(pre, 0, sizeof(pre)); 56 memset(cut, 0, sizeof(cut)); 57 bcc_cnt = dfs_clock = 0; 58 for (register int i = 1; i <= N; i++) 59 if (!pre[i]) 60 dfs(i, i); 61 // 输出 62 printf("BCC : %d\n", bcc_cnt); 63 for (register int i = 1; i <= bcc_cnt; i++) { 64 printf("[%d]", i); 65 for (register int j = 0; j < bcc[i].size(); j++) 66 printf(" %d", bcc[i][j]); 67 printf("\n"); 68 } 69 } 70 71 int main() { 72 scanf("%d%d", &N, &M); 73 init(); 74 for (register int i = 1; i <= M; i++) { 75 int u, v; 76 scanf("%d%d", &u, &v); 77 add(u, v); add(v, u); 78 } 79 work(); 80 return 0; 81 }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5, maxm = 5e5 + 5; int N, M; struct Edge { int u, v, pre; } e[maxm]; int m, G[maxn]; void init() { memset(G, -1, sizeof(G)); m = 0; } void add(int u, int v) { e[m++] = (Edge){u, v, G[u]}; G[u] = m-1; } stack<pair<int, int> > s; vector<int> bcc[maxn]; int bcc_cnt, bccno[maxn]; int cut[maxn], pre[maxn], low[maxn], dfs_clock; void dfs(int u, int fa) { low[u] = pre[u] = ++dfs_clock; int child = 0; for (register int i = G[u]; ~i; i = e[i].pre) { int v = e[i].v; if (!pre[v]) { child++; s.push(make_pair(u, v)); dfs(v, u); if (low[v] >= pre[u]) { cut[u] = 1; bcc_cnt++; // 找到一个双连通分量 for (;;) { pair<int, int> e = s.top(); s.pop(); if (bccno[e.first] != bcc_cnt) bccno[e.first] = bcc_cnt, bcc[bcc_cnt].push_back(e.first); if (bccno[e.second] != bcc_cnt) bccno[e.second] = bcc_cnt, bcc[bcc_cnt].push_back(e.second); if (e.first == u) break; } } low[u] = min(low[u], low[v]); } else if (v != fa) low[u] = min(low[u], pre[v]); } if (u == fa && child == 1) cut[u] = 0; } void work() { memset(pre, 0, sizeof(pre)); memset(cut, 0, sizeof(cut)); bcc_cnt = dfs_clock = 0; for (register int i = 1; i <= N; i++) if (!pre[i]) dfs(i, i); // 输出 printf("BCC : %d\n", bcc_cnt); for (register int i = 1; i <= bcc_cnt; i++) { printf("[%d]", i); for (register int j = 0; j < bcc[i].size(); j++) printf(" %d", bcc[i][j]); printf("\n"); } } int main() { scanf("%d%d", &N, &M); init(); for (register int i = 1; i <= M; i++) { int u, v; scanf("%d%d", &u, &v); add(u, v); add(v, u); } work(); return 0; }
标签:ide dfs ext style == ems clu spl pac
原文地址:https://www.cnblogs.com/ac-evil/p/11143404.html