#include <bits/stdc++.h> using namespace std; int d, m, n, s, t, A[500001], B[500001], C[500001], D[500001], E[500001]; queue<int> Q; vector<int> K[500001], K2[500001]; void wyznacz_skladowa(int a) { A[a] = B[a] = ++t; C[a] = 1; D[++d] = a; for (auto b: K[a]) { if (!A[b]) { wyznacz_skladowa(b); B[a] = min(B[a], B[b]); } else if (C[b]) { B[a] = min(B[a], A[b]); } } if (A[a] == B[a]) { if (D[d] == a) { E[a] = 1; } else { ++s; } while (D[d] != a) { C[D[d]] = 0; --d; } C[a] = 0; --d; } } bool jest_cykl_po_usunieciu(int a) { memcpy(B, A, (n + 1) * sizeof(int)); for (auto b: K2[a]) { --B[b]; } B[a] = 0; E[a] = 1; for (int i = 1; i <= n; ++i) { if (!E[i] && !B[i]) { Q.push(i); } } while (!Q.empty()) { int i = Q.front(); Q.pop(); for (auto j: K2[i]) { if (E[j]) { continue; } if (--B[j] == 0) { Q.push(j); } } } E[a] = 0; for (int i = 1; i <= n; ++i) { if (B[i]) { return true; } } return false; } int main() { int a, b; scanf("%d %d", &n, &m); while (m--) { scanf("%d %d", &a, &b); K[a].push_back(b); } d = s = t = 0; for (int i = 1; i <= n; ++i) { if (!A[i]) { wyznacz_skladowa(i); } } if (!s) { puts("NIE"); return 0; } if (s > 1) { puts("0"); puts(""); return 0; } memset(A, 0, (n + 1) * sizeof(int)); for (int i = 1; i <= n; ++i) { if (E[i]) { continue; } for (auto j: K[i]) { if (E[j]) { continue; } ++A[j]; K2[i].push_back(j); } } d = 0; for (int i = 1; i <= n; ++i) { if (E[i]) { continue; } if (!jest_cykl_po_usunieciu(i)) { D[d++] = i; } } printf("%d\n", d); for (int i = 0; i < d; ++i) { printf("%d ", D[i]); } puts(""); 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 | #include <bits/stdc++.h> using namespace std; int d, m, n, s, t, A[500001], B[500001], C[500001], D[500001], E[500001]; queue<int> Q; vector<int> K[500001], K2[500001]; void wyznacz_skladowa(int a) { A[a] = B[a] = ++t; C[a] = 1; D[++d] = a; for (auto b: K[a]) { if (!A[b]) { wyznacz_skladowa(b); B[a] = min(B[a], B[b]); } else if (C[b]) { B[a] = min(B[a], A[b]); } } if (A[a] == B[a]) { if (D[d] == a) { E[a] = 1; } else { ++s; } while (D[d] != a) { C[D[d]] = 0; --d; } C[a] = 0; --d; } } bool jest_cykl_po_usunieciu(int a) { memcpy(B, A, (n + 1) * sizeof(int)); for (auto b: K2[a]) { --B[b]; } B[a] = 0; E[a] = 1; for (int i = 1; i <= n; ++i) { if (!E[i] && !B[i]) { Q.push(i); } } while (!Q.empty()) { int i = Q.front(); Q.pop(); for (auto j: K2[i]) { if (E[j]) { continue; } if (--B[j] == 0) { Q.push(j); } } } E[a] = 0; for (int i = 1; i <= n; ++i) { if (B[i]) { return true; } } return false; } int main() { int a, b; scanf("%d %d", &n, &m); while (m--) { scanf("%d %d", &a, &b); K[a].push_back(b); } d = s = t = 0; for (int i = 1; i <= n; ++i) { if (!A[i]) { wyznacz_skladowa(i); } } if (!s) { puts("NIE"); return 0; } if (s > 1) { puts("0"); puts(""); return 0; } memset(A, 0, (n + 1) * sizeof(int)); for (int i = 1; i <= n; ++i) { if (E[i]) { continue; } for (auto j: K[i]) { if (E[j]) { continue; } ++A[j]; K2[i].push_back(j); } } d = 0; for (int i = 1; i <= n; ++i) { if (E[i]) { continue; } if (!jest_cykl_po_usunieciu(i)) { D[d++] = i; } } printf("%d\n", d); for (int i = 0; i < d; ++i) { printf("%d ", D[i]); } puts(""); return 0; } |