/* Using algorithm described by R. Tarjan, Enumeration of the elementary circuits of a directed graph, SIAM Journal on Computing, 2 (1973), pp. 211-216 Implementation based on: - https://github.com/josch/cycles_tarjan/blob/master/cycles.py */ #include <cstdio> #include <algorithm> #include <functional> #include <vector> using namespace std; const int MAX_N = 500000; const int MAX_M = 1000000; vector<int> adj[MAX_N]; vector<int> adj_tr[MAX_N]; vector<int> adj_scc[MAX_N]; bool visited[MAX_N]; vector<int> f_order; int scc[MAX_N]; int scc_cnt[MAX_N]; int relabel_to[MAX_N]; int relabel_from[MAX_N]; int cycle_cnt[MAX_N]; int num_cycles; int s; vector<int> point_stack; bool marked[MAX_N]; vector<int> marked_stack; void dfs_visit(int u) { visited[u] = true; for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (!visited[v]) { dfs_visit(v); } } f_order.push_back(u); } void dfs_visit_tr(int u, int curr_scc) { scc[u] = curr_scc; visited[u] = true; for (int i = 0; i < adj_tr[u].size(); i++) { int v = adj_tr[u][i]; if (!visited[v]) { dfs_visit_tr(v, curr_scc); } } } void dfs(int n) { for (int u = 0; u < n; u++) { visited[u] = false; } for (int u = 0; u < n; u++) { if (!visited[u]) { dfs_visit(u); } } } void dfs_tr(int n) { for (int u = 0; u < n; u++) { visited[u] = false; } int curr_scc = 0; for (int i = n - 1; i >= 0; i--) { int u = f_order[i]; if (!visited[u]) { dfs_visit_tr(u, curr_scc++); } } } void strongly_connected_components(int n) { dfs(n); dfs_tr(n); } bool backtrack(int v) { bool f = false; point_stack.push_back(v); marked[v] = true; marked_stack.push_back(v); for (int i = 0; i < adj_scc[v].size(); i++) { int w = adj_scc[v][i]; if (w != -1) { if (w < s) { adj_scc[v][i] = -1; } else if (w == s) { //printf("Cycle: "); num_cycles++; for (int j = 0; j < point_stack.size(); j++) { //printf("%d ", point_stack[j]); cycle_cnt[point_stack[j]]++; } //printf("\n"); f = true; } else if (!marked[w]) { f = ((backtrack(w)) || (f)); } } } if (f) { while (marked_stack.back() != v) { int u = marked_stack.back(); marked_stack.pop_back(); marked[u] = false; } marked_stack.pop_back(); marked[v] = false; } point_stack.pop_back(); return f; } int main() { int n, m, a, b; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d%d", &a, &b); adj[a - 1].push_back(b - 1); adj_tr[b - 1].push_back(a - 1); } strongly_connected_components(n); for (int i = 0; i < n; i++) { scc_cnt[i] = 0; } for (int i = 0; i < n; i++) { scc_cnt[scc[i]]++; } int num_scc = 0; int check_scc = -1; for (int i = 0; i < n; i++) { if (scc_cnt[i] > 1) { num_scc++; check_scc = i; } } if (num_scc == 0) { printf("NIE\n"); } else if (num_scc > 1) { printf("0\n\n"); } else { int N = scc_cnt[check_scc]; int relabel = 0; for (int u = 0; u < n; u++) { if (scc[u] == check_scc) { relabel_to[u] = relabel; relabel_from[relabel] = u; relabel++; } } for (int ru = 0; ru < N; ru++) { int u = relabel_from[ru]; for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (scc[v] == check_scc) { int rv = relabel_to[v]; adj_scc[ru].push_back(rv); } } } num_cycles = 0; for (int ru = 0; ru < N; ru++) { cycle_cnt[ru] = 0; } for (int ru = 0; ru < N; ru++) { marked[ru] = false; } for (s = 0; s < N; s++) { backtrack(s); while (!marked_stack.empty()) { int u = marked_stack.back(); marked_stack.pop_back(); marked[u] = false; } } int res = 0; for (int ru = 0; ru < N; ru++) { if (cycle_cnt[ru] == num_cycles) { res++; } } if (res == 0) { printf("0\n\n"); } else { printf("%d\n", res); int i = 0; for (int ru = 0; ru < N; ru++) { if (cycle_cnt[ru] == num_cycles) { if (i < res - 1) { printf("%d ", relabel_from[ru] + 1); } else { printf("%d\n", relabel_from[ru] + 1); } 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | /* Using algorithm described by R. Tarjan, Enumeration of the elementary circuits of a directed graph, SIAM Journal on Computing, 2 (1973), pp. 211-216 Implementation based on: - https://github.com/josch/cycles_tarjan/blob/master/cycles.py */ #include <cstdio> #include <algorithm> #include <functional> #include <vector> using namespace std; const int MAX_N = 500000; const int MAX_M = 1000000; vector<int> adj[MAX_N]; vector<int> adj_tr[MAX_N]; vector<int> adj_scc[MAX_N]; bool visited[MAX_N]; vector<int> f_order; int scc[MAX_N]; int scc_cnt[MAX_N]; int relabel_to[MAX_N]; int relabel_from[MAX_N]; int cycle_cnt[MAX_N]; int num_cycles; int s; vector<int> point_stack; bool marked[MAX_N]; vector<int> marked_stack; void dfs_visit(int u) { visited[u] = true; for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (!visited[v]) { dfs_visit(v); } } f_order.push_back(u); } void dfs_visit_tr(int u, int curr_scc) { scc[u] = curr_scc; visited[u] = true; for (int i = 0; i < adj_tr[u].size(); i++) { int v = adj_tr[u][i]; if (!visited[v]) { dfs_visit_tr(v, curr_scc); } } } void dfs(int n) { for (int u = 0; u < n; u++) { visited[u] = false; } for (int u = 0; u < n; u++) { if (!visited[u]) { dfs_visit(u); } } } void dfs_tr(int n) { for (int u = 0; u < n; u++) { visited[u] = false; } int curr_scc = 0; for (int i = n - 1; i >= 0; i--) { int u = f_order[i]; if (!visited[u]) { dfs_visit_tr(u, curr_scc++); } } } void strongly_connected_components(int n) { dfs(n); dfs_tr(n); } bool backtrack(int v) { bool f = false; point_stack.push_back(v); marked[v] = true; marked_stack.push_back(v); for (int i = 0; i < adj_scc[v].size(); i++) { int w = adj_scc[v][i]; if (w != -1) { if (w < s) { adj_scc[v][i] = -1; } else if (w == s) { //printf("Cycle: "); num_cycles++; for (int j = 0; j < point_stack.size(); j++) { //printf("%d ", point_stack[j]); cycle_cnt[point_stack[j]]++; } //printf("\n"); f = true; } else if (!marked[w]) { f = ((backtrack(w)) || (f)); } } } if (f) { while (marked_stack.back() != v) { int u = marked_stack.back(); marked_stack.pop_back(); marked[u] = false; } marked_stack.pop_back(); marked[v] = false; } point_stack.pop_back(); return f; } int main() { int n, m, a, b; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d%d", &a, &b); adj[a - 1].push_back(b - 1); adj_tr[b - 1].push_back(a - 1); } strongly_connected_components(n); for (int i = 0; i < n; i++) { scc_cnt[i] = 0; } for (int i = 0; i < n; i++) { scc_cnt[scc[i]]++; } int num_scc = 0; int check_scc = -1; for (int i = 0; i < n; i++) { if (scc_cnt[i] > 1) { num_scc++; check_scc = i; } } if (num_scc == 0) { printf("NIE\n"); } else if (num_scc > 1) { printf("0\n\n"); } else { int N = scc_cnt[check_scc]; int relabel = 0; for (int u = 0; u < n; u++) { if (scc[u] == check_scc) { relabel_to[u] = relabel; relabel_from[relabel] = u; relabel++; } } for (int ru = 0; ru < N; ru++) { int u = relabel_from[ru]; for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (scc[v] == check_scc) { int rv = relabel_to[v]; adj_scc[ru].push_back(rv); } } } num_cycles = 0; for (int ru = 0; ru < N; ru++) { cycle_cnt[ru] = 0; } for (int ru = 0; ru < N; ru++) { marked[ru] = false; } for (s = 0; s < N; s++) { backtrack(s); while (!marked_stack.empty()) { int u = marked_stack.back(); marked_stack.pop_back(); marked[u] = false; } } int res = 0; for (int ru = 0; ru < N; ru++) { if (cycle_cnt[ru] == num_cycles) { res++; } } if (res == 0) { printf("0\n\n"); } else { printf("%d\n", res); int i = 0; for (int ru = 0; ru < N; ru++) { if (cycle_cnt[ru] == num_cycles) { if (i < res - 1) { printf("%d ", relabel_from[ru] + 1); } else { printf("%d\n", relabel_from[ru] + 1); } i++; } } } } return 0; } |