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
|
#include <stack> #include <cstdio> #include <vector> #include <bitset> #include <cassert> #include <algorithm>
using namespace std;
const auto U = [](auto x) { return [x](auto ...y) { return x(x, y...); }; };
vector<int> web[100005]; int dfn[100005], low[100005]; int sccnt; vector<int> scc[100005]; int sccid[100005]; bitset<100005> instk; class iedge { public: int u, v, xu, xv; }; vector<iedge> ch[100005]; bitset<100005> NightWarden; iedge fa[100005]; int sz[100005];
char ibf[100000005]; unsigned li = 0; unsigned qread() { unsigned res = 0; char ch; while((ch = ibf[li++]) < '0' || ch > '9'); do { res = res * 10 + ch - '0'; } while((ch = ibf[li++]) >= '0' && ch <= '9'); return res; }
int main() { fread(ibf, 1, sizeof ibf, stdin); int n, m, r; n = qread(); m = qread(); r = qread(); for(int i=1;i<=m;i++) { int u, v; u = qread(); v = qread(); web[u].push_back(v); } int ddfn = 0; stack<int> stk; U([&](auto &&self, int u) -> void { dfn[u] = low[u] = ++ddfn; instk[u] = true; stk.push(u); for(int v : web[u]) { if(!dfn[v]) { self(self, v); low[u] = min(low[u], low[v]); } else if(instk[v]) low[u] = min(low[u], dfn[v]); } if(dfn[u] == low[u]) { sccnt++; int x; do { x = stk.top(); stk.pop(); instk[x] = false; scc[sccnt].push_back(x); sccid[x] = sccnt; } while(x != u); } })(r); assert(stk.empty()); for(int i=1;i<=n;i++) { for(int j : web[i]) { if(sccid[i] != sccid[j]) ch[sccid[i]].push_back({sccid[i], sccid[j], i, j}); } } int met = 0; U([&](auto &&self, int u) -> void { met++; sz[u] = scc[u].size(); for(const auto &e : ch[u]) { fa[e.v] = {e.v, e.u, e.xv, e.xu}; self(self, e.v); sz[u] += sz[e.v]; } })(sccid[r]); assert(met == sccnt); for(int i=1;i<=n;i++) printf("%d%c", sz[sccid[i]], " \n"[i == n]); vector<pair<int, int>> ans; U([&](auto &&self, int u) -> void { for(const auto &e : ch[u]) { self(self, e.v); } if(u != sccid[r] && !NightWarden[u]) { int lst = fa[u].xu, x = fa[u].xv; while(sccid[x] != sccid[r] && fa[sccid[x]].xu == x && !NightWarden[sccid[x]]) { NightWarden[sccid[x]] = true; lst = x; x = fa[sccid[x]].xv; } ans.push_back({fa[u].xu, x}); } })(sccid[r]); printf("%d\n", (int)ans.size()); for(const auto &[u, v] : ans) printf("%d %d\n", u, v); return 0; }
|