#include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <vector> #include <queue> using namespace std; bool visited[1000000 + 2]; deque<int> D; vector<int> G[1000000 + 2], H[1000000 + 2]; vector<int> comp[1000000 + 2]; int only_strong_comp; bool in_strong_comp[1000000 + 2]; int postorder[1000000 + 2]; int postorder_id = 1; vector<int> base_cycle; bool push_cycle = false; int first_cycle_item = 0; void dfs1(int v) { if (visited[v]) { return; } visited[v] = true; for (size_t i = 0; i < G[v].size(); i++) { dfs1(G[v][i]); } D.push_front(v); } void dfs2(int v, int id) { if (visited[v]) { return; } visited[v] = true; comp[id].push_back(v); for (size_t i = 0; i < H[v].size(); i++) { dfs2(H[v][i], id); } } bool dfs3(int v) { visited[v] = true; for (size_t i = 0; i < G[v].size(); i++) { int w = G[v][i]; if (visited[w] && postorder[w] == 0) { push_cycle = true; first_cycle_item = w; base_cycle.push_back(v); return true; } if (in_strong_comp[w] && !visited[w]) { if (dfs3(w)) { if (push_cycle) { base_cycle.push_back(v); } if (first_cycle_item == v) { push_cycle = false; } return true; } } } postorder[v] = postorder_id++; return false; } bool dfs4(int v, int removed) { visited[v] = true; for (size_t i = 0; i < G[v].size(); i++) { int w = G[v][i]; if (w == removed) { continue; } if (visited[w] && postorder[w] == 0) { return false; } if (in_strong_comp[w] && !visited[w]) { if (!dfs4(w, removed)) { return false; } } } postorder[v] = postorder_id++; return true; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); G[a].push_back(b); H[b].push_back(a); } // Assign vertexes to strongly connected components. memset(visited, false, (n + 2) * sizeof(bool)); for (int i = 1; i <= n; i++) { dfs1(i); } memset(visited, false, (n + 2) * sizeof(bool)); int id = 0; for (int v : D) { if (!visited[v]) { dfs2(v, id); id++; } } // Check the number of components and only proceed, if there is exactly one larger than a single vertex. int max_comp_id = -1; int max_comp_size = -1; for (int i = 0; i < id; i++) { if (comp[i].size() > 1) { if (max_comp_size == -1) { max_comp_id = i; max_comp_size = comp[i].size(); } else { printf("0\n"); return 0; } } } if (max_comp_size == -1) { printf("NIE\n"); return 0; } only_strong_comp = max_comp_id; memset(in_strong_comp, false, (n + 2) * sizeof(bool)); for (size_t i = 0; i < comp[only_strong_comp].size(); i++) { in_strong_comp[comp[only_strong_comp][i]] = true; //printf("In strong component: %d\n", comp[only_strong_comp][i]); } // Find the base cycle in the component. memset(visited, false, (n + 2) * sizeof(bool)); memset(postorder, 0, (n + 2) * sizeof(int)); postorder_id = 1; dfs3(comp[only_strong_comp][0]); /*printf("@@@ base cycle size: %d\n", base_cycle.size()); for (size_t i = 0; i < base_cycle.size(); i++) { printf("@@@ %d\n", base_cycle[i]); }*/ // For each component of the cycle, check if it is part of the solution. vector<int> wyn; for (size_t i = 0; i < base_cycle.size(); i++) { memset(visited, false, (n + 2) * sizeof(bool)); memset(postorder, 0, (n + 2) * sizeof(int)); postorder_id = 1; bool success = true; for (size_t j = 0; j < comp[only_strong_comp].size(); j++) { if (base_cycle[i] == comp[only_strong_comp][j]) { continue; } if (visited[comp[only_strong_comp][j]]) { continue; } if (!dfs4(comp[only_strong_comp][j], base_cycle[i])) { success = false; break; } } if (success) { wyn.push_back(base_cycle[i]); } } sort(wyn.begin(), wyn.end()); printf("%d\n", wyn.size()); for (size_t i = 0; i < wyn.size(); i++) { printf("%d ", wyn[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 198 199 200 201 202 203 204 | #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <vector> #include <queue> using namespace std; bool visited[1000000 + 2]; deque<int> D; vector<int> G[1000000 + 2], H[1000000 + 2]; vector<int> comp[1000000 + 2]; int only_strong_comp; bool in_strong_comp[1000000 + 2]; int postorder[1000000 + 2]; int postorder_id = 1; vector<int> base_cycle; bool push_cycle = false; int first_cycle_item = 0; void dfs1(int v) { if (visited[v]) { return; } visited[v] = true; for (size_t i = 0; i < G[v].size(); i++) { dfs1(G[v][i]); } D.push_front(v); } void dfs2(int v, int id) { if (visited[v]) { return; } visited[v] = true; comp[id].push_back(v); for (size_t i = 0; i < H[v].size(); i++) { dfs2(H[v][i], id); } } bool dfs3(int v) { visited[v] = true; for (size_t i = 0; i < G[v].size(); i++) { int w = G[v][i]; if (visited[w] && postorder[w] == 0) { push_cycle = true; first_cycle_item = w; base_cycle.push_back(v); return true; } if (in_strong_comp[w] && !visited[w]) { if (dfs3(w)) { if (push_cycle) { base_cycle.push_back(v); } if (first_cycle_item == v) { push_cycle = false; } return true; } } } postorder[v] = postorder_id++; return false; } bool dfs4(int v, int removed) { visited[v] = true; for (size_t i = 0; i < G[v].size(); i++) { int w = G[v][i]; if (w == removed) { continue; } if (visited[w] && postorder[w] == 0) { return false; } if (in_strong_comp[w] && !visited[w]) { if (!dfs4(w, removed)) { return false; } } } postorder[v] = postorder_id++; return true; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); G[a].push_back(b); H[b].push_back(a); } // Assign vertexes to strongly connected components. memset(visited, false, (n + 2) * sizeof(bool)); for (int i = 1; i <= n; i++) { dfs1(i); } memset(visited, false, (n + 2) * sizeof(bool)); int id = 0; for (int v : D) { if (!visited[v]) { dfs2(v, id); id++; } } // Check the number of components and only proceed, if there is exactly one larger than a single vertex. int max_comp_id = -1; int max_comp_size = -1; for (int i = 0; i < id; i++) { if (comp[i].size() > 1) { if (max_comp_size == -1) { max_comp_id = i; max_comp_size = comp[i].size(); } else { printf("0\n"); return 0; } } } if (max_comp_size == -1) { printf("NIE\n"); return 0; } only_strong_comp = max_comp_id; memset(in_strong_comp, false, (n + 2) * sizeof(bool)); for (size_t i = 0; i < comp[only_strong_comp].size(); i++) { in_strong_comp[comp[only_strong_comp][i]] = true; //printf("In strong component: %d\n", comp[only_strong_comp][i]); } // Find the base cycle in the component. memset(visited, false, (n + 2) * sizeof(bool)); memset(postorder, 0, (n + 2) * sizeof(int)); postorder_id = 1; dfs3(comp[only_strong_comp][0]); /*printf("@@@ base cycle size: %d\n", base_cycle.size()); for (size_t i = 0; i < base_cycle.size(); i++) { printf("@@@ %d\n", base_cycle[i]); }*/ // For each component of the cycle, check if it is part of the solution. vector<int> wyn; for (size_t i = 0; i < base_cycle.size(); i++) { memset(visited, false, (n + 2) * sizeof(bool)); memset(postorder, 0, (n + 2) * sizeof(int)); postorder_id = 1; bool success = true; for (size_t j = 0; j < comp[only_strong_comp].size(); j++) { if (base_cycle[i] == comp[only_strong_comp][j]) { continue; } if (visited[comp[only_strong_comp][j]]) { continue; } if (!dfs4(comp[only_strong_comp][j], base_cycle[i])) { success = false; break; } } if (success) { wyn.push_back(base_cycle[i]); } } sort(wyn.begin(), wyn.end()); printf("%d\n", wyn.size()); for (size_t i = 0; i < wyn.size(); i++) { printf("%d ", wyn[i]); } return 0; } |