// Krzysztof Małysa #include <bits/stdc++.h> #include <unistd.h> using namespace std; #define FOR(i,a,n) for (int i = (a), __n ## i = n; i < __n ## i; ++i) #define REP(i,n) FOR(i,0,n) #define FORD(i,a,n) for (int i = (a), __n ## i = n; i >= __n ## i; --i) #define LET(x,a) __typeof(a) x = (a) #define FOREACH(i,x) for (LET(i, x.begin()), __n##i = x.end(); i != __n##i; ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) (int(x.size())) #define eprintf(...) fprintf(stderr, __VA_ARGS__) #define ST first #define ND second #define MP make_pair #define PB push_back #define O(...) ostream& operator <<(ostream& os, const __VA_ARGS__& x) #define OO(...) O(__VA_ARGS__) { return __out(os, ALL(x)); } #define T template #define CL class typedef unsigned uint; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<LL> VLL; typedef pair<int, int> PII; typedef vector<PII> VPII; typedef vector<VPII> VVPII; typedef pair<LL, LL> PLLLL; typedef vector<PLLLL> VPLLLL; typedef vector<bool> VB; typedef vector<char> VC; typedef vector<string> VS; T<CL A> inline A abs(const A& a) { return a < A() ? -a : a; } T<CL A, CL B> inline void mini(A& a, const B& b) { if (b < a) a = b; } T<CL A, CL B> inline void maxi(A& a, const B& b) { if (b > a) a = b; } T<CL Iter> ostream& __out(ostream& os, Iter a, Iter b, const string& s = ", "); T<CL A, CL B> O(pair<A, B>); T<CL A> OO(vector<A>) T<CL A> OO(deque<A>) T<CL A> OO(list<A>) T<CL A, CL B> OO(set<A, B>) T<CL A, CL B, CL C> OO(map<A, B, C>) T<CL A, CL B> OO(multiset<A, B>) T<CL A, CL B, CL C> OO(multimap<A, B, C>) T<CL Iter> ostream& __out(ostream& os, Iter a, Iter b, const string& s) { os << "{"; if (a != b) { os << *a; while (++a != b) os << s << *a; } return os << "}"; } T<CL Iter> ostream& __dump(ostream& os, Iter a, Iter b) { os << "{\n"; Iter beg = a; while (a != b) { os << " " << a - beg << ": " << *a << "\n"; ++a; } return os << "}"; } T<CL A, CL B> O(pair<A, B>) { return os << "(" << x.ST << ", " << x.ND << ")"; } CL Input { static const int BUFF_SIZE = 1 << 16; unsigned char buff[BUFF_SIZE], *pos, *end; void grabBuffer() { pos = buff; end = buff + read(0, buff, BUFF_SIZE); } public: Input() : pos(buff), end(buff) {} int peek() { if (pos == end) grabBuffer(); return pos != end ? *pos : -1; } int getChar() { if (pos == end) grabBuffer(); return pos != end ? *pos++ : -1; } void skipWhiteSpaces() { while (isspace(peek())) ++pos; } T<CL A> A get(); void operator()(char* s) { skipWhiteSpaces(); while (!isspace(peek())) *s++ = *pos++; *s = '\0'; } T<CL A> void operator()(A& x) { x = get<A>(); } T<CL A, CL B> void operator()(A& a, B& b) { operator()(a), operator()(b); } T<CL A, CL B, CL C> void operator()(A& a, B& b, C& c) { operator()(a, b), operator()(c); } T<CL A, CL B, CL C, CL D> void operator()(A& a, B& b, C& c, D& d) { operator()(a, b, c); operator()(d); } T<CL A, CL B, CL C, CL D, CL E> void operator()(A& a, B& b, C& c, D& d, E& e) { operator()(a, b, c, d); operator()(e); } T<CL A, CL B, CL C, CL D, CL E, CL F> void operator()(A& a, B& b, C& c, D& d, E& e, F& f) { operator()(a, b, c, d, e); operator()(f); } Input& operator>>(char *s) { operator()(s); return *this; } T<CL A> Input& operator>>(A& a) { operator()(a); return *this; } } fin; T<> char Input::get<char>() { skipWhiteSpaces(); return *pos++; } T<> uint Input::get<uint>() { skipWhiteSpaces(); uint x = 0; while (isdigit(peek())) x = x * 10 + *pos++ - '0'; return x; } T<> int Input::get<int>() { skipWhiteSpaces(); return peek() == '-' ? (++pos, -get<uint>()) : get<uint>(); } T<> ULL Input::get<ULL>() { skipWhiteSpaces(); ULL x = 0; while (isdigit(peek())) x = x * 10 + *pos++ - '0'; return x; } T<> LL Input::get<LL>() { skipWhiteSpaces(); return peek() == '-' ? (++pos, -get<ULL>()) : get<ULL>(); } T<> string Input::get<string>() { skipWhiteSpaces(); string x; while (!isspace(peek())) x += *pos++; return x; } int ceil2(int x) { return 1 << (sizeof(x) * 8 -__builtin_clz(x - 1)); } #undef T #undef CL #ifdef DEBUG # define D(...) __VA_ARGS__ # define E(...) eprintf(__VA_ARGS__) # define OUT(a,b) cerr << #a ":", __out(cerr, a, b), E("\n") # define DUMP(x) cerr << #x ":", __dump(cerr, ALL(x)), E("\n") # define LOG(x) cerr << #x ": " << (x) # define LOG2(x, y) cerr << x << ": " << (y) # define LOGN(x) LOG(x) << endl # define LOGN2(x, y) LOG2(x, y) << endl #else # define D(...) # define E(...) # define OUT(...) # define DUMP(...) # define LOG(...) # define LOG2(...) # define LOGN(...) # define LOGN2(...) #endif /// End of templates int n; VVI G; VI cycle; // a <- b <- ... <- x (<- a) // without a at the end #ifdef DEBUG VI vis; #else VC vis; #endif VI id; // id non-negative only for cycle nodes // VPII mosts; // most left node id, right most node id (id is cycle node id) // -1 - cycle found, 0 - cycle not found, > 0 number of node on which cease // adding nodes to cycle (cycle found) int find_cycle(int x) { // D(static int k = 0;++k;) // LOGN2(string(k, ' '), x); vis[x] = 1; int tmp; FOREACH (i, G[x]) if (vis[*i] == 1) { cycle.PB(x); vis[x] = 2; return *i; } else if (vis[*i] == 0 && (tmp = find_cycle(*i)) != 0) { vis[x] = 2; if (tmp == -1) return -1; cycle.PB(x); return x == tmp ? -1 : tmp; } // D(--k); vis[x] = 2; return 0; } void remove_edge(int a, int b) { FOREACH (i, G[a]) if (*i == b) { swap(*i, G[a].back()); G[a].pop_back(); break; } } VPII most; PII get_most(int x) { if (most[x] != PII(-1, -1)) return most[x]; PII tmp(n + 1, -1); FOREACH (i, G[x]) { PII ggg; if (id[*i] != -1) ggg = PII(id[*i], id[*i]); else ggg = get_most(*i); mini(tmp.ST, ggg.ST); maxi(tmp.ND, ggg.ND); } return most[x] = tmp; } VB attainable; void mark(int x, int k) { if (id[x] > k) return; FOREACH (i, G[x]) if (!attainable[*i]) { attainable[*i] = true; mark(*i, k); } } int main() { int m, a, b; fin >> n >> m; G.resize(n + 1); vis.resize(n + 1); id.resize(n + 1, -1); REP (i, m) { fin >> a >> b; G[a].PB(b); } // Find cycle FOR (i, 1, n + 1) if (vis[i] == 0 && find_cycle(i)) break; if (cycle.empty()) { puts("NIE\n"); return 0; } LOGN(cycle); // Number cycle nodes REP (i, SZ(cycle)) id[cycle[i]] = i; LOGN(id); // Check if there is separate cycle from the found REP (i, n + 1) vis[i] = (id[i] == -1 ? 0 : 2); LOGN(vis); FOR (i, 1, n + 1) if (vis[i] == 0 && find_cycle(i) != 0) { puts("0\n"); return 0; } // Remove edges of cycle remove_edge(cycle[0], cycle.back()); FOR (i, 1, SZ(cycle)) remove_edge(cycle[i], cycle[i - 1]); DUMP(G); // Process other cycles VI good(SZ(cycle) + 1); // if prefix sum of node i is 0 then i belongs to result most.resize(n + 1, PII(-1, -1)); attainable.resize(n + 1); FORD (i, SZ(cycle) - 1, 0) { PII best = get_most(cycle[i]); // left most node id, right most node id E("====================\n"); LOGN(i); LOGN(cycle[i]); LOGN(best); // Found cycle if (best.ND >= i) { // Mark all nodes attainable from <1, i> nodes REP (j, i + 1) if (!attainable[cycle[j]]) mark(cycle[j], i); FOR (j, i, best.ND) if (attainable[cycle[j]]) { best.ND = j; break; } LOGN(attainable); D( REP (j, SZ(cycle)) E(" %i", (int)attainable[cycle[j]]); E("\n"); ) LOGN(best.ND); // Check if there is a cycle beginning in a, and ending in b (a, b are in cycle) and id[a] < id[b] < i REP (j, n + 1) vis[j] = (id[j] >= i ? 2 : 0); // Restore some of cycle edges FOR (j, 1, i) G[cycle[j]].PB(cycle[j - 1]); LOGN(vis); REP (j, i) if (vis[cycle[j]] == 0 && find_cycle(cycle[j])) { // Found such a cycle puts("0\n"); return 0; } LOGN(vis); // Erase everything not from <i, best.ND> ++good[0]; --good[i]; ++good[best.ND + 1]; --good[SZ(cycle)]; break; } assert(best.ST != i); // Found alternative way to move forward if (best.ST < i) { ++good[best.ST + 1]; --good[i]; } } LOGN(most); LOGN(good); // Print result VI res; REP (i, SZ(cycle)) { good[i + 1] += good[i]; if (good[i] == 0) res.PB(cycle[i]); } sort(ALL(res)); printf("%i\n", SZ(res)); REP (i, SZ(res)) printf(i ? " %i" : "%i", res[i]); puts("\n"); return 0; } /* 4 5 1 2 2 3 3 1 3 4 4 2 3 2 1 2 2 3 5 7 1 4 4 5 5 3 3 2 2 1 3 4 5 1 5 8 1 4 4 5 5 3 3 2 2 1 3 4 5 1 2 3 4 5 1 2 2 3 3 1 2 4 4 2 */
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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | // Krzysztof Małysa #include <bits/stdc++.h> #include <unistd.h> using namespace std; #define FOR(i,a,n) for (int i = (a), __n ## i = n; i < __n ## i; ++i) #define REP(i,n) FOR(i,0,n) #define FORD(i,a,n) for (int i = (a), __n ## i = n; i >= __n ## i; --i) #define LET(x,a) __typeof(a) x = (a) #define FOREACH(i,x) for (LET(i, x.begin()), __n##i = x.end(); i != __n##i; ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) (int(x.size())) #define eprintf(...) fprintf(stderr, __VA_ARGS__) #define ST first #define ND second #define MP make_pair #define PB push_back #define O(...) ostream& operator <<(ostream& os, const __VA_ARGS__& x) #define OO(...) O(__VA_ARGS__) { return __out(os, ALL(x)); } #define T template #define CL class typedef unsigned uint; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<LL> VLL; typedef pair<int, int> PII; typedef vector<PII> VPII; typedef vector<VPII> VVPII; typedef pair<LL, LL> PLLLL; typedef vector<PLLLL> VPLLLL; typedef vector<bool> VB; typedef vector<char> VC; typedef vector<string> VS; T<CL A> inline A abs(const A& a) { return a < A() ? -a : a; } T<CL A, CL B> inline void mini(A& a, const B& b) { if (b < a) a = b; } T<CL A, CL B> inline void maxi(A& a, const B& b) { if (b > a) a = b; } T<CL Iter> ostream& __out(ostream& os, Iter a, Iter b, const string& s = ", "); T<CL A, CL B> O(pair<A, B>); T<CL A> OO(vector<A>) T<CL A> OO(deque<A>) T<CL A> OO(list<A>) T<CL A, CL B> OO(set<A, B>) T<CL A, CL B, CL C> OO(map<A, B, C>) T<CL A, CL B> OO(multiset<A, B>) T<CL A, CL B, CL C> OO(multimap<A, B, C>) T<CL Iter> ostream& __out(ostream& os, Iter a, Iter b, const string& s) { os << "{"; if (a != b) { os << *a; while (++a != b) os << s << *a; } return os << "}"; } T<CL Iter> ostream& __dump(ostream& os, Iter a, Iter b) { os << "{\n"; Iter beg = a; while (a != b) { os << " " << a - beg << ": " << *a << "\n"; ++a; } return os << "}"; } T<CL A, CL B> O(pair<A, B>) { return os << "(" << x.ST << ", " << x.ND << ")"; } CL Input { static const int BUFF_SIZE = 1 << 16; unsigned char buff[BUFF_SIZE], *pos, *end; void grabBuffer() { pos = buff; end = buff + read(0, buff, BUFF_SIZE); } public: Input() : pos(buff), end(buff) {} int peek() { if (pos == end) grabBuffer(); return pos != end ? *pos : -1; } int getChar() { if (pos == end) grabBuffer(); return pos != end ? *pos++ : -1; } void skipWhiteSpaces() { while (isspace(peek())) ++pos; } T<CL A> A get(); void operator()(char* s) { skipWhiteSpaces(); while (!isspace(peek())) *s++ = *pos++; *s = '\0'; } T<CL A> void operator()(A& x) { x = get<A>(); } T<CL A, CL B> void operator()(A& a, B& b) { operator()(a), operator()(b); } T<CL A, CL B, CL C> void operator()(A& a, B& b, C& c) { operator()(a, b), operator()(c); } T<CL A, CL B, CL C, CL D> void operator()(A& a, B& b, C& c, D& d) { operator()(a, b, c); operator()(d); } T<CL A, CL B, CL C, CL D, CL E> void operator()(A& a, B& b, C& c, D& d, E& e) { operator()(a, b, c, d); operator()(e); } T<CL A, CL B, CL C, CL D, CL E, CL F> void operator()(A& a, B& b, C& c, D& d, E& e, F& f) { operator()(a, b, c, d, e); operator()(f); } Input& operator>>(char *s) { operator()(s); return *this; } T<CL A> Input& operator>>(A& a) { operator()(a); return *this; } } fin; T<> char Input::get<char>() { skipWhiteSpaces(); return *pos++; } T<> uint Input::get<uint>() { skipWhiteSpaces(); uint x = 0; while (isdigit(peek())) x = x * 10 + *pos++ - '0'; return x; } T<> int Input::get<int>() { skipWhiteSpaces(); return peek() == '-' ? (++pos, -get<uint>()) : get<uint>(); } T<> ULL Input::get<ULL>() { skipWhiteSpaces(); ULL x = 0; while (isdigit(peek())) x = x * 10 + *pos++ - '0'; return x; } T<> LL Input::get<LL>() { skipWhiteSpaces(); return peek() == '-' ? (++pos, -get<ULL>()) : get<ULL>(); } T<> string Input::get<string>() { skipWhiteSpaces(); string x; while (!isspace(peek())) x += *pos++; return x; } int ceil2(int x) { return 1 << (sizeof(x) * 8 -__builtin_clz(x - 1)); } #undef T #undef CL #ifdef DEBUG # define D(...) __VA_ARGS__ # define E(...) eprintf(__VA_ARGS__) # define OUT(a,b) cerr << #a ":", __out(cerr, a, b), E("\n") # define DUMP(x) cerr << #x ":", __dump(cerr, ALL(x)), E("\n") # define LOG(x) cerr << #x ": " << (x) # define LOG2(x, y) cerr << x << ": " << (y) # define LOGN(x) LOG(x) << endl # define LOGN2(x, y) LOG2(x, y) << endl #else # define D(...) # define E(...) # define OUT(...) # define DUMP(...) # define LOG(...) # define LOG2(...) # define LOGN(...) # define LOGN2(...) #endif /// End of templates int n; VVI G; VI cycle; // a <- b <- ... <- x (<- a) // without a at the end #ifdef DEBUG VI vis; #else VC vis; #endif VI id; // id non-negative only for cycle nodes // VPII mosts; // most left node id, right most node id (id is cycle node id) // -1 - cycle found, 0 - cycle not found, > 0 number of node on which cease // adding nodes to cycle (cycle found) int find_cycle(int x) { // D(static int k = 0;++k;) // LOGN2(string(k, ' '), x); vis[x] = 1; int tmp; FOREACH (i, G[x]) if (vis[*i] == 1) { cycle.PB(x); vis[x] = 2; return *i; } else if (vis[*i] == 0 && (tmp = find_cycle(*i)) != 0) { vis[x] = 2; if (tmp == -1) return -1; cycle.PB(x); return x == tmp ? -1 : tmp; } // D(--k); vis[x] = 2; return 0; } void remove_edge(int a, int b) { FOREACH (i, G[a]) if (*i == b) { swap(*i, G[a].back()); G[a].pop_back(); break; } } VPII most; PII get_most(int x) { if (most[x] != PII(-1, -1)) return most[x]; PII tmp(n + 1, -1); FOREACH (i, G[x]) { PII ggg; if (id[*i] != -1) ggg = PII(id[*i], id[*i]); else ggg = get_most(*i); mini(tmp.ST, ggg.ST); maxi(tmp.ND, ggg.ND); } return most[x] = tmp; } VB attainable; void mark(int x, int k) { if (id[x] > k) return; FOREACH (i, G[x]) if (!attainable[*i]) { attainable[*i] = true; mark(*i, k); } } int main() { int m, a, b; fin >> n >> m; G.resize(n + 1); vis.resize(n + 1); id.resize(n + 1, -1); REP (i, m) { fin >> a >> b; G[a].PB(b); } // Find cycle FOR (i, 1, n + 1) if (vis[i] == 0 && find_cycle(i)) break; if (cycle.empty()) { puts("NIE\n"); return 0; } LOGN(cycle); // Number cycle nodes REP (i, SZ(cycle)) id[cycle[i]] = i; LOGN(id); // Check if there is separate cycle from the found REP (i, n + 1) vis[i] = (id[i] == -1 ? 0 : 2); LOGN(vis); FOR (i, 1, n + 1) if (vis[i] == 0 && find_cycle(i) != 0) { puts("0\n"); return 0; } // Remove edges of cycle remove_edge(cycle[0], cycle.back()); FOR (i, 1, SZ(cycle)) remove_edge(cycle[i], cycle[i - 1]); DUMP(G); // Process other cycles VI good(SZ(cycle) + 1); // if prefix sum of node i is 0 then i belongs to result most.resize(n + 1, PII(-1, -1)); attainable.resize(n + 1); FORD (i, SZ(cycle) - 1, 0) { PII best = get_most(cycle[i]); // left most node id, right most node id E("====================\n"); LOGN(i); LOGN(cycle[i]); LOGN(best); // Found cycle if (best.ND >= i) { // Mark all nodes attainable from <1, i> nodes REP (j, i + 1) if (!attainable[cycle[j]]) mark(cycle[j], i); FOR (j, i, best.ND) if (attainable[cycle[j]]) { best.ND = j; break; } LOGN(attainable); D( REP (j, SZ(cycle)) E(" %i", (int)attainable[cycle[j]]); E("\n"); ) LOGN(best.ND); // Check if there is a cycle beginning in a, and ending in b (a, b are in cycle) and id[a] < id[b] < i REP (j, n + 1) vis[j] = (id[j] >= i ? 2 : 0); // Restore some of cycle edges FOR (j, 1, i) G[cycle[j]].PB(cycle[j - 1]); LOGN(vis); REP (j, i) if (vis[cycle[j]] == 0 && find_cycle(cycle[j])) { // Found such a cycle puts("0\n"); return 0; } LOGN(vis); // Erase everything not from <i, best.ND> ++good[0]; --good[i]; ++good[best.ND + 1]; --good[SZ(cycle)]; break; } assert(best.ST != i); // Found alternative way to move forward if (best.ST < i) { ++good[best.ST + 1]; --good[i]; } } LOGN(most); LOGN(good); // Print result VI res; REP (i, SZ(cycle)) { good[i + 1] += good[i]; if (good[i] == 0) res.PB(cycle[i]); } sort(ALL(res)); printf("%i\n", SZ(res)); REP (i, SZ(res)) printf(i ? " %i" : "%i", res[i]); puts("\n"); return 0; } /* 4 5 1 2 2 3 3 1 3 4 4 2 3 2 1 2 2 3 5 7 1 4 4 5 5 3 3 2 2 1 3 4 5 1 5 8 1 4 4 5 5 3 3 2 2 1 3 4 5 1 2 3 4 5 1 2 2 3 3 1 2 4 4 2 */ |