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
| #include <bits/stdc++.h> using namespace std;
const int MXN = 100005; const int MXV = MXN * 2; int T, N, M, V; int dfn[MXN], low[MXN], did; int cid, stk[MXN], top; int que[MXN], qh, qt; bool instk[MXN]; vector<int> to[MXN], comps[MXN], nto[MXV]; vector<pair<int, int>> segs[MXV]; set<int> rese[MXN], vire[MXN];
inline int read() { int x(0), c(getchar()); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + (c - '0'), c = getchar(); return x; } inline void write(int x) { assert(x); static char buf[11]; int d(0); while (x) buf[d++] = x % 10 + '0', x /= 10; while (d) putchar(buf[--d]); }
void build_scc(int p, int q, int s) { nto[p].push_back(s); int otop(top); stk[++otop] = p; while (true) { int c(stk[top--]); instk[c] = false; for (int i : to[c]) { if (instk[i] && dfn[i] >= dfn[p]) { rese[c].insert(i), rese[i].insert(c); vire[c].insert(i), vire[i].insert(c); } } if (c == q) break; } qh = qt = 0; for (int i(top + 1); i <= otop; ++i) { int c(stk[i]); if (rese[c].size() == 2) que[qt++] = c; } while (qh != qt) { int c(que[qh++]); if (rese[c].size() != 2) break; int x(*rese[c].begin()); int y(*next(rese[c].begin())); rese[x].erase(c), rese[y].erase(c); if (rese[x].find(y) != rese[x].end()) { vire[x].erase(y), vire[y].erase(x); if (rese[x].size() == 2) que[qt++] = x; if (rese[y].size() == 2) que[qt++] = y; } else { rese[x].insert(y), rese[y].insert(x); } } int x(que[qh - 1]), y(que[qh]); if (vire[x].size() == 1) vire[x].insert(y), vire[y].insert(x); for (int c(*vire[p].begin()), l(p);;) { nto[s].push_back(c); int n(*vire[c].begin() + *next(vire[c].begin()) - l); if (n == p) break; l = c, c = n; } for (int i(top + 1); i <= otop; ++i) { int c(stk[i]); rese[c].clear(), vire[c].clear(); } } void build(int p, int f) { low[p] = dfn[p] = did++; instk[p] = true, stk[++top] = p; for (int q : to[p]) { if (q == f) continue; if (dfn[q] == -1) { build(q, p); low[p] = min(low[p], low[q]); if (low[q] == dfn[q]) { nto[p].push_back(q); instk[q] = false; --top; } else if (low[q] == dfn[p]) { build_scc(p, q, V++); } } else { low[p] = min(low[p], dfn[q]); } } }
void calc(int p) { segs[p].clear(); if (p < N) segs[p].emplace_back(p, p); for (int q : nto[p]) { calc(q); segs[p].emplace_back(segs[q][0].first, q); } if (p < N) sort(segs[p].begin(), segs[p].end()); else if (segs[p].back().first < segs[p][0].first) reverse(segs[p].begin(), segs[p].end()); } void generate(int p, int ci) { for (auto [fir, q] : segs[p]) { if (q == p) comps[ci].push_back(p); else generate(q, ci); } } void output(int layer) { for (int i : comps[layer]) { while (top != cid && comps[top][0] < i) output(top++); write(i + 1), putchar(' '); } }
int main() { for (read(), T = read(); T--;) { N = read(), M = read(); for (int i(0); i != N; ++i) to[i].clear(); for (int x(0), y(0); M--;) { x = read() - 1, y = read() - 1; to[x].push_back(y), to[y].push_back(x); } did = 0; fill(dfn, dfn + N, -1); for (int i(0); i != N + N; ++i) { segs[i].clear(); nto[i].clear(); } V = N; cid = 0; for (int i(0); i != N; ++i) { if (~dfn[i]) continue; top = -1, build(i, -1); calc(i); comps[cid].clear(), generate(i, cid++); } top = 0; while (top != cid) output(top++); putchar('\n'); cout << flush; } return 0; }
|