#include <iostream> #include <vector> #include <queue> #include <algorithm> #include <set> using namespace std; struct Node; typedef vector<Node *> Nodes; struct Node { Node () : scc(-1), visited(false), in(0), iter(-1), started(false) {} Nodes neighbours; Nodes parents; int scc; int id; bool visited; unsigned int in; int iter; bool started; }; typedef vector<Node> Graph; void getTimes( Node *n, Nodes × ) { if (n->visited) return; n->visited = true; for (auto next: n->neighbours) { if (!next->visited) getTimes(next, times); } times.push_back(n); } void getSCC( Node *n, vector<int> &sccs, int i ) { if (n->scc != -1) return; n->scc = i; sccs[i]++; for (auto next: n->parents) { if (next->scc == -1) getSCC(next, sccs, i); } } int SCC( Graph &g ) { int gsize = g.size(); Nodes times; times.reserve (g.size()); for (int i = 0; i < gsize; i++) { if (!g[i].visited) { getTimes(&g[i], times); } } vector<int> sccs(gsize, 0); for (int i = gsize-1; i >= 0; i--) { if (times[i]->scc == -1) { getSCC(times[i], sccs, i); } } int gotOne = -2; for (int i = 0; i < gsize; i++) { if (sccs[i] > 1) { if (gotOne > 0) return -1; gotOne = i; } } return gotOne; } void clearEdges(Nodes &nodes, int scc) { nodes.erase(remove_if(nodes.begin(), nodes.end(), [&scc] (const Node *n) { return n->scc != scc; }), nodes.end()); } void getResult(Graph &g, Nodes &resultNodes, int scc) { Node *startNode = 0; for (auto &n: g) { if (n.scc == scc) { clearEdges(n.neighbours, scc); clearEdges(n.parents, scc); } } // come on, there must be a startNode! // now, graph starting with startNode contains only // single strongly connected component // first, find a node that have neighbours with 1 out and 1 in degree for (auto &n: g) { if (n.scc == scc) { bool oneOut = false; for (auto &next : n.neighbours) { if (next->parents.size() == 1) oneOut = true; } for (auto &next : n.parents) { if (next->neighbours.size() == 1 && oneOut) { startNode = &n; break; } } if (startNode != 0) break; } } if (startNode == 0) { // surely there is no common crossroad return; } set<Node *> reached; // try topological sort; queue<Node *> q; bool found = false; while (!found) { while (!q.empty()) q.pop(); q.push(startNode); startNode->started = true; while (!q.empty() && !found) { Node *n = q.front(); q.pop(); reached.erase(n); for (auto &next : n->neighbours) { if (next->iter != startNode->id) { next->in = 0; next->iter = startNode->id; } next->in++; if (next->in == next->parents.size()) { if (next==startNode) { if (q.empty()) { found = true; // we got one! resultNodes.push_back(next); } } else { q.push(next); } } else { if (!next->started) { reached.insert(next); } } } } // if failed - get one of reached nodes, and try again, until it succeeds if (!found) { if (!reached.empty()) startNode = *(reached.begin()); else return; } // could it be that we would not find vertex? } // there is first result; already in resultNodes // then, flush for next results while (!q.empty()) q.pop(); q.push(resultNodes[0]); int gsize = g.size(); unsigned int openEdges = 0; resultNodes[0]->in = openEdges; while (!q.empty()) { Node *n = q.front(); q.pop(); openEdges -= n->in; for (auto &next : n->neighbours) { if (next->iter != gsize) { next->in = 0; next->iter = gsize; } next->in++; openEdges++; } for (auto &next : n->neighbours) { if (next->in == next->parents.size()) { if (next != resultNodes[0]) { q.push(next); if (openEdges == next->in) resultNodes.push_back(next); } } } } } int main() { ios_base::sync_with_stdio(false); int N, M; cin >> N >> M; Graph g(N); for (int i = 0; i < N; i++) g[i].id = i; for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; g[a-1].neighbours.push_back(&g[b-1]); g[b-1].parents.push_back(&g[a-1]); } int res = SCC(g); if (-2 == res) cout << "NIE" << endl; else { Nodes resultNodes; if (res >=0) getResult(g, resultNodes, res); else res = 0; cout << resultNodes.size() << endl; sort(resultNodes.begin(), resultNodes.end(), [] (const Node *a, const Node *b) { return a->id < b->id; }); for_each(resultNodes.begin(), resultNodes.end(), [] (const Node *n) { cout << n->id+1 << " "; }); cout << endl; } 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 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 | #include <iostream> #include <vector> #include <queue> #include <algorithm> #include <set> using namespace std; struct Node; typedef vector<Node *> Nodes; struct Node { Node () : scc(-1), visited(false), in(0), iter(-1), started(false) {} Nodes neighbours; Nodes parents; int scc; int id; bool visited; unsigned int in; int iter; bool started; }; typedef vector<Node> Graph; void getTimes( Node *n, Nodes × ) { if (n->visited) return; n->visited = true; for (auto next: n->neighbours) { if (!next->visited) getTimes(next, times); } times.push_back(n); } void getSCC( Node *n, vector<int> &sccs, int i ) { if (n->scc != -1) return; n->scc = i; sccs[i]++; for (auto next: n->parents) { if (next->scc == -1) getSCC(next, sccs, i); } } int SCC( Graph &g ) { int gsize = g.size(); Nodes times; times.reserve (g.size()); for (int i = 0; i < gsize; i++) { if (!g[i].visited) { getTimes(&g[i], times); } } vector<int> sccs(gsize, 0); for (int i = gsize-1; i >= 0; i--) { if (times[i]->scc == -1) { getSCC(times[i], sccs, i); } } int gotOne = -2; for (int i = 0; i < gsize; i++) { if (sccs[i] > 1) { if (gotOne > 0) return -1; gotOne = i; } } return gotOne; } void clearEdges(Nodes &nodes, int scc) { nodes.erase(remove_if(nodes.begin(), nodes.end(), [&scc] (const Node *n) { return n->scc != scc; }), nodes.end()); } void getResult(Graph &g, Nodes &resultNodes, int scc) { Node *startNode = 0; for (auto &n: g) { if (n.scc == scc) { clearEdges(n.neighbours, scc); clearEdges(n.parents, scc); } } // come on, there must be a startNode! // now, graph starting with startNode contains only // single strongly connected component // first, find a node that have neighbours with 1 out and 1 in degree for (auto &n: g) { if (n.scc == scc) { bool oneOut = false; for (auto &next : n.neighbours) { if (next->parents.size() == 1) oneOut = true; } for (auto &next : n.parents) { if (next->neighbours.size() == 1 && oneOut) { startNode = &n; break; } } if (startNode != 0) break; } } if (startNode == 0) { // surely there is no common crossroad return; } set<Node *> reached; // try topological sort; queue<Node *> q; bool found = false; while (!found) { while (!q.empty()) q.pop(); q.push(startNode); startNode->started = true; while (!q.empty() && !found) { Node *n = q.front(); q.pop(); reached.erase(n); for (auto &next : n->neighbours) { if (next->iter != startNode->id) { next->in = 0; next->iter = startNode->id; } next->in++; if (next->in == next->parents.size()) { if (next==startNode) { if (q.empty()) { found = true; // we got one! resultNodes.push_back(next); } } else { q.push(next); } } else { if (!next->started) { reached.insert(next); } } } } // if failed - get one of reached nodes, and try again, until it succeeds if (!found) { if (!reached.empty()) startNode = *(reached.begin()); else return; } // could it be that we would not find vertex? } // there is first result; already in resultNodes // then, flush for next results while (!q.empty()) q.pop(); q.push(resultNodes[0]); int gsize = g.size(); unsigned int openEdges = 0; resultNodes[0]->in = openEdges; while (!q.empty()) { Node *n = q.front(); q.pop(); openEdges -= n->in; for (auto &next : n->neighbours) { if (next->iter != gsize) { next->in = 0; next->iter = gsize; } next->in++; openEdges++; } for (auto &next : n->neighbours) { if (next->in == next->parents.size()) { if (next != resultNodes[0]) { q.push(next); if (openEdges == next->in) resultNodes.push_back(next); } } } } } int main() { ios_base::sync_with_stdio(false); int N, M; cin >> N >> M; Graph g(N); for (int i = 0; i < N; i++) g[i].id = i; for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; g[a-1].neighbours.push_back(&g[b-1]); g[b-1].parents.push_back(&g[a-1]); } int res = SCC(g); if (-2 == res) cout << "NIE" << endl; else { Nodes resultNodes; if (res >=0) getResult(g, resultNodes, res); else res = 0; cout << resultNodes.size() << endl; sort(resultNodes.begin(), resultNodes.end(), [] (const Node *a, const Node *b) { return a->id < b->id; }); for_each(resultNodes.begin(), resultNodes.end(), [] (const Node *n) { cout << n->id+1 << " "; }); cout << endl; } return 0; } |