#include <algorithm> #include <iostream> #include <iterator> #include <vector> #include <array> using std::begin; using std::end; constexpr uint32_t out_of_value = 2000001; class node { public: static uint32_t visit_part; static uint32_t numerator; uint32_t visit_numerator = 0; uint32_t my_end = out_of_value; std::vector<size_t> neightbours, reversed; uint32_t dfs2(const uint32_t); public: //bool in_cycle = false; int32_t number = -2; static void next_visit_part(); inline bool visited(); void connect(const size_t); void post_order(); void dfs1(); node* find_cycle(); void clear(); void find_end(); bool other_cycle(); } *graph; uint32_t node::visit_part = 0; uint32_t node::numerator = 0; std::vector<std::vector<size_t>> scc; std::vector<size_t> cycle; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); uint32_t n, m; std::cin >> n >> m; graph = new node[n]; for(uint32_t a, b, i = 0; i < m; ++i) { std::cin >> a >> b; graph[a-1].connect(b-1); } node::next_visit_part(); for(size_t i = 0; i < n; ++i) { if(graph[i].visited() == false) { graph[i].post_order(); } } size_t* const sorted = new size_t[n]; for(size_t i = 0; i < n; ++i) { sorted[graph[i].number] = i; } std::reverse(sorted, sorted+n); node::next_visit_part(); for(size_t i = 0; i < n; ++i) { scc.emplace_back(); if(graph[sorted[i]].visited() == false) { graph[sorted[i]].dfs1(); } if(scc.back().size() <= 1) { scc.pop_back(); } } if(scc.empty() == true) { std::cout << "NIE\n"; return 0; } if(scc.size() > 1) { std::cout << 0 << '\n' << '\n'; return 0; } for(const auto& x : scc.back()) { graph[x].number = -3; } for(const auto& x : scc.back()) { graph[x].clear(); } node::next_visit_part(); graph[scc.back().front()].find_cycle(); std::reverse(begin(cycle), end(cycle)); for(const auto x : scc.back()) { if(graph[x].other_cycle() == true) { std::cout << 0 << '\n'; } } node::next_visit_part(); for(uint32_t i = 0; i < cycle.size(); ++i) { graph[cycle[i]].number = i; } for(size_t i = 0; i < cycle.size(); ++i) { graph[cycle[i]].find_end(); } uint32_t temp_end = out_of_value; if(graph[cycle.front()].my_end != out_of_value) { temp_end = graph[cycle.front()].my_end; } bool correct = true; std::vector<bool> odpowiedzi; for(size_t i = 0; i < cycle.size(); ++i) { if(temp_end != out_of_value && static_cast<int32_t>(temp_end) == graph[cycle[i]].number) { temp_end = out_of_value; correct = true; } odpowiedzi.push_back(correct); if(graph[i].my_end != out_of_value) { if(temp_end == out_of_value) { temp_end = graph[i].my_end; } else if (temp_end <= i && graph[i].my_end <= i) { temp_end = std::max(temp_end, graph[i].my_end); } else if(temp_end > i && graph[i].my_end <= i) { temp_end = graph[i].my_end; } else { temp_end = std::max(temp_end, graph[i].my_end); } } if(temp_end != out_of_value) { correct = false; } else correct = true; } std::vector<uint32_t> answers; if(temp_end < out_of_value) { for(size_t i = 0; i < odpowiedzi.size() && cycle[i] != temp_end; ++i) { odpowiedzi[i] = false; } } for(size_t i = 0; i < odpowiedzi.size(); ++i) { if(odpowiedzi[i] == true) { answers.push_back(cycle[i]+1); } } std::sort(begin(answers), end(answers)); std::cout << answers.size() << '\n'; std::copy(begin(answers), end(answers), std::ostream_iterator<uint32_t>(std::cout, " ")); std::cout << '\n'; } void node::connect(const size_t i) { neightbours.push_back(i); graph[i].reversed.push_back(this-graph); } void node::post_order() { visit_numerator = visit_part; for(const auto& x : neightbours) { if(graph[x].visited() == false) { graph[x].post_order(); } } number = numerator; numerator += 1; } inline bool node::visited() { return visit_numerator == visit_part; } void node::next_visit_part() { visit_part += 1; } void node::dfs1() { visit_numerator = visit_part; for(const auto& x : reversed) { if(graph[x].visited() == false) { graph[x].dfs1(); } } scc.back().push_back(this-graph); } std::array<uint32_t, 2> out_of_value_element = {{out_of_value, out_of_value}}; uint32_t my_max(const uint32_t a, const uint32_t b, const uint32_t i) { if(a == out_of_value || b == out_of_value) { return std::min(a, b); } if(a <= i && b <= i) { return std::max(a,b); } if(a <= i || b <= i) { return std::min(a, b); } return std::max(a,b); } uint32_t node::dfs2(const uint32_t first) { if(number >= 0) { return number; } uint32_t temp = out_of_value; visit_numerator = visit_part; for(const auto& x : neightbours) { if(graph[x].visit_numerator != visit_part) { temp = my_max(graph[x].dfs2(first), temp, first); } } return temp; } void node::find_end() { next_visit_part(); for(const auto& x : neightbours) { if(graph[x].number < 0 || graph[x].number != (number+1)%static_cast<int32_t>(cycle.size())) { my_end = my_max(graph[x].dfs2(number), my_end, number); } } } node* node::find_cycle() { if(number < -3) { cycle.push_back(this - graph); return this; } number = -4; for(const auto& x : neightbours) { if(graph[x].number <= -3) { const auto temp = graph[x].find_cycle(); if(temp == this || temp == nullptr) { return nullptr; } cycle.push_back(this - graph); return temp; } } return nullptr; } void node::clear() { auto it = std::remove_if(begin(neightbours), end(neightbours), [](const size_t i){return graph[i].number > -3;}); neightbours.resize(std::distance(begin(neightbours), it)); } bool node::other_cycle() { if(visited() == true) { return true; } for(const auto& x : neightbours) { if(graph[x].number == -3) { if(graph[x].other_cycle() == true) { return true; } } } return false; }
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 | #include <algorithm> #include <iostream> #include <iterator> #include <vector> #include <array> using std::begin; using std::end; constexpr uint32_t out_of_value = 2000001; class node { public: static uint32_t visit_part; static uint32_t numerator; uint32_t visit_numerator = 0; uint32_t my_end = out_of_value; std::vector<size_t> neightbours, reversed; uint32_t dfs2(const uint32_t); public: //bool in_cycle = false; int32_t number = -2; static void next_visit_part(); inline bool visited(); void connect(const size_t); void post_order(); void dfs1(); node* find_cycle(); void clear(); void find_end(); bool other_cycle(); } *graph; uint32_t node::visit_part = 0; uint32_t node::numerator = 0; std::vector<std::vector<size_t>> scc; std::vector<size_t> cycle; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); uint32_t n, m; std::cin >> n >> m; graph = new node[n]; for(uint32_t a, b, i = 0; i < m; ++i) { std::cin >> a >> b; graph[a-1].connect(b-1); } node::next_visit_part(); for(size_t i = 0; i < n; ++i) { if(graph[i].visited() == false) { graph[i].post_order(); } } size_t* const sorted = new size_t[n]; for(size_t i = 0; i < n; ++i) { sorted[graph[i].number] = i; } std::reverse(sorted, sorted+n); node::next_visit_part(); for(size_t i = 0; i < n; ++i) { scc.emplace_back(); if(graph[sorted[i]].visited() == false) { graph[sorted[i]].dfs1(); } if(scc.back().size() <= 1) { scc.pop_back(); } } if(scc.empty() == true) { std::cout << "NIE\n"; return 0; } if(scc.size() > 1) { std::cout << 0 << '\n' << '\n'; return 0; } for(const auto& x : scc.back()) { graph[x].number = -3; } for(const auto& x : scc.back()) { graph[x].clear(); } node::next_visit_part(); graph[scc.back().front()].find_cycle(); std::reverse(begin(cycle), end(cycle)); for(const auto x : scc.back()) { if(graph[x].other_cycle() == true) { std::cout << 0 << '\n'; } } node::next_visit_part(); for(uint32_t i = 0; i < cycle.size(); ++i) { graph[cycle[i]].number = i; } for(size_t i = 0; i < cycle.size(); ++i) { graph[cycle[i]].find_end(); } uint32_t temp_end = out_of_value; if(graph[cycle.front()].my_end != out_of_value) { temp_end = graph[cycle.front()].my_end; } bool correct = true; std::vector<bool> odpowiedzi; for(size_t i = 0; i < cycle.size(); ++i) { if(temp_end != out_of_value && static_cast<int32_t>(temp_end) == graph[cycle[i]].number) { temp_end = out_of_value; correct = true; } odpowiedzi.push_back(correct); if(graph[i].my_end != out_of_value) { if(temp_end == out_of_value) { temp_end = graph[i].my_end; } else if (temp_end <= i && graph[i].my_end <= i) { temp_end = std::max(temp_end, graph[i].my_end); } else if(temp_end > i && graph[i].my_end <= i) { temp_end = graph[i].my_end; } else { temp_end = std::max(temp_end, graph[i].my_end); } } if(temp_end != out_of_value) { correct = false; } else correct = true; } std::vector<uint32_t> answers; if(temp_end < out_of_value) { for(size_t i = 0; i < odpowiedzi.size() && cycle[i] != temp_end; ++i) { odpowiedzi[i] = false; } } for(size_t i = 0; i < odpowiedzi.size(); ++i) { if(odpowiedzi[i] == true) { answers.push_back(cycle[i]+1); } } std::sort(begin(answers), end(answers)); std::cout << answers.size() << '\n'; std::copy(begin(answers), end(answers), std::ostream_iterator<uint32_t>(std::cout, " ")); std::cout << '\n'; } void node::connect(const size_t i) { neightbours.push_back(i); graph[i].reversed.push_back(this-graph); } void node::post_order() { visit_numerator = visit_part; for(const auto& x : neightbours) { if(graph[x].visited() == false) { graph[x].post_order(); } } number = numerator; numerator += 1; } inline bool node::visited() { return visit_numerator == visit_part; } void node::next_visit_part() { visit_part += 1; } void node::dfs1() { visit_numerator = visit_part; for(const auto& x : reversed) { if(graph[x].visited() == false) { graph[x].dfs1(); } } scc.back().push_back(this-graph); } std::array<uint32_t, 2> out_of_value_element = {{out_of_value, out_of_value}}; uint32_t my_max(const uint32_t a, const uint32_t b, const uint32_t i) { if(a == out_of_value || b == out_of_value) { return std::min(a, b); } if(a <= i && b <= i) { return std::max(a,b); } if(a <= i || b <= i) { return std::min(a, b); } return std::max(a,b); } uint32_t node::dfs2(const uint32_t first) { if(number >= 0) { return number; } uint32_t temp = out_of_value; visit_numerator = visit_part; for(const auto& x : neightbours) { if(graph[x].visit_numerator != visit_part) { temp = my_max(graph[x].dfs2(first), temp, first); } } return temp; } void node::find_end() { next_visit_part(); for(const auto& x : neightbours) { if(graph[x].number < 0 || graph[x].number != (number+1)%static_cast<int32_t>(cycle.size())) { my_end = my_max(graph[x].dfs2(number), my_end, number); } } } node* node::find_cycle() { if(number < -3) { cycle.push_back(this - graph); return this; } number = -4; for(const auto& x : neightbours) { if(graph[x].number <= -3) { const auto temp = graph[x].find_cycle(); if(temp == this || temp == nullptr) { return nullptr; } cycle.push_back(this - graph); return temp; } } return nullptr; } void node::clear() { auto it = std::remove_if(begin(neightbours), end(neightbours), [](const size_t i){return graph[i].number > -3;}); neightbours.resize(std::distance(begin(neightbours), it)); } bool node::other_cycle() { if(visited() == true) { return true; } for(const auto& x : neightbours) { if(graph[x].number == -3) { if(graph[x].other_cycle() == true) { return true; } } } return false; } |