#include <bits/stdc++.h> using namespace std; typedef long long LL; template<typename TH> void debug_vars(const char* data, TH head){ cerr << data << "=" << head << "\n"; } template<typename TH, typename... TA> void debug_vars(const char* data, TH head, TA... tail){ while(*data != ',') cerr << *data++; cerr << "=" << head << ","; debug_vars(data+1, tail...); } #ifdef LOCAL #define debug(...) debug_vars(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #endif ///////////////////////////////////////////////////////// const int MaxN = 500005, MaxM = 1000005; int N, M; vector<int> adj[MaxM], revAdj[MaxM]; int cycPos[MaxN]; bool visited[MaxN], current[MaxN]; bool reachable[MaxN]; void input(){ scanf("%d%d", &N, &M); for(int i = 0; i < M; i++){ int u, v; scanf("%d%d", &u, &v); adj[u].push_back(v); revAdj[v].push_back(u); } } vector<int> cycle; bool cnt; bool dfs(int v){ visited[v] = current[v] = true; for(int s : adj[v]){ if(current[s]){ cnt = false; cycle.push_back(s); return true; } if(visited[s]) continue; if(dfs(s)){ cycle.push_back(s); return (cycle[0] != v); } if(!cnt) return false; } current[v] = false; return false; } vector<int> find_cycle(){ cnt = true; fill(current, current+N+1, false); cycle.clear(); for(int i = 1; i <= N; i++){ if(!visited[i]){ dfs(i); if(cycle.size() > 0) break; } } return cycle; } int main(){ input(); vector<int> cyc = find_cycle(); if(cyc.size() == 0){ printf("NIE\n"); return 0; } reverse(cyc.begin(), cyc.end()); fill(visited, visited+N+1, false); for(int i = 0; i < (int)cyc.size(); i++){ visited[cyc[i]] = true; cycPos[cyc[i]] = i+1; } if(find_cycle().size() > 0){ printf("0\n\n"); return 0; } cyc.insert(cyc.begin(), -1); fill(reachable, reachable+N+1, false); // znajdujemy najbardziej na lewo sciezke idaca w lewo int L = -1, R = N+100; for(int id = 1; id < (int)cyc.size(); id++){ int v = cyc[id]; queue<int> Q; Q.push(v); while(!Q.empty()){ int s = Q.front(); Q.pop(); for(int o : revAdj[s]){ if(reachable[o]) continue; reachable[o] = true; if(!cycPos[o]) Q.push(o); } } if(reachable[v]){ R = id; break; } } // znajdujemy najbardziej na prawo sciezke idaca w lewo fill(reachable, reachable+N+1, false); for(int id = (int)cyc.size()-1; id > 0; id--){ int v = cyc[id]; queue<int> Q; Q.push(v); while(!Q.empty()){ int s = Q.front(); Q.pop(); for(int o : adj[s]){ if(reachable[o]) continue; reachable[o] = true; if(!cycPos[o]) Q.push(o); } } if(reachable[v]){ L = id; break; } } // teraz wszystkie odpowiedzi musza byc wewntrz [L,R] if(L > R){ printf("0\n\n"); return 0; } // znowu robimy cos podobnego, ale znow troszke inaczej fill(reachable, reachable+N+1, false); vector<int> result; int minCorrect = 0; for(int id = 1; id < (int)cyc.size(); id++){ int v = cyc[id]; if(id >= minCorrect && id >= L && id <= R) result.push_back(v); queue<int> Q; Q.push(v); while(!Q.empty()){ int s = Q.front(); Q.pop(); for(int o : adj[s]){ if(reachable[o]) continue; reachable[o] = true; if(cycPos[o]){ minCorrect = max(minCorrect, cycPos[o]); } else { Q.push(o); } } } } sort(result.begin(), result.end()); printf("%zu\n", result.size()); for(int v : result) printf("%d ", v); printf("\n"); }
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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; template<typename TH> void debug_vars(const char* data, TH head){ cerr << data << "=" << head << "\n"; } template<typename TH, typename... TA> void debug_vars(const char* data, TH head, TA... tail){ while(*data != ',') cerr << *data++; cerr << "=" << head << ","; debug_vars(data+1, tail...); } #ifdef LOCAL #define debug(...) debug_vars(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #endif ///////////////////////////////////////////////////////// const int MaxN = 500005, MaxM = 1000005; int N, M; vector<int> adj[MaxM], revAdj[MaxM]; int cycPos[MaxN]; bool visited[MaxN], current[MaxN]; bool reachable[MaxN]; void input(){ scanf("%d%d", &N, &M); for(int i = 0; i < M; i++){ int u, v; scanf("%d%d", &u, &v); adj[u].push_back(v); revAdj[v].push_back(u); } } vector<int> cycle; bool cnt; bool dfs(int v){ visited[v] = current[v] = true; for(int s : adj[v]){ if(current[s]){ cnt = false; cycle.push_back(s); return true; } if(visited[s]) continue; if(dfs(s)){ cycle.push_back(s); return (cycle[0] != v); } if(!cnt) return false; } current[v] = false; return false; } vector<int> find_cycle(){ cnt = true; fill(current, current+N+1, false); cycle.clear(); for(int i = 1; i <= N; i++){ if(!visited[i]){ dfs(i); if(cycle.size() > 0) break; } } return cycle; } int main(){ input(); vector<int> cyc = find_cycle(); if(cyc.size() == 0){ printf("NIE\n"); return 0; } reverse(cyc.begin(), cyc.end()); fill(visited, visited+N+1, false); for(int i = 0; i < (int)cyc.size(); i++){ visited[cyc[i]] = true; cycPos[cyc[i]] = i+1; } if(find_cycle().size() > 0){ printf("0\n\n"); return 0; } cyc.insert(cyc.begin(), -1); fill(reachable, reachable+N+1, false); // znajdujemy najbardziej na lewo sciezke idaca w lewo int L = -1, R = N+100; for(int id = 1; id < (int)cyc.size(); id++){ int v = cyc[id]; queue<int> Q; Q.push(v); while(!Q.empty()){ int s = Q.front(); Q.pop(); for(int o : revAdj[s]){ if(reachable[o]) continue; reachable[o] = true; if(!cycPos[o]) Q.push(o); } } if(reachable[v]){ R = id; break; } } // znajdujemy najbardziej na prawo sciezke idaca w lewo fill(reachable, reachable+N+1, false); for(int id = (int)cyc.size()-1; id > 0; id--){ int v = cyc[id]; queue<int> Q; Q.push(v); while(!Q.empty()){ int s = Q.front(); Q.pop(); for(int o : adj[s]){ if(reachable[o]) continue; reachable[o] = true; if(!cycPos[o]) Q.push(o); } } if(reachable[v]){ L = id; break; } } // teraz wszystkie odpowiedzi musza byc wewntrz [L,R] if(L > R){ printf("0\n\n"); return 0; } // znowu robimy cos podobnego, ale znow troszke inaczej fill(reachable, reachable+N+1, false); vector<int> result; int minCorrect = 0; for(int id = 1; id < (int)cyc.size(); id++){ int v = cyc[id]; if(id >= minCorrect && id >= L && id <= R) result.push_back(v); queue<int> Q; Q.push(v); while(!Q.empty()){ int s = Q.front(); Q.pop(); for(int o : adj[s]){ if(reachable[o]) continue; reachable[o] = true; if(cycPos[o]){ minCorrect = max(minCorrect, cycPos[o]); } else { Q.push(o); } } } } sort(result.begin(), result.end()); printf("%zu\n", result.size()); for(int v : result) printf("%d ", v); printf("\n"); } |