#include <cstdlib> #include <set> #include <stack> #include <vector> #include <iostream> using namespace std; typedef vector<int> Vertex; typedef vector<Vertex> Graph; void strongConnect(Graph const &gr, vector<int> &idx, vector<int> &low, stack<int> &st, vector<bool> &on, int &index, int const &v, vector< vector<int> > &scs) { idx[v] = index; low[v] = index; ++index; st.push(v); on[v] = true; for(unsigned int i = 0; i < gr[v].size(); ++i) { int w = gr[v][i]; if(idx[w] == -1) { strongConnect(gr, idx, low, st, on, index, w, scs); low[v] = min(low[v], low[w]); } else if(on[w]) { low[v] = min(low[v], idx[w]); } } if(idx[v] == low[v]) { scs.push_back(vector<int>()); while(true) { int w = st.top(); st.pop(); on[w] = false; scs.back().push_back(w); if(w == v) break; } if(scs.back().size() == 1) scs.pop_back(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); srand(0xdeadbeef); int n, m; cin >> n >> m; Graph gr(n); for(int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a; --b; gr[a].push_back(b); } int index = 0; vector<int> idx(n, -1); vector<int> low(n, -1); vector<bool> on(n, false); stack<int> st; vector< vector<int> > scs; for(int v = 0; v < n; ++v) { if(idx[v] == -1) { strongConnect(gr, idx, low, st, on, index, v, scs); } } if(scs.size() == 0) { cout << "NIE\n"; return 0; } if(scs.size() > 1) { cout << "0\n"; return 0; } vector<int> &osc = scs[0]; set<int> oscs; for(unsigned int i = 0; i < osc.size(); ++i) oscs.insert(osc[i]); for(unsigned int i = 0; i < osc.size(); ++i) { int v = osc[i]; for(unsigned int j = 0; j < gr[v].size(); ++j) { int w = gr[v][j]; if(oscs.find(w) == oscs.end()) { gr[v][j] = gr[v].back(); gr[v].pop_back(); --j; } } } int stepsLeft = 2000000; for(int i = 0; stepsLeft > 0; ++i) { int v0 = osc[rand() % osc.size()]; int offset = rand(); stack<int> st; stack<int> ft; st.push(v0); ft.push(v0); on[v0] = true; while(not st.empty()) { int v = st.top(); st.pop(); --stepsLeft; bool broken = false; for(unsigned int j = 0; j < gr[v].size(); ++j) { int w = gr[v][(j + offset) % gr[v].size()]; if(w == v0) { for(set<int>::iterator it = oscs.begin(); it != oscs.end();) { if(not on[*it]) { oscs.erase(it++); } else { ++it; } } broken = true; break; } } if(broken) break; for(unsigned int j = 0; j < gr[v].size(); ++j) { int w = gr[v][(j + offset) % gr[v].size()]; if(not on[w]) { st.push(w); ft.push(w); on[w] = true; break; } } } while(not ft.empty()) { on[ft.top()] = false; ft.pop(); } } cout << oscs.size() << '\n'; for(set<int>::iterator it = oscs.begin(); it != oscs.end(); ++it) { cout << 1 + *it << ' '; } cout << '\n'; 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 | #include <cstdlib> #include <set> #include <stack> #include <vector> #include <iostream> using namespace std; typedef vector<int> Vertex; typedef vector<Vertex> Graph; void strongConnect(Graph const &gr, vector<int> &idx, vector<int> &low, stack<int> &st, vector<bool> &on, int &index, int const &v, vector< vector<int> > &scs) { idx[v] = index; low[v] = index; ++index; st.push(v); on[v] = true; for(unsigned int i = 0; i < gr[v].size(); ++i) { int w = gr[v][i]; if(idx[w] == -1) { strongConnect(gr, idx, low, st, on, index, w, scs); low[v] = min(low[v], low[w]); } else if(on[w]) { low[v] = min(low[v], idx[w]); } } if(idx[v] == low[v]) { scs.push_back(vector<int>()); while(true) { int w = st.top(); st.pop(); on[w] = false; scs.back().push_back(w); if(w == v) break; } if(scs.back().size() == 1) scs.pop_back(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); srand(0xdeadbeef); int n, m; cin >> n >> m; Graph gr(n); for(int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a; --b; gr[a].push_back(b); } int index = 0; vector<int> idx(n, -1); vector<int> low(n, -1); vector<bool> on(n, false); stack<int> st; vector< vector<int> > scs; for(int v = 0; v < n; ++v) { if(idx[v] == -1) { strongConnect(gr, idx, low, st, on, index, v, scs); } } if(scs.size() == 0) { cout << "NIE\n"; return 0; } if(scs.size() > 1) { cout << "0\n"; return 0; } vector<int> &osc = scs[0]; set<int> oscs; for(unsigned int i = 0; i < osc.size(); ++i) oscs.insert(osc[i]); for(unsigned int i = 0; i < osc.size(); ++i) { int v = osc[i]; for(unsigned int j = 0; j < gr[v].size(); ++j) { int w = gr[v][j]; if(oscs.find(w) == oscs.end()) { gr[v][j] = gr[v].back(); gr[v].pop_back(); --j; } } } int stepsLeft = 2000000; for(int i = 0; stepsLeft > 0; ++i) { int v0 = osc[rand() % osc.size()]; int offset = rand(); stack<int> st; stack<int> ft; st.push(v0); ft.push(v0); on[v0] = true; while(not st.empty()) { int v = st.top(); st.pop(); --stepsLeft; bool broken = false; for(unsigned int j = 0; j < gr[v].size(); ++j) { int w = gr[v][(j + offset) % gr[v].size()]; if(w == v0) { for(set<int>::iterator it = oscs.begin(); it != oscs.end();) { if(not on[*it]) { oscs.erase(it++); } else { ++it; } } broken = true; break; } } if(broken) break; for(unsigned int j = 0; j < gr[v].size(); ++j) { int w = gr[v][(j + offset) % gr[v].size()]; if(not on[w]) { st.push(w); ft.push(w); on[w] = true; break; } } } while(not ft.empty()) { on[ft.top()] = false; ft.pop(); } } cout << oscs.size() << '\n'; for(set<int>::iterator it = oscs.begin(); it != oscs.end(); ++it) { cout << 1 + *it << ' '; } cout << '\n'; return 0; } |