#include <cstdio> #include <vector> #include <deque> #include <unordered_set> #include <algorithm> using namespace std; int N, M, a, b, minId, index; bool fin, cyc; deque<int> s; unordered_set<int> cv, cr, ct; vector<int> res, scc; struct vs { unordered_set<int> e, b; int index, lowlink; bool onStack, blocked; }; vector<vs> v; void strongconnect(int vi) { int wi; v[vi].index = v[vi].lowlink = index++; s.push_back(vi); v[vi].onStack = true; for (auto itE = v[vi].e.begin(); !fin && itE != v[vi].e.end();) { int wi = *itE; if (wi < minId) { itE = v[vi].e.erase(itE); } else { if (!v[wi].index) { strongconnect(wi); if (v[wi].lowlink < v[vi].lowlink) { v[vi].lowlink = v[wi].lowlink; } } else if (v[wi].onStack) { if (v[wi].index < v[vi].lowlink) { v[vi].lowlink = v[wi].index; } } itE++; } } if (v[vi].lowlink == v[vi].index) { // strongly connected scc.clear(); do { wi = s.back(); s.pop_back(); v[wi].onStack = false; scc.push_back(wi); } while (wi != vi); if (scc.size() > 1) { if (cv.size() > 0) { fin = true; } else { cv.insert(scc.begin(), scc.end()); } } } } void unblock(int vi) { int wi; v[vi].blocked = false; for (auto itB = v[vi].b.begin(); itB != v[vi].b.end(); itB++) { wi = *itB; if (v[wi].blocked) { unblock(wi); } } v[vi].b.clear(); } bool circuit(int vi) { bool f = false; int wi, si; s.push_back(vi); v[vi].blocked = true; for (auto itE = v[vi].e.begin(); itE != v[vi].e.end(); itE++) { wi = *itE; if (wi == minId) { cyc = true; ct.clear(); for (auto itS = s.begin(); itS != s.end(); itS++) { si = *itS; if (cr.find(si) != cr.end()) { ct.insert(si); } } cr = ct; f = true; } else if (!v[wi].blocked) { if (circuit(wi)) { f = true; } } } if (f) { unblock(vi); } else { for (auto itE = v[vi].e.begin(); itE != v[vi].e.end(); itE++) { wi = *itE; if (v[wi].b.find(vi) == v[wi].b.end()) { v[wi].b.insert(vi); } } } s.pop_back(); return f; } int main() { scanf("%d %d", &N, &M); v.resize(N + 1); for (int i = 0; i < M; i++) { scanf("%d %d", &a, &b); v[a].e.insert(b); } // Johnson minId = 1; s.clear(); for (int i = 1; i <= N; i++) { cr.insert(i); } while (!fin && minId < N) { // clean for (int i = minId; i <= N; i++) { v[i].index = v[i].lowlink = 0; v[i].onStack = false; } // Tarjan index = 1; cv.clear(); for (int i = minId; !fin && i <= N; i++) { if (!v[i].index) { strongconnect(i); } } if (!fin && cv.size() > 0) { minId = N; for (auto itV = cv.begin(); itV != cv.end(); itV++) { int vi = *itV; if (vi < minId) { minId = vi; } v[vi].b.clear(); v[vi].blocked = false; } circuit(minId); minId++; } else { minId = N; } } if (fin) { printf("0\n\n"); } else if (!cyc) { printf("NIE\n"); } else { res.insert(res.end(), cr.begin(), cr.end()); sort(res.begin(), res.end()); printf("%d\n", (int)res.size()); for (auto itR = res.begin(); itR != res.end(); itR++) { printf("%d ", *itR); } 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 | #include <cstdio> #include <vector> #include <deque> #include <unordered_set> #include <algorithm> using namespace std; int N, M, a, b, minId, index; bool fin, cyc; deque<int> s; unordered_set<int> cv, cr, ct; vector<int> res, scc; struct vs { unordered_set<int> e, b; int index, lowlink; bool onStack, blocked; }; vector<vs> v; void strongconnect(int vi) { int wi; v[vi].index = v[vi].lowlink = index++; s.push_back(vi); v[vi].onStack = true; for (auto itE = v[vi].e.begin(); !fin && itE != v[vi].e.end();) { int wi = *itE; if (wi < minId) { itE = v[vi].e.erase(itE); } else { if (!v[wi].index) { strongconnect(wi); if (v[wi].lowlink < v[vi].lowlink) { v[vi].lowlink = v[wi].lowlink; } } else if (v[wi].onStack) { if (v[wi].index < v[vi].lowlink) { v[vi].lowlink = v[wi].index; } } itE++; } } if (v[vi].lowlink == v[vi].index) { // strongly connected scc.clear(); do { wi = s.back(); s.pop_back(); v[wi].onStack = false; scc.push_back(wi); } while (wi != vi); if (scc.size() > 1) { if (cv.size() > 0) { fin = true; } else { cv.insert(scc.begin(), scc.end()); } } } } void unblock(int vi) { int wi; v[vi].blocked = false; for (auto itB = v[vi].b.begin(); itB != v[vi].b.end(); itB++) { wi = *itB; if (v[wi].blocked) { unblock(wi); } } v[vi].b.clear(); } bool circuit(int vi) { bool f = false; int wi, si; s.push_back(vi); v[vi].blocked = true; for (auto itE = v[vi].e.begin(); itE != v[vi].e.end(); itE++) { wi = *itE; if (wi == minId) { cyc = true; ct.clear(); for (auto itS = s.begin(); itS != s.end(); itS++) { si = *itS; if (cr.find(si) != cr.end()) { ct.insert(si); } } cr = ct; f = true; } else if (!v[wi].blocked) { if (circuit(wi)) { f = true; } } } if (f) { unblock(vi); } else { for (auto itE = v[vi].e.begin(); itE != v[vi].e.end(); itE++) { wi = *itE; if (v[wi].b.find(vi) == v[wi].b.end()) { v[wi].b.insert(vi); } } } s.pop_back(); return f; } int main() { scanf("%d %d", &N, &M); v.resize(N + 1); for (int i = 0; i < M; i++) { scanf("%d %d", &a, &b); v[a].e.insert(b); } // Johnson minId = 1; s.clear(); for (int i = 1; i <= N; i++) { cr.insert(i); } while (!fin && minId < N) { // clean for (int i = minId; i <= N; i++) { v[i].index = v[i].lowlink = 0; v[i].onStack = false; } // Tarjan index = 1; cv.clear(); for (int i = minId; !fin && i <= N; i++) { if (!v[i].index) { strongconnect(i); } } if (!fin && cv.size() > 0) { minId = N; for (auto itV = cv.begin(); itV != cv.end(); itV++) { int vi = *itV; if (vi < minId) { minId = vi; } v[vi].b.clear(); v[vi].blocked = false; } circuit(minId); minId++; } else { minId = N; } } if (fin) { printf("0\n\n"); } else if (!cyc) { printf("NIE\n"); } else { res.insert(res.end(), cr.begin(), cr.end()); sort(res.begin(), res.end()); printf("%d\n", (int)res.size()); for (auto itR = res.begin(); itR != res.end(); itR++) { printf("%d ", *itR); } printf("\n"); } return 0; } |