1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| const int maxn = 100000 + 10;
// == define Edge and Graph == class Edge { public: int to; Edge() {} Edge(int to) : to(to) {} };
vector<Edge> edges; vector<int> G[maxn]; // == Edge and Graph finsihed ==
// == tarjan info == int n, m; vector<int> dcc[maxn]; int dfn[maxn], low[maxn], cnt = 0, Clock = 0; bool cut[maxn];
stack<int> stk; int root = 0;
void init() { cnt = 0; Clock = 0; root = 0;
edges.clear(); edges.push_back(Edge()); edges.push_back(Edge()); // NIL edges
Set(dfn, 0); Set(low, 0); Set(cut, 0);
while(!stk.empty()) stk.pop(); } // == tarjan info finished ==
// == tarjan main == void addEdge(int u, int v) { edges.push_back(Edge(v)); G[u].push_back(edges.size() - 1); }
void tarjan(int u) { dfn[u] = low[u] = ++Clock; stk.push(u);
if(u == root && G[u].size() == 0) { // single point dcc[++cnt].push_back(u); return; }
int cld = 0; _for(i, 0, G[u].size()) { int eid = G[u][i], v = edges[eid].to;
if(!dfn[v]) { tarjan(v); low[u] = min(low[u], low[v]);
// judge cut-point if(low[v] >= dfn[u]) { cld++; if(u != root || cld > 1) cut[u] = true;
// save the point in the same DCC cnt++; for(;;) { int z = stk.top(); stk.pop(); dcc[cnt].push_back(z); if(z == v) break; } dcc[cnt].push_back(u); } } else low[u] = min(low[u], dfn[v]); } } // == tarjan finsihed ==
// == vDCC Graph == vector<int> GC[maxn]; vector<Edge> vDCC; int belong[maxn], newID[maxn]; int num = 0;
void initDCC() { vDCC.clear(); vDCC.push_back(Edge()); vDCC.push_back(Edge()); // NIL edges
Set(belong, 0); Set(newID, 0); num = 0; }
void add_c(int u, int v) { vDCC.push_back(Edge(v)); GC[u].push_back(vDCC.size() - 1); }
// == vDCC finsihed ==
void getDCC() { // input DCC nodes and data _rep(i, 1, cnt) { printf("v-DCC #%d:", i); _for(j, 0, dcc[i].size()) { printf(" %d", dcc[i][j]); } puts(""); }
// point reduction num = cnt; _rep(i, 1, n) if(cut[i]) newID[i] = ++num;
// build new Graph for vDCC _rep(i, 1, cnt) _for(j, 0, dcc[i].size()) { int z = dcc[i][j]; if(cut[z]) { add_c(i, newID[z]), add_c(newID[z], i); } else belong[z] = i; }
// print info printf("缩点之后的森林,点数 %d, 边数 %d\n", num, (int)(vDCC.size() - 1) >> 1); printf("下图编号 1~%d 的为原图的v-DCC,编号 >=%d 的为原图的割点\n", cnt, cnt); for(int i = 2; i < vDCC.size(); i += 2) { printf("vDCC#%d <-----> vDCC#%d\n", vDCC[i^1].to, vDCC[i].to); } } // == vDCC Graph finished ==
int main() { freopen("input.txt", "r", stdin); cin >> n >> m;
init(); _rep(i, 1, m) { int u, v; scanf("%d%d", &u, &v); if(u == v) continue; addEdge(u, v), addEdge(v, u); }
// == tarjan == _rep(i, 1, n) if(!dfn[i]) { root = i, tarjan(i); } _rep(i, 1, n) if(cut[i]) printf("%d ", i); puts("are cut vertexes"); // == tarjan finished ==
initDCC(); getDCC(); }
|