#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define f first #define s second using namespace std; const int MAX_N = 1e6 + 9; const int LOG_MAX_N = 22; typedef long long lli; typedef pair<int, int> PII; bitset<MAX_N> bypassed; bitset<MAX_N> cycleBeneath; vector<int> G[MAX_N]; vector<int> component; int myComponent[MAX_N]; lli D[MAX_N]; lli F[MAX_N]; lli t = 1; lli zero = 1; int LCA[MAX_N][LOG_MAX_N]; int P[MAX_N]; int depth[MAX_N]; int valStarting[MAX_N]; int valEnding[MAX_N]; int valFinal[MAX_N]; int bypass[MAX_N]; int totalCycles = 0; int currentComponent = 1; int mainComponent = -1; bool cycleInCurrentComponent = false; int n, m; int sol; int findLCA(int a, int b) { //cout << "LCA " << a <<" i " << b << ": "; if(depth[a] != depth[b]) { if(depth[a] > depth[b]) swap(a, b); for(int i = LOG_MAX_N; i >= 0; --i) { if(LCA[b][i] != 0 && depth[LCA[b][i]] >= depth[a]) b = LCA[b][i]; } } if(a == b) { //cout << a << endl; return a; } for(int i = LOG_MAX_N; i >= 0; --i) { if(LCA[a][i] != LCA[b][i]) { a = LCA[a][i]; b = LCA[b][i]; } } //cout << LCA[a][1] << endl; return LCA[a][1]; } void DFS_components(int v) { D[v] = 1; myComponent[v] = currentComponent; for(int i = 0; i < G[v].size(); ++i) { int u = G[v][i]; if(D[u] == 0) { depth[u] = depth[v] + 1; P[u] = v; DFS_components(u); } else if(F[u] == 0) { if(cycleInCurrentComponent == false) { cycleInCurrentComponent = true; ++totalCycles; } } } F[v] = 1; return; } void DFS_findCycles(int v) { D[v] = ++t; for(int i = 0; i < G[v].size(); ++i) { int u = G[v][i]; if(D[u] <= zero) { DFS_findCycles(u); if(cycleBeneath[u]) cycleBeneath[v] = true; } else if(F[u] <= zero) { cycleBeneath[v] = true; ++valStarting[v]; ++valEnding[u]; ++totalCycles; } } F[v] = ++t; } void DFS_calculate(int v) { D[v] = ++t; for(int i = 0; i < G[v].size(); ++i) { int u = G[v][i]; if(D[u] <= zero) { DFS_calculate(u); valFinal[v] += valFinal[u] - valEnding[u]; } } valFinal[v] += valStarting[v]; F[v] = ++t; return; } void DFS_calculateBypass(int v) { D[v] = ++t; for(int i = 0; i < G[v].size(); ++i) { int u = G[v][i]; if(D[u] <= zero) { DFS_calculateBypass(u); } else if(F[u] > zero && cycleBeneath[u] && valFinal[v] != 0 && valFinal[u] != 0) { int lca = findLCA(v, u); if(P[v] != lca) bypass[P[v]] = depth[lca] + 1; if(P[u] != lca) bypass[P[u]] = depth[lca] + 1; } } F[v] = ++t; return; } void DFS_bypass(int v) { D[v] = ++t; for(int i = 0; i < G[v].size(); ++i) { int u = G[v][i]; if(D[u] <= zero) { DFS_bypass(u); if(bypass[u] != -1) { if(bypass[v] == -1) bypass[v] = bypass[u]; else bypass[v] = min(bypass[v], bypass[u]); } } } if(bypass[v] != -1 && bypass[v] <= depth[v]) bypassed[v] = true; F[v] = ++t; return; } int main() { scanf("%d%d", &n, &m); fill(bypass, bypass + n + 1, -1); depth[0] = -1; int a, b; for(int i = 0; i < m; ++i) { scanf("%d%d", &a, &b); G[a].pb(b); } for(int i = 1; i <= n; ++i) { if(D[i] == 0) { DFS_components(i); ++currentComponent; } if(totalCycles >= 2) { printf("0\n\n"); return 0; } else if(cycleInCurrentComponent == true) { mainComponent = currentComponent - 1; cycleInCurrentComponent = false; } } if(totalCycles == 0) { printf("NIE\n"); return 0; } totalCycles = 0; zero = t; for(int i = 1; i <= n; ++i) { LCA[i][0] = i; LCA[i][1] = P[i]; } for(int j = 2; j <= LOG_MAX_N; ++j) { for(int i = 1; i <= n; ++i) { LCA[i][j] = LCA[LCA[i][j - 1]][j - 1]; } } for(int i = 1; i <= n; ++i) { if(myComponent[i] == mainComponent) component.pb(i); } for(int i = 0; i < component.size(); ++i) { int root = component[i]; if(D[root] <= zero) DFS_findCycles(root); } /*for(int i = 0; i < n; ++i) { cout << i << ": " << cycleBeneath[i] << endl; }*/ zero = t; for(int i = 0; i < component.size(); ++i) { int root = component[i]; if(D[root] <= zero) DFS_calculate(root); } zero = t; for(int i = 0; i < component.size(); ++i) { int root = component[i]; if(D[root] <= zero) DFS_calculateBypass(root); } zero = t; for(int i = 0; i < component.size(); ++i) { int root = component[i]; if(D[root] <= zero) DFS_bypass(root); } for(int i = 1; i <= n; ++i) if(valFinal[i] == totalCycles && !bypassed[i]) ++sol; printf("%d\n", sol); for(int i = 1; i <= n; ++i) if(valFinal[i] == totalCycles && !bypassed[i]) printf("%d ", i); 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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | #include <bits/stdc++.h> #define pb push_back #define mp make_pair #define f first #define s second using namespace std; const int MAX_N = 1e6 + 9; const int LOG_MAX_N = 22; typedef long long lli; typedef pair<int, int> PII; bitset<MAX_N> bypassed; bitset<MAX_N> cycleBeneath; vector<int> G[MAX_N]; vector<int> component; int myComponent[MAX_N]; lli D[MAX_N]; lli F[MAX_N]; lli t = 1; lli zero = 1; int LCA[MAX_N][LOG_MAX_N]; int P[MAX_N]; int depth[MAX_N]; int valStarting[MAX_N]; int valEnding[MAX_N]; int valFinal[MAX_N]; int bypass[MAX_N]; int totalCycles = 0; int currentComponent = 1; int mainComponent = -1; bool cycleInCurrentComponent = false; int n, m; int sol; int findLCA(int a, int b) { //cout << "LCA " << a <<" i " << b << ": "; if(depth[a] != depth[b]) { if(depth[a] > depth[b]) swap(a, b); for(int i = LOG_MAX_N; i >= 0; --i) { if(LCA[b][i] != 0 && depth[LCA[b][i]] >= depth[a]) b = LCA[b][i]; } } if(a == b) { //cout << a << endl; return a; } for(int i = LOG_MAX_N; i >= 0; --i) { if(LCA[a][i] != LCA[b][i]) { a = LCA[a][i]; b = LCA[b][i]; } } //cout << LCA[a][1] << endl; return LCA[a][1]; } void DFS_components(int v) { D[v] = 1; myComponent[v] = currentComponent; for(int i = 0; i < G[v].size(); ++i) { int u = G[v][i]; if(D[u] == 0) { depth[u] = depth[v] + 1; P[u] = v; DFS_components(u); } else if(F[u] == 0) { if(cycleInCurrentComponent == false) { cycleInCurrentComponent = true; ++totalCycles; } } } F[v] = 1; return; } void DFS_findCycles(int v) { D[v] = ++t; for(int i = 0; i < G[v].size(); ++i) { int u = G[v][i]; if(D[u] <= zero) { DFS_findCycles(u); if(cycleBeneath[u]) cycleBeneath[v] = true; } else if(F[u] <= zero) { cycleBeneath[v] = true; ++valStarting[v]; ++valEnding[u]; ++totalCycles; } } F[v] = ++t; } void DFS_calculate(int v) { D[v] = ++t; for(int i = 0; i < G[v].size(); ++i) { int u = G[v][i]; if(D[u] <= zero) { DFS_calculate(u); valFinal[v] += valFinal[u] - valEnding[u]; } } valFinal[v] += valStarting[v]; F[v] = ++t; return; } void DFS_calculateBypass(int v) { D[v] = ++t; for(int i = 0; i < G[v].size(); ++i) { int u = G[v][i]; if(D[u] <= zero) { DFS_calculateBypass(u); } else if(F[u] > zero && cycleBeneath[u] && valFinal[v] != 0 && valFinal[u] != 0) { int lca = findLCA(v, u); if(P[v] != lca) bypass[P[v]] = depth[lca] + 1; if(P[u] != lca) bypass[P[u]] = depth[lca] + 1; } } F[v] = ++t; return; } void DFS_bypass(int v) { D[v] = ++t; for(int i = 0; i < G[v].size(); ++i) { int u = G[v][i]; if(D[u] <= zero) { DFS_bypass(u); if(bypass[u] != -1) { if(bypass[v] == -1) bypass[v] = bypass[u]; else bypass[v] = min(bypass[v], bypass[u]); } } } if(bypass[v] != -1 && bypass[v] <= depth[v]) bypassed[v] = true; F[v] = ++t; return; } int main() { scanf("%d%d", &n, &m); fill(bypass, bypass + n + 1, -1); depth[0] = -1; int a, b; for(int i = 0; i < m; ++i) { scanf("%d%d", &a, &b); G[a].pb(b); } for(int i = 1; i <= n; ++i) { if(D[i] == 0) { DFS_components(i); ++currentComponent; } if(totalCycles >= 2) { printf("0\n\n"); return 0; } else if(cycleInCurrentComponent == true) { mainComponent = currentComponent - 1; cycleInCurrentComponent = false; } } if(totalCycles == 0) { printf("NIE\n"); return 0; } totalCycles = 0; zero = t; for(int i = 1; i <= n; ++i) { LCA[i][0] = i; LCA[i][1] = P[i]; } for(int j = 2; j <= LOG_MAX_N; ++j) { for(int i = 1; i <= n; ++i) { LCA[i][j] = LCA[LCA[i][j - 1]][j - 1]; } } for(int i = 1; i <= n; ++i) { if(myComponent[i] == mainComponent) component.pb(i); } for(int i = 0; i < component.size(); ++i) { int root = component[i]; if(D[root] <= zero) DFS_findCycles(root); } /*for(int i = 0; i < n; ++i) { cout << i << ": " << cycleBeneath[i] << endl; }*/ zero = t; for(int i = 0; i < component.size(); ++i) { int root = component[i]; if(D[root] <= zero) DFS_calculate(root); } zero = t; for(int i = 0; i < component.size(); ++i) { int root = component[i]; if(D[root] <= zero) DFS_calculateBypass(root); } zero = t; for(int i = 0; i < component.size(); ++i) { int root = component[i]; if(D[root] <= zero) DFS_bypass(root); } for(int i = 1; i <= n; ++i) if(valFinal[i] == totalCycles && !bypassed[i]) ++sol; printf("%d\n", sol); for(int i = 1; i <= n; ++i) if(valFinal[i] == totalCycles && !bypassed[i]) printf("%d ", i); return 0; } |