#include <cstdio> #include <iostream> #include <vector> #include <list> #include <string> #include <set> #include <map> #include <algorithm> #include <utility> #include <cmath> #include <queue> #include <stack> #include <cassert> #include <cstring> #include <climits> #include <functional> #include <unordered_set> #include <unordered_map> #define VAR(i,v) __typeof(v) i = (v) #define SIZE(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define REP(i,b) for(int i=0; i<(b); ++i) #define FOR(i,a,b) for(int i=(a); i<=(b); ++i) #define FORD(i,a,b) for(int i=(a); i>=(b); --i) #define FOREACH(i,c) for(VAR(i,(c).begin()); i != (c).end(); ++i) #define PB push_back #define MP make_pair #define ST first #define ND second #define NL printf("\n") using namespace std; typedef pair<int,int> PII; typedef vector<int> VI; typedef long long LL; const int INF = 2147483640; const int MAXN = 1000005; const bool DEBUG = 0; multiset<int> g[MAXN], h[MAXN]; multiset<int> path; int in[MAXN]; vector<VI> cycles; int hm[MAXN]; VI res; int n, m; bool brk = 0; void find_cycle(int u, int b) { if(DEBUG) printf("find_cycle(%d, %d)\n", u, b); while(!h[u].empty()) { int v = *h[u].begin(); h[u].erase(h[u].begin()); res.PB(v); if(v == b) { cycles.PB(res); res.clear(); brk = 1; return; } find_cycle(v,b); if(brk) return; if(!res.empty()) res.erase(--res.end()); } } int main() { scanf("%d %d", &n, &m); REP(i,m) { int a, b; scanf("%d %d", &a, &b); g[a].insert(b); } FOR(i,1,n) { FOR(i,1,n) h[i] = g[i]; while(!h[i].empty()) { brk = 0; res.PB(i); find_cycle(i,i); res.clear(); } } if(!cycles.empty()) { int p = 0; for(VI X: cycles) { if(X.front() != X.back()) continue; p++; sort(ALL(X)); X.erase(unique(ALL(X)), X.end()); for(int x: X) hm[x]++; } VI answer; FOR(i,1,n) if(hm[i] == p) answer.PB(i); printf("%d\n", SIZE(answer)); for(int x: answer) printf("%d ", x); NL; } else { printf("NIE\n"); } if(DEBUG) { for(VI X: cycles) { for(int x: X) printf("%d ", x); NL; } } 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 | #include <cstdio> #include <iostream> #include <vector> #include <list> #include <string> #include <set> #include <map> #include <algorithm> #include <utility> #include <cmath> #include <queue> #include <stack> #include <cassert> #include <cstring> #include <climits> #include <functional> #include <unordered_set> #include <unordered_map> #define VAR(i,v) __typeof(v) i = (v) #define SIZE(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define REP(i,b) for(int i=0; i<(b); ++i) #define FOR(i,a,b) for(int i=(a); i<=(b); ++i) #define FORD(i,a,b) for(int i=(a); i>=(b); --i) #define FOREACH(i,c) for(VAR(i,(c).begin()); i != (c).end(); ++i) #define PB push_back #define MP make_pair #define ST first #define ND second #define NL printf("\n") using namespace std; typedef pair<int,int> PII; typedef vector<int> VI; typedef long long LL; const int INF = 2147483640; const int MAXN = 1000005; const bool DEBUG = 0; multiset<int> g[MAXN], h[MAXN]; multiset<int> path; int in[MAXN]; vector<VI> cycles; int hm[MAXN]; VI res; int n, m; bool brk = 0; void find_cycle(int u, int b) { if(DEBUG) printf("find_cycle(%d, %d)\n", u, b); while(!h[u].empty()) { int v = *h[u].begin(); h[u].erase(h[u].begin()); res.PB(v); if(v == b) { cycles.PB(res); res.clear(); brk = 1; return; } find_cycle(v,b); if(brk) return; if(!res.empty()) res.erase(--res.end()); } } int main() { scanf("%d %d", &n, &m); REP(i,m) { int a, b; scanf("%d %d", &a, &b); g[a].insert(b); } FOR(i,1,n) { FOR(i,1,n) h[i] = g[i]; while(!h[i].empty()) { brk = 0; res.PB(i); find_cycle(i,i); res.clear(); } } if(!cycles.empty()) { int p = 0; for(VI X: cycles) { if(X.front() != X.back()) continue; p++; sort(ALL(X)); X.erase(unique(ALL(X)), X.end()); for(int x: X) hm[x]++; } VI answer; FOR(i,1,n) if(hm[i] == p) answer.PB(i); printf("%d\n", SIZE(answer)); for(int x: answer) printf("%d ", x); NL; } else { printf("NIE\n"); } if(DEBUG) { for(VI X: cycles) { for(int x: X) printf("%d ", x); NL; } } return 0; } |