#include <iostream> #include <vector> #include <algorithm> #include <set> #include <map> #include <string> #include <sstream> #include <cstdlib> #include <ctime> #include <iterator> #include <stack> #include <queue> using namespace std; typedef pair<int, int> P; // graph has outgoing edges, dual has incoming edges map<int, pair<set<int>, set<int> > > graph; // node, edges in graph, edges in dual graph map<int, set<int> > sccs; // scc leader number, nodes in it map<int, bool> vr; map<int, bool> v; priority_queue<P, vector<P>, less<P> > fs; //first f, second node priority_queue<P, vector<P>, less<P> > scs; //first size, second leader int s; int t = 0; void DFSr(int node) { if(vr[node]) return; vr[node] = true; for(set<int>::iterator itn = graph[node].second.begin(); itn != graph[node].second.end(); ++itn) { // cout << "Internal DFS " << *itn << endl; if(!vr[*itn]) DFSr(*itn); } ++t; fs.push(make_pair(t, node)); // cout << "Finished visiting node " << node << " t " << t << endl; } void DFS(int node) { if(v[node]) return; // cout << "Marking node " << node << " as a follower of " << s << endl; v[node] = true; sccs[s].insert(node); for(set<int>::iterator itn = graph[node].first.begin(); itn != graph[node].first.end(); ++itn) { if(!v[*itn]) DFS(*itn); } // cout << "Finished marking node " << node << " leader " << s << endl; } int main() { string line; std::ios_base::sync_with_stdio (false); getline(cin, line); // skip redundant data while (getline(cin, line)) { istringstream is(line); vector<int> edge = vector<int>(istream_iterator<int>(is), istream_iterator<int>()); // cout << "edge from: " << edge[0] << " to : " << edge[1] << endl; if(graph.find(edge[0]) != graph.end()) { graph[edge[0]].first.insert(edge[1]); } else { graph[edge[0]] = make_pair(set<int>(), set<int>()); graph[edge[0]].first.insert(edge[1]); vr[edge[0]] = false; v[edge[0]] = false; } if(graph.find(edge[1]) != graph.end()) { graph[edge[1]].second.insert(edge[0]); } else { graph[edge[1]] = make_pair(set<int>(), set<int>()); graph[edge[1]].second.insert(edge[0]); vr[edge[1]] = false; v[edge[1]] = false; } } int t = 0; for(map<int, pair<set<int>, set<int> > >::iterator it = graph.begin(); it != graph.end(); ++it) { int node = it->first; // cout << "Going to node " << it->first << endl; // cout << "Straight edges to "; // for(set<int>::iterator itn = graph[node].first.begin(); itn != graph[node].first.end(); ++itn) { // cout << *itn << " "; // } // cout << endl; // cout << "Reverse edges to "; // for(set<int>::iterator itn = graph[node].second.begin(); itn != graph[node].second.end(); ++itn) { // cout << *itn << " "; // } // cout << endl; DFSr(it->first); } // cout << "Phase two" << endl; // cout << "fs size " << fs.size() << endl; while(!fs.empty()) { // cout << "current fs " << fs.top().first << "/" << fs.top().second << endl; s = fs.top().second; fs.pop(); // second DFS on normal graph if(!v[s]) { // cout << "Visiting leader node " << s << endl; // DFS it! sccs[s] = set<int>(); DFS(s); } } for(map<int, set<int> >::iterator it = sccs.begin(); it != sccs.end(); ++it) { // cout << "SCCS with leader: " << it->first << " containing: "; // for(set<int>::iterator itn = it->second.begin(); itn != it->second.end(); ++itn) { // cout << *itn << " "; // } // cout << endl; scs.push(make_pair(it->second.size(), it->first)); } int big_count = 0; int bl = -1; // big_leader while(!scs.empty()) { // cout << "SCS with leader: " << scs.top().second << " count " << scs.top().first << endl; if (scs.top().first > 1) { bl = scs.top().second; ++big_count; } scs.pop(); } if (big_count != 1) { cout << "NIE" << endl; } else { // filter the graph to contain only the ones in the SCS map<int, pair<set<int>, set<int> > > fgraph; int maxout = -1; int maxin = -1; for(set<int>::iterator itn = sccs[bl].begin(); itn != sccs[bl].end(); ++itn) { // cout << *itn << " "; set<int> outs; set<int> ins; int v = *itn; std::set_intersection(graph[v].first.begin(), graph[v].first.end(), sccs[bl].begin(), sccs[bl].end(), std::inserter(outs, outs.begin())); std::set_intersection(graph[v].second.begin(), graph[v].second.end(), sccs[bl].begin(), sccs[bl].end(), std::inserter(ins, ins.begin())); if (maxout < outs.size()) { maxout = outs.size(); } if (maxin < ins.size()) { maxin = ins.size(); } fgraph[v] = make_pair(outs, ins); } // cout << endl; // calculate the degrees, if out is always 1 then all in cycle if (maxout == 1) { // cout << "FINAL" << endl; for(set<int>::iterator itn = sccs[bl].begin(); itn != sccs[bl].end(); ++itn) { cout << *itn << " "; } cout << endl; return 0; } // otherwise start from leader and run a dfs (might be simple next next next while not again leader) // and mark from the one that has in > 1 to the one that has out > 1 int lastin = -1; int firstin = -1; int lastout = -1; int firstout = -1; vector<int> mc; //main cycle int v = bl; do { // cout << "mc node: " << v << endl; if ((firstout == -1) && (fgraph[v].first.size() > 1)) { firstout = v; } if (fgraph[v].first.size() > 1) { lastout = v; } if ((firstin == -1) && (fgraph[v].second.size() > 1)) { firstin = v; } if (fgraph[v].second.size() > 1) { lastin = v; } mc.push_back(v); v = *(fgraph[v].first.begin()); } while (v != bl); // cout << "DEGz" << endl; // cout << "fi " << firstin << " li " << lastin << " fo " << firstout << " lo " << lastout << endl; if(lastin == -1) lastin = bl; if(firstout == -1); firstout = mc[mc.size() - 1]; // extend for case li > fo for (int el : mc) { mc.push_back(el); } set<int> result; int i = 0; while (mc[i] != lastin) ++i; result.insert(mc[i]); while (mc[i] != firstout) { ++i; result.insert(mc[i]); } cout << result.size() << endl; for (int vres : result) { cout << vres << " "; } 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 | #include <iostream> #include <vector> #include <algorithm> #include <set> #include <map> #include <string> #include <sstream> #include <cstdlib> #include <ctime> #include <iterator> #include <stack> #include <queue> using namespace std; typedef pair<int, int> P; // graph has outgoing edges, dual has incoming edges map<int, pair<set<int>, set<int> > > graph; // node, edges in graph, edges in dual graph map<int, set<int> > sccs; // scc leader number, nodes in it map<int, bool> vr; map<int, bool> v; priority_queue<P, vector<P>, less<P> > fs; //first f, second node priority_queue<P, vector<P>, less<P> > scs; //first size, second leader int s; int t = 0; void DFSr(int node) { if(vr[node]) return; vr[node] = true; for(set<int>::iterator itn = graph[node].second.begin(); itn != graph[node].second.end(); ++itn) { // cout << "Internal DFS " << *itn << endl; if(!vr[*itn]) DFSr(*itn); } ++t; fs.push(make_pair(t, node)); // cout << "Finished visiting node " << node << " t " << t << endl; } void DFS(int node) { if(v[node]) return; // cout << "Marking node " << node << " as a follower of " << s << endl; v[node] = true; sccs[s].insert(node); for(set<int>::iterator itn = graph[node].first.begin(); itn != graph[node].first.end(); ++itn) { if(!v[*itn]) DFS(*itn); } // cout << "Finished marking node " << node << " leader " << s << endl; } int main() { string line; std::ios_base::sync_with_stdio (false); getline(cin, line); // skip redundant data while (getline(cin, line)) { istringstream is(line); vector<int> edge = vector<int>(istream_iterator<int>(is), istream_iterator<int>()); // cout << "edge from: " << edge[0] << " to : " << edge[1] << endl; if(graph.find(edge[0]) != graph.end()) { graph[edge[0]].first.insert(edge[1]); } else { graph[edge[0]] = make_pair(set<int>(), set<int>()); graph[edge[0]].first.insert(edge[1]); vr[edge[0]] = false; v[edge[0]] = false; } if(graph.find(edge[1]) != graph.end()) { graph[edge[1]].second.insert(edge[0]); } else { graph[edge[1]] = make_pair(set<int>(), set<int>()); graph[edge[1]].second.insert(edge[0]); vr[edge[1]] = false; v[edge[1]] = false; } } int t = 0; for(map<int, pair<set<int>, set<int> > >::iterator it = graph.begin(); it != graph.end(); ++it) { int node = it->first; // cout << "Going to node " << it->first << endl; // cout << "Straight edges to "; // for(set<int>::iterator itn = graph[node].first.begin(); itn != graph[node].first.end(); ++itn) { // cout << *itn << " "; // } // cout << endl; // cout << "Reverse edges to "; // for(set<int>::iterator itn = graph[node].second.begin(); itn != graph[node].second.end(); ++itn) { // cout << *itn << " "; // } // cout << endl; DFSr(it->first); } // cout << "Phase two" << endl; // cout << "fs size " << fs.size() << endl; while(!fs.empty()) { // cout << "current fs " << fs.top().first << "/" << fs.top().second << endl; s = fs.top().second; fs.pop(); // second DFS on normal graph if(!v[s]) { // cout << "Visiting leader node " << s << endl; // DFS it! sccs[s] = set<int>(); DFS(s); } } for(map<int, set<int> >::iterator it = sccs.begin(); it != sccs.end(); ++it) { // cout << "SCCS with leader: " << it->first << " containing: "; // for(set<int>::iterator itn = it->second.begin(); itn != it->second.end(); ++itn) { // cout << *itn << " "; // } // cout << endl; scs.push(make_pair(it->second.size(), it->first)); } int big_count = 0; int bl = -1; // big_leader while(!scs.empty()) { // cout << "SCS with leader: " << scs.top().second << " count " << scs.top().first << endl; if (scs.top().first > 1) { bl = scs.top().second; ++big_count; } scs.pop(); } if (big_count != 1) { cout << "NIE" << endl; } else { // filter the graph to contain only the ones in the SCS map<int, pair<set<int>, set<int> > > fgraph; int maxout = -1; int maxin = -1; for(set<int>::iterator itn = sccs[bl].begin(); itn != sccs[bl].end(); ++itn) { // cout << *itn << " "; set<int> outs; set<int> ins; int v = *itn; std::set_intersection(graph[v].first.begin(), graph[v].first.end(), sccs[bl].begin(), sccs[bl].end(), std::inserter(outs, outs.begin())); std::set_intersection(graph[v].second.begin(), graph[v].second.end(), sccs[bl].begin(), sccs[bl].end(), std::inserter(ins, ins.begin())); if (maxout < outs.size()) { maxout = outs.size(); } if (maxin < ins.size()) { maxin = ins.size(); } fgraph[v] = make_pair(outs, ins); } // cout << endl; // calculate the degrees, if out is always 1 then all in cycle if (maxout == 1) { // cout << "FINAL" << endl; for(set<int>::iterator itn = sccs[bl].begin(); itn != sccs[bl].end(); ++itn) { cout << *itn << " "; } cout << endl; return 0; } // otherwise start from leader and run a dfs (might be simple next next next while not again leader) // and mark from the one that has in > 1 to the one that has out > 1 int lastin = -1; int firstin = -1; int lastout = -1; int firstout = -1; vector<int> mc; //main cycle int v = bl; do { // cout << "mc node: " << v << endl; if ((firstout == -1) && (fgraph[v].first.size() > 1)) { firstout = v; } if (fgraph[v].first.size() > 1) { lastout = v; } if ((firstin == -1) && (fgraph[v].second.size() > 1)) { firstin = v; } if (fgraph[v].second.size() > 1) { lastin = v; } mc.push_back(v); v = *(fgraph[v].first.begin()); } while (v != bl); // cout << "DEGz" << endl; // cout << "fi " << firstin << " li " << lastin << " fo " << firstout << " lo " << lastout << endl; if(lastin == -1) lastin = bl; if(firstout == -1); firstout = mc[mc.size() - 1]; // extend for case li > fo for (int el : mc) { mc.push_back(el); } set<int> result; int i = 0; while (mc[i] != lastin) ++i; result.insert(mc[i]); while (mc[i] != firstout) { ++i; result.insert(mc[i]); } cout << result.size() << endl; for (int vres : result) { cout << vres << " "; } cout << endl; } return 0; } |