#include <iostream> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <queue> //#include <stack> #include <cmath> #include <cassert> #include <cstring> #include <ext/numeric> using namespace std ; using namespace __gnu_cxx ; typedef long long LL ; typedef pair<int,int> PII ; typedef vector<int> VI ; const int INF = 1e9+100 ; const LL INFLL = (LL)INF * (LL)INF ; #define REP(i,n) for(i=0;i<(n);++i) #define ALL(c) c.begin(),c.end() #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define CLEAR(t) memset(t,0,sizeof(t)) #define PB push_back #define MP make_pair #define FI first #define SE second template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { s<< "(" << x.FI << "," << x.SE << ")"; return s;} template<class T> ostream& operator<<(ostream &s, const vector<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T> ostream& operator<<(ostream &s, const set<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; } /////////////////////////////////////////// const int MAXN = 500100 ; VI G[MAXN], revG[MAXN] ; int used[MAXN] ; bool revUsed[MAXN] ; VI stack, cycle ; bool DFS(int u) { // zwraca true gdy natrafi na cykl stack.PB(u) ; used[u]=1 ; FOREACH(it, G[u]) { if(used[*it]==1) { // mamy cykl do { cycle.PB(stack.back()) ; stack.pop_back() ; } while(cycle.back()!=(*it)) ; reverse(ALL(cycle)) ; return true ; } if(used[*it]==0) { if(DFS(*it)) return true ; } } used[u]=2 ; stack.pop_back() ; return false ; } bool findCycle(int n, const VI &forbidden) { CLEAR(used) ; FOREACH(it, forbidden) used[*it]=2 ; cycle.clear() ; stack.clear() ; for(int i=1 ; i<=n ; i++) if(!used[i]) if(DFS(i)) return true ; return false ; } ////////////////////////////////////////////////// int posInCycle[MAXN] ; int goForward(int u) { int ret=-1 ; used[u] = true ; FOREACH(it, G[u]) { ret = max(ret, posInCycle[*it]) ; if(!used[*it] && posInCycle[*it]==-1) ret = max(ret, goForward(*it)) ; } return ret ; } void goBackward(int u) { revUsed[u] = true ; FOREACH(it, revG[u]) if(!revUsed[*it] && posInCycle[*it]==-1) goBackward(*it) ; } int main() { ios_base::sync_with_stdio(0) ; int n, m, i, a, b ; cin >> n >> m ; while(m--) { cin >> a >> b ; G[a].PB(b) ; revG[b].PB(a) ; } if(!findCycle(n, VI())) { cout << "NIE" << endl ; return 0 ; } VI mainCycle = cycle ; // cout << "mainCycle : " << mainCycle << endl ; if(findCycle(n, mainCycle)) { // cout << "jest inny cykl: " << cycle << endl ; cout << 0 << endl << endl ; return 0 ; } memset(posInCycle, -1, sizeof(posInCycle)) ; REP(i, mainCycle.size()) posInCycle[mainCycle[i]] = i ; CLEAR(used) ; CLEAR(revUsed) ; vector<bool> isBad(mainCycle.size()) ; // czy niespelnia jeden z trzech warunkow int bestForward = -1 ; bool canGoBack=false ; REP(i, mainCycle.size()) { // cout << "i=" << i << ", u = " << cycle[i] << ", bestForward = " << bestForward << ", canGoBack = " << canGoBack << endl ; if(bestForward > i) isBad[i] = true ; if(canGoBack) isBad[i] = true ; bestForward = max(bestForward, goForward(cycle[i])) ; goBackward(cycle[i]) ; FOREACH(it, G[cycle[i]]) if(revUsed[*it]) canGoBack=true ; } // cout << isBad << endl ; canGoBack=false ; CLEAR(used) ; for(i=mainCycle.size()-1 ; i>=0 ; i--) { if(canGoBack) isBad[i]=true ; goForward(cycle[i]) ; FOREACH(it, revG[cycle[i]]) if(used[*it]) canGoBack=true ; } // cout << isBad << endl ; VI ret ; REP(i, isBad.size()) if(!isBad[i]) ret.PB(mainCycle[i]) ; sort(ALL(ret)) ; cout << ret.size() << endl << ret << endl ; }
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 | #include <iostream> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <queue> //#include <stack> #include <cmath> #include <cassert> #include <cstring> #include <ext/numeric> using namespace std ; using namespace __gnu_cxx ; typedef long long LL ; typedef pair<int,int> PII ; typedef vector<int> VI ; const int INF = 1e9+100 ; const LL INFLL = (LL)INF * (LL)INF ; #define REP(i,n) for(i=0;i<(n);++i) #define ALL(c) c.begin(),c.end() #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define CLEAR(t) memset(t,0,sizeof(t)) #define PB push_back #define MP make_pair #define FI first #define SE second template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { s<< "(" << x.FI << "," << x.SE << ")"; return s;} template<class T> ostream& operator<<(ostream &s, const vector<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T> ostream& operator<<(ostream &s, const set<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; } /////////////////////////////////////////// const int MAXN = 500100 ; VI G[MAXN], revG[MAXN] ; int used[MAXN] ; bool revUsed[MAXN] ; VI stack, cycle ; bool DFS(int u) { // zwraca true gdy natrafi na cykl stack.PB(u) ; used[u]=1 ; FOREACH(it, G[u]) { if(used[*it]==1) { // mamy cykl do { cycle.PB(stack.back()) ; stack.pop_back() ; } while(cycle.back()!=(*it)) ; reverse(ALL(cycle)) ; return true ; } if(used[*it]==0) { if(DFS(*it)) return true ; } } used[u]=2 ; stack.pop_back() ; return false ; } bool findCycle(int n, const VI &forbidden) { CLEAR(used) ; FOREACH(it, forbidden) used[*it]=2 ; cycle.clear() ; stack.clear() ; for(int i=1 ; i<=n ; i++) if(!used[i]) if(DFS(i)) return true ; return false ; } ////////////////////////////////////////////////// int posInCycle[MAXN] ; int goForward(int u) { int ret=-1 ; used[u] = true ; FOREACH(it, G[u]) { ret = max(ret, posInCycle[*it]) ; if(!used[*it] && posInCycle[*it]==-1) ret = max(ret, goForward(*it)) ; } return ret ; } void goBackward(int u) { revUsed[u] = true ; FOREACH(it, revG[u]) if(!revUsed[*it] && posInCycle[*it]==-1) goBackward(*it) ; } int main() { ios_base::sync_with_stdio(0) ; int n, m, i, a, b ; cin >> n >> m ; while(m--) { cin >> a >> b ; G[a].PB(b) ; revG[b].PB(a) ; } if(!findCycle(n, VI())) { cout << "NIE" << endl ; return 0 ; } VI mainCycle = cycle ; // cout << "mainCycle : " << mainCycle << endl ; if(findCycle(n, mainCycle)) { // cout << "jest inny cykl: " << cycle << endl ; cout << 0 << endl << endl ; return 0 ; } memset(posInCycle, -1, sizeof(posInCycle)) ; REP(i, mainCycle.size()) posInCycle[mainCycle[i]] = i ; CLEAR(used) ; CLEAR(revUsed) ; vector<bool> isBad(mainCycle.size()) ; // czy niespelnia jeden z trzech warunkow int bestForward = -1 ; bool canGoBack=false ; REP(i, mainCycle.size()) { // cout << "i=" << i << ", u = " << cycle[i] << ", bestForward = " << bestForward << ", canGoBack = " << canGoBack << endl ; if(bestForward > i) isBad[i] = true ; if(canGoBack) isBad[i] = true ; bestForward = max(bestForward, goForward(cycle[i])) ; goBackward(cycle[i]) ; FOREACH(it, G[cycle[i]]) if(revUsed[*it]) canGoBack=true ; } // cout << isBad << endl ; canGoBack=false ; CLEAR(used) ; for(i=mainCycle.size()-1 ; i>=0 ; i--) { if(canGoBack) isBad[i]=true ; goForward(cycle[i]) ; FOREACH(it, revG[cycle[i]]) if(used[*it]) canGoBack=true ; } // cout << isBad << endl ; VI ret ; REP(i, isBad.size()) if(!isBad[i]) ret.PB(mainCycle[i]) ; sort(ALL(ret)) ; cout << ret.size() << endl << ret << endl ; } |