#include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <vector> #include <utility> #include <set> #include <stack> #include <map> using namespace std; typedef vector<int> VI; typedef long long LL; #define FOR(x, b, e) for (int x = b; x <= (e); ++x) #define FORD(x, b, e) for (int x = b; x >= (e); --x) #define REP(x, n) for (int x = 0; x < (n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FOREACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define ST first #define ND second int n, m; struct V { int idx; set<V*> roads; int deg; // Tarjan int Tidx; int Tlow; bool TonStack; int Tscc; // Find cycle int Cc; V(int _idx) : idx(_idx), deg(0) ,Tidx(-1), Tlow(-1), TonStack(false), Cc(0) {} bool operator<(V &other) { return deg != other.deg ? deg < other.deg : roads < other.roads; } }; bool compVP(V *a, V *b) { return *a < *b; } V *vs[500000]; map<int, set<V*> > sccs; void strongConnect(V* v, int &index, stack<V*> &S) { v->Tidx = index; v->Tlow = index; index++; S.push(v); v->TonStack = true; FOREACH(r, v->roads) { V *w = *r; if (w->Tidx == -1) { strongConnect(w, index, S); v->Tlow = min(v->Tlow, w->Tlow); } else if (w->TonStack) { v->Tlow = min(v->Tlow, w->Tidx); } } if (v->Tidx == v->Tlow) { V* w; do { w = S.top(); S.pop(); w->TonStack = false; w->Tscc = v->idx; sccs[v->idx].insert(w); } while (w != v); } } void tarjan() { int index = 0; stack<V*> S; REP(i, n) { if (vs[i]->Tidx == -1) strongConnect(vs[i], index, S); } FOREACH(c, sccs) if (c->second.size() == 1) sccs.erase(c); } set<set<int> > cycles; bool hasCycle(V* v, V* without, set<int> &cycle) { if (v == without || v->Cc == 2) { return false; } if (v->Cc == 1) { return true; } v->Cc = 1; FOREACH(c, v->roads) if (hasCycle(*c, without, cycle)) { cycle.insert(v->idx); return true; } v->Cc = 2; return false; } bool hasCycle(V* without) { FOREACH(c, cycles) if (c->find(without->idx) == c->end()) return true; FOREACH(v, sccs.begin()->second) (*v)->Cc = 0; FOREACH(v, sccs.begin()->second) { set<int> cycle; if (hasCycle(*v, without, cycle)) { cycles.insert(cycle); return true; } } return false; } void findSolution() { map<int, set<V*> >::iterator it = sccs.begin(); int scc = it->first; set<V*> elems = it->second; bool single = true; FOREACH(v, elems) { FOREACH(r, (*v)->roads) { if ((*r)->Tscc != scc) { (*v)->roads.erase(r); } else { (*v)->deg++; (*r)->deg++; if ((*v)->deg > 2 || (*r)->deg > 2) single = false; } } } if (!single) { map<int, set<int> > contract; FOREACH(r, elems) { V *v = *r; if (v->deg == 2) { V *c = *v->roads.begin(); while (c->deg == 2) { v->roads.erase(c); v->roads.insert(*c->roads.begin()); elems.erase(c); contract[v->idx].insert(c->idx); map<int, set<int> >::iterator it = contract.find(c->idx); if (it != contract.end()) { contract[v->idx].insert(it->second.begin(), it->second.end()); contract.erase(it); } c = *c->roads.begin(); } } else { set<V*, bool (*)(V*, V*)> sameNodes(compVP); FOREACH(c, v->roads) { if ((*c)->roads.size() + 1 == (*c)->deg) { VAR(ret, sameNodes.insert(*c)); if (!ret.second) { elems.erase(*ret.first); elems.erase(*c); } } } } } set<int> crossroads; FOREACH(r, elems) { V *v = *r; if (!hasCycle(v)) { crossroads.insert(v->idx); map<int, set<int> >::iterator it = contract.find(v->idx); if (it != contract.end()) { crossroads.insert(it->second.begin(), it->second.end()); } } } printf("%ld\n", crossroads.size()); FOREACH(i, crossroads) printf("%d ", *i); } else { printf("%ld\n", elems.size()); FOREACH(r, elems) if ((*r)->Tscc = scc) printf("%d ", (*r)->idx); } } int main() { scanf("%d %d", &n, &m); REP(i, n) vs[i] = new V(i + 1); int a, b; REP(i, m) { scanf("%d %d", &a, &b); vs[a-1]->roads.insert(vs[b-1]); } tarjan(); if (sccs.size() == 0) printf("NIE"); else if (sccs.size() > 1) printf("0"); else findSolution(); printf("\n"); REP(i, n) delete vs[i]; 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 190 191 192 193 194 195 196 197 | #include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <vector> #include <utility> #include <set> #include <stack> #include <map> using namespace std; typedef vector<int> VI; typedef long long LL; #define FOR(x, b, e) for (int x = b; x <= (e); ++x) #define FORD(x, b, e) for (int x = b; x >= (e); --x) #define REP(x, n) for (int x = 0; x < (n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FOREACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define ST first #define ND second int n, m; struct V { int idx; set<V*> roads; int deg; // Tarjan int Tidx; int Tlow; bool TonStack; int Tscc; // Find cycle int Cc; V(int _idx) : idx(_idx), deg(0) ,Tidx(-1), Tlow(-1), TonStack(false), Cc(0) {} bool operator<(V &other) { return deg != other.deg ? deg < other.deg : roads < other.roads; } }; bool compVP(V *a, V *b) { return *a < *b; } V *vs[500000]; map<int, set<V*> > sccs; void strongConnect(V* v, int &index, stack<V*> &S) { v->Tidx = index; v->Tlow = index; index++; S.push(v); v->TonStack = true; FOREACH(r, v->roads) { V *w = *r; if (w->Tidx == -1) { strongConnect(w, index, S); v->Tlow = min(v->Tlow, w->Tlow); } else if (w->TonStack) { v->Tlow = min(v->Tlow, w->Tidx); } } if (v->Tidx == v->Tlow) { V* w; do { w = S.top(); S.pop(); w->TonStack = false; w->Tscc = v->idx; sccs[v->idx].insert(w); } while (w != v); } } void tarjan() { int index = 0; stack<V*> S; REP(i, n) { if (vs[i]->Tidx == -1) strongConnect(vs[i], index, S); } FOREACH(c, sccs) if (c->second.size() == 1) sccs.erase(c); } set<set<int> > cycles; bool hasCycle(V* v, V* without, set<int> &cycle) { if (v == without || v->Cc == 2) { return false; } if (v->Cc == 1) { return true; } v->Cc = 1; FOREACH(c, v->roads) if (hasCycle(*c, without, cycle)) { cycle.insert(v->idx); return true; } v->Cc = 2; return false; } bool hasCycle(V* without) { FOREACH(c, cycles) if (c->find(without->idx) == c->end()) return true; FOREACH(v, sccs.begin()->second) (*v)->Cc = 0; FOREACH(v, sccs.begin()->second) { set<int> cycle; if (hasCycle(*v, without, cycle)) { cycles.insert(cycle); return true; } } return false; } void findSolution() { map<int, set<V*> >::iterator it = sccs.begin(); int scc = it->first; set<V*> elems = it->second; bool single = true; FOREACH(v, elems) { FOREACH(r, (*v)->roads) { if ((*r)->Tscc != scc) { (*v)->roads.erase(r); } else { (*v)->deg++; (*r)->deg++; if ((*v)->deg > 2 || (*r)->deg > 2) single = false; } } } if (!single) { map<int, set<int> > contract; FOREACH(r, elems) { V *v = *r; if (v->deg == 2) { V *c = *v->roads.begin(); while (c->deg == 2) { v->roads.erase(c); v->roads.insert(*c->roads.begin()); elems.erase(c); contract[v->idx].insert(c->idx); map<int, set<int> >::iterator it = contract.find(c->idx); if (it != contract.end()) { contract[v->idx].insert(it->second.begin(), it->second.end()); contract.erase(it); } c = *c->roads.begin(); } } else { set<V*, bool (*)(V*, V*)> sameNodes(compVP); FOREACH(c, v->roads) { if ((*c)->roads.size() + 1 == (*c)->deg) { VAR(ret, sameNodes.insert(*c)); if (!ret.second) { elems.erase(*ret.first); elems.erase(*c); } } } } } set<int> crossroads; FOREACH(r, elems) { V *v = *r; if (!hasCycle(v)) { crossroads.insert(v->idx); map<int, set<int> >::iterator it = contract.find(v->idx); if (it != contract.end()) { crossroads.insert(it->second.begin(), it->second.end()); } } } printf("%ld\n", crossroads.size()); FOREACH(i, crossroads) printf("%d ", *i); } else { printf("%ld\n", elems.size()); FOREACH(r, elems) if ((*r)->Tscc = scc) printf("%d ", (*r)->idx); } } int main() { scanf("%d %d", &n, &m); REP(i, n) vs[i] = new V(i + 1); int a, b; REP(i, m) { scanf("%d %d", &a, &b); vs[a-1]->roads.insert(vs[b-1]); } tarjan(); if (sccs.size() == 0) printf("NIE"); else if (sccs.size() > 1) printf("0"); else findSolution(); printf("\n"); REP(i, n) delete vs[i]; return 0; } |