#include <algorithm> #include <cstdio> #include <iostream> #include <list> #include <unordered_map> #include <unordered_set> #include <set> #include <stack> #include <vector> using namespace std; typedef pair<int, int> pii; typedef unordered_set<int> si; typedef vector<int> vi; const int N_MAX = 500000+10; int n, m; struct graph { unordered_map<int, vi> G; vi& operator[](int i) { return G[i]; } graph reverse() { graph h; for (auto& z : G) for (auto y : z.second) h[y].push_back(z.first); return h; } graph subgraph(si& V) { graph h; for (auto& z : G) { if (V.find(z.first) == V.end()) continue; for (auto y : z.second) if (V.find(y) != V.end()) h[z.first].push_back(y); } return h; } graph without(vi& cycle) { set<pii> C; for (int i = 1; i < cycle.size(); i++) C.insert({cycle[i-1], cycle[i]}); C.insert({cycle[cycle.size()-1], cycle[0]}); graph h; for (auto& z : G) { for (auto y : z.second) if (C.find({z.first, y}) == C.end()) h[z.first].push_back(y); } return h; } }; struct kosaraju { stack<int> S; vector<bool> visited; graph& G; graph revG; bool multiple; kosaraju(graph& G) : G(G), revG(G.reverse()), visited(n+1, false), multiple(false) {} void dfs1(int v) { if (visited[v]) return; visited[v] = true; for (auto w : G[v]) dfs1(w); S.push(v); } void dfs2(int v, si& res) { if (visited[v]) return; res.insert(v); visited[v] = true; for (auto w : revG[v]) dfs2(w, res); } si get() { for (int u = 1; u <= n; u++) dfs1(u); si ans; visited.assign(n+1, false); while(!S.empty()) { int u = S.top(); S.pop(); si res; dfs2(u, res); // cerr << "res: "; for (auto w : res) cerr << w << ' '; cerr << '\n'; if (res.size() > 1) { if (ans.size()) { multiple = true; return si(); } swap(ans, res); } } return ans; } }; set<int> naive(graph& g, si& scc) { set<int> ans; for (auto u : scc) { si test_scc = scc; test_scc.erase(u); // cerr << "test " << u << '\n'; // cerr << "scc: "; for (auto w : test_scc) cerr << w << ' '; cerr << '\n'; graph h = g.subgraph(test_scc); kosaraju k(h); si res = k.get(); // cerr << "res: "; for (auto w : res) cerr << w << ' '; cerr << '\n'; if ((res.size() == 0) && !k.multiple) ans.insert(u); } return ans; } struct segment_builder { vi& cycle; list<pii>& cuts; int s; unordered_map<int, int> pos; vector<bool> t; segment_builder(vi& cycle, list<pii>& cuts) : cycle(cycle), cuts(cuts), s(cycle.size()), t(4*cycle.size(), true) { for (int i = 0; i < s; i++) { pos[cycle[i]] = i; } } void mark_false(int i, int l, int r, int a, int b) { // cerr << "mark_false: " << i << " " << l << " " << r << " " << a << " " << b << '\n'; if (a > b) return; if ((l == a) && (r == b)) { /*cerr << "mark\n";*/ t[i] = false; return; } int mid = (l + r) / 2; mark_false(2*i, l, mid, a, min(mid, b)); mark_false(2*i+1, mid+1, r, max(mid+1, a), b); } bool is_true(int i, int l, int r, int a) { // cerr << "is_true: " << i << " " << l << " " << r << " " << a << " " << t[i] << '\n'; if (t[i] == false) return false; if (l == r) return t[i]; int mid = (l + r) / 2; if (a <= mid) { return is_true(2*i, l, mid, a); } else { return is_true(2*i+1, mid+1, r, a); } } set<int> get() { for (auto c : cuts) { int x = c.first, y = c.second; // cerr << "x=" << x << " y=" << y << '\n'; // cerr << "pos: x=" << pos[x] << " y=" << pos[y] << '\n'; if (pos[x] >= pos[y]) { // cerr << "mark_false [" << 0 << ", " << pos[y]-1 << "]\n"; mark_false(1, 0, s-1, 0, pos[y]-1); // cerr << "mark_false [" << pos[x]+1 << ", " << s-1 << "]\n"; mark_false(1, 0, s-1, pos[x]+1, s-1); } else { // cerr << "mark_false [" << pos[x]+1 << ", " << pos[y]-1 << "]\n"; mark_false(1, 0, s-1, pos[x]+1, pos[y]-1); } } set<int> ans; // cerr << "cycle size: " << s << '\n'; for (auto x : cycle) { if (is_true(1, 0, s-1, pos[x])) ans.insert(x); // else cerr << "wrong " << x << '\n'; } return ans; } }; struct solve { graph& g; si& scc; vector<bool> visited; list<int> cycle_list; vi cycle; vi furthest; solve(graph& g, si& scc) : g(g), scc(scc), visited(n+1, false), furthest(n+1, 0) {} int dfs_cycle(int v) { if (visited[v]) return v; visited[v] = true; cycle_list.push_back(v); int w = *g[v].begin(); return dfs_cycle(w); } bool node_disjoint_cycle() { si test; si cycle_set(cycle.begin(), cycle.end()); for (auto x : scc) if (cycle_set.find(x) == cycle_set.end()) test.insert(x); graph test_g = g.subgraph(test); kosaraju k(test_g); si test_scc = k.get(); // cerr << "node disjoint cycle: "; for (auto u : test_scc) cerr << u << ' '; cerr << '\n'; // cerr << "multiple: " << k.multiple << '\n'; bool res = k.multiple || (test_scc.size() > 1); return res; } void find_cycle() { int head = dfs_cycle(*scc.begin()); // cerr << (*scc.begin()) << "-" << head << '\n'; while (cycle_list.front() != head) { visited[cycle_list.front()] = false; cycle_list.pop_front(); } cycle = vi(cycle_list.begin(), cycle_list.end()); for (int i = 0; i < cycle.size(); i++) furthest[cycle[i]] = i; } void dfs_to(graph& g_c, int v, int start, list<pii>& cuts) { if (visited[v]) { cuts.push_back({start, cycle[furthest[v]]}); return; } visited[v] = true; int f = -1; for (auto w : g_c[v]) { dfs_to(g_c, w, start, cuts); f = max(f, furthest[w]); } furthest[v] = f; } set<int> get() { find_cycle(); // cerr << "cycle: "; for (auto u : cycle) cerr << u << ' '; cerr << '\n'; // if (cycle.size() == scc.size()) { return set<int>(cycle.begin(), cycle.end()); } if (node_disjoint_cycle()) { return {}; } graph g_c = g.without(cycle); list<pii> cuts; for (auto from : cycle) { for (auto w : g_c[from]) dfs_to(g_c, w, from, cuts); // cerr << "from: " << from << '\n'; // cerr << "r: "; for (auto u : cuts) cerr << '(' << u.first << ',' << u.second << ") "; cerr << '\n'; } segment_builder sb(cycle, cuts); return sb.get(); } }; int main() { scanf("%d %d", &n, &m); graph inG; for (int i = 0; i < m; i++) { int x, y; scanf("%d %d", &x, &y); inG[x].push_back(y); } kosaraju k(inG); si scc = k.get(); if (scc.size() == 0) { if (k.multiple) printf("0\n\n"); else printf("NIE\n"); return 0; } // cerr << "scc: "; for (auto u : scc) cerr << u << ' '; cerr << '\n'; graph g = inG.subgraph(scc); // auto ans = naive(g, scc); // printf("%d\n", (int)ans.size()); // for (auto u : ans) printf("%d ", u); // printf("\n"); // cerr << "---\n"; solve sol(g, scc); auto ans = sol.get(); printf("%d\n", (int)ans.size()); for (auto u : ans) printf("%d ", u); printf("\n"); return 0; }
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | #include <algorithm> #include <cstdio> #include <iostream> #include <list> #include <unordered_map> #include <unordered_set> #include <set> #include <stack> #include <vector> using namespace std; typedef pair<int, int> pii; typedef unordered_set<int> si; typedef vector<int> vi; const int N_MAX = 500000+10; int n, m; struct graph { unordered_map<int, vi> G; vi& operator[](int i) { return G[i]; } graph reverse() { graph h; for (auto& z : G) for (auto y : z.second) h[y].push_back(z.first); return h; } graph subgraph(si& V) { graph h; for (auto& z : G) { if (V.find(z.first) == V.end()) continue; for (auto y : z.second) if (V.find(y) != V.end()) h[z.first].push_back(y); } return h; } graph without(vi& cycle) { set<pii> C; for (int i = 1; i < cycle.size(); i++) C.insert({cycle[i-1], cycle[i]}); C.insert({cycle[cycle.size()-1], cycle[0]}); graph h; for (auto& z : G) { for (auto y : z.second) if (C.find({z.first, y}) == C.end()) h[z.first].push_back(y); } return h; } }; struct kosaraju { stack<int> S; vector<bool> visited; graph& G; graph revG; bool multiple; kosaraju(graph& G) : G(G), revG(G.reverse()), visited(n+1, false), multiple(false) {} void dfs1(int v) { if (visited[v]) return; visited[v] = true; for (auto w : G[v]) dfs1(w); S.push(v); } void dfs2(int v, si& res) { if (visited[v]) return; res.insert(v); visited[v] = true; for (auto w : revG[v]) dfs2(w, res); } si get() { for (int u = 1; u <= n; u++) dfs1(u); si ans; visited.assign(n+1, false); while(!S.empty()) { int u = S.top(); S.pop(); si res; dfs2(u, res); // cerr << "res: "; for (auto w : res) cerr << w << ' '; cerr << '\n'; if (res.size() > 1) { if (ans.size()) { multiple = true; return si(); } swap(ans, res); } } return ans; } }; set<int> naive(graph& g, si& scc) { set<int> ans; for (auto u : scc) { si test_scc = scc; test_scc.erase(u); // cerr << "test " << u << '\n'; // cerr << "scc: "; for (auto w : test_scc) cerr << w << ' '; cerr << '\n'; graph h = g.subgraph(test_scc); kosaraju k(h); si res = k.get(); // cerr << "res: "; for (auto w : res) cerr << w << ' '; cerr << '\n'; if ((res.size() == 0) && !k.multiple) ans.insert(u); } return ans; } struct segment_builder { vi& cycle; list<pii>& cuts; int s; unordered_map<int, int> pos; vector<bool> t; segment_builder(vi& cycle, list<pii>& cuts) : cycle(cycle), cuts(cuts), s(cycle.size()), t(4*cycle.size(), true) { for (int i = 0; i < s; i++) { pos[cycle[i]] = i; } } void mark_false(int i, int l, int r, int a, int b) { // cerr << "mark_false: " << i << " " << l << " " << r << " " << a << " " << b << '\n'; if (a > b) return; if ((l == a) && (r == b)) { /*cerr << "mark\n";*/ t[i] = false; return; } int mid = (l + r) / 2; mark_false(2*i, l, mid, a, min(mid, b)); mark_false(2*i+1, mid+1, r, max(mid+1, a), b); } bool is_true(int i, int l, int r, int a) { // cerr << "is_true: " << i << " " << l << " " << r << " " << a << " " << t[i] << '\n'; if (t[i] == false) return false; if (l == r) return t[i]; int mid = (l + r) / 2; if (a <= mid) { return is_true(2*i, l, mid, a); } else { return is_true(2*i+1, mid+1, r, a); } } set<int> get() { for (auto c : cuts) { int x = c.first, y = c.second; // cerr << "x=" << x << " y=" << y << '\n'; // cerr << "pos: x=" << pos[x] << " y=" << pos[y] << '\n'; if (pos[x] >= pos[y]) { // cerr << "mark_false [" << 0 << ", " << pos[y]-1 << "]\n"; mark_false(1, 0, s-1, 0, pos[y]-1); // cerr << "mark_false [" << pos[x]+1 << ", " << s-1 << "]\n"; mark_false(1, 0, s-1, pos[x]+1, s-1); } else { // cerr << "mark_false [" << pos[x]+1 << ", " << pos[y]-1 << "]\n"; mark_false(1, 0, s-1, pos[x]+1, pos[y]-1); } } set<int> ans; // cerr << "cycle size: " << s << '\n'; for (auto x : cycle) { if (is_true(1, 0, s-1, pos[x])) ans.insert(x); // else cerr << "wrong " << x << '\n'; } return ans; } }; struct solve { graph& g; si& scc; vector<bool> visited; list<int> cycle_list; vi cycle; vi furthest; solve(graph& g, si& scc) : g(g), scc(scc), visited(n+1, false), furthest(n+1, 0) {} int dfs_cycle(int v) { if (visited[v]) return v; visited[v] = true; cycle_list.push_back(v); int w = *g[v].begin(); return dfs_cycle(w); } bool node_disjoint_cycle() { si test; si cycle_set(cycle.begin(), cycle.end()); for (auto x : scc) if (cycle_set.find(x) == cycle_set.end()) test.insert(x); graph test_g = g.subgraph(test); kosaraju k(test_g); si test_scc = k.get(); // cerr << "node disjoint cycle: "; for (auto u : test_scc) cerr << u << ' '; cerr << '\n'; // cerr << "multiple: " << k.multiple << '\n'; bool res = k.multiple || (test_scc.size() > 1); return res; } void find_cycle() { int head = dfs_cycle(*scc.begin()); // cerr << (*scc.begin()) << "-" << head << '\n'; while (cycle_list.front() != head) { visited[cycle_list.front()] = false; cycle_list.pop_front(); } cycle = vi(cycle_list.begin(), cycle_list.end()); for (int i = 0; i < cycle.size(); i++) furthest[cycle[i]] = i; } void dfs_to(graph& g_c, int v, int start, list<pii>& cuts) { if (visited[v]) { cuts.push_back({start, cycle[furthest[v]]}); return; } visited[v] = true; int f = -1; for (auto w : g_c[v]) { dfs_to(g_c, w, start, cuts); f = max(f, furthest[w]); } furthest[v] = f; } set<int> get() { find_cycle(); // cerr << "cycle: "; for (auto u : cycle) cerr << u << ' '; cerr << '\n'; // if (cycle.size() == scc.size()) { return set<int>(cycle.begin(), cycle.end()); } if (node_disjoint_cycle()) { return {}; } graph g_c = g.without(cycle); list<pii> cuts; for (auto from : cycle) { for (auto w : g_c[from]) dfs_to(g_c, w, from, cuts); // cerr << "from: " << from << '\n'; // cerr << "r: "; for (auto u : cuts) cerr << '(' << u.first << ',' << u.second << ") "; cerr << '\n'; } segment_builder sb(cycle, cuts); return sb.get(); } }; int main() { scanf("%d %d", &n, &m); graph inG; for (int i = 0; i < m; i++) { int x, y; scanf("%d %d", &x, &y); inG[x].push_back(y); } kosaraju k(inG); si scc = k.get(); if (scc.size() == 0) { if (k.multiple) printf("0\n\n"); else printf("NIE\n"); return 0; } // cerr << "scc: "; for (auto u : scc) cerr << u << ' '; cerr << '\n'; graph g = inG.subgraph(scc); // auto ans = naive(g, scc); // printf("%d\n", (int)ans.size()); // for (auto u : ans) printf("%d ", u); // printf("\n"); // cerr << "---\n"; solve sol(g, scc); auto ans = sol.get(); printf("%d\n", (int)ans.size()); for (auto u : ans) printf("%d ", u); printf("\n"); return 0; } |