#include <cstdio> #include <vector> #include <queue> const int MAX_N = 590009; const int MAX_M = 1000009; int n, m; std::vector<int> adj[MAX_N]; std::vector<int> radj[MAX_N]; std::vector<int> s; bool is_needed[MAX_N]; bool v[MAX_N]; void dfs(int a) { v[a] = true; for (int b : adj[a]) { if (!v[b]) { dfs(b); } } s.push_back(a); } int t; int scc_no; int scc[MAX_N]; std::pair<int, int> d[MAX_N]; std::deque<int> lower; std::deque<int> higher; std::deque<int> needed; int path_no; bool first_cycle; void rdfs(int a) { d[a].first = ++t; scc[a] = scc_no; if (first_cycle) higher.push_back(a); for (int b : radj[a]) { if (scc[b] && scc[b] != scc_no) continue; if (!d[b].first) { // tree edge //fprintf(stderr, "tree\t%d -> %d\n", a, b); rdfs(b); } else if (!d[b].second) { // backward edge //fprintf(stderr, "back\t%d -> %d\n", a, b); first_cycle = false; while (!higher.empty() && d[higher.front()].first < d[b].first) { higher.pop_front(); } lower.clear(); /*fprintf(stderr, "higher:"); for (int b : higher) { fprintf(stderr, " %d", b); } fprintf(stderr, "\n");*/ } else { // forward or cross edge //fprintf(stderr, "forw\t%d -> %d\n", a, b); while (!lower.empty() && d[lower.front()].second > d[b].second) { lower.pop_front(); } } } /*fprintf(stderr, "higher@%d:", a); for (int b : higher) { fprintf(stderr, " %d", b); } fprintf(stderr, "\n");*/ if (!higher.empty() && higher.back() == a) { lower.push_front(a); higher.pop_back(); } d[a].second = ++t; } bool rdfs_start(int a) { if (!d[a].first) { lower.clear(); higher.clear(); path_no = 0; first_cycle = true; scc[a] = ++scc_no; d[a].first = t = 1; higher.push_back(a); /*fprintf(stderr, "Radj[%d]:", a); for (int c : radj[a]) { fprintf(stderr, " %d", c); } fprintf(stderr, "\n");*/ for (int b : radj[a]) { if (!d[b].first) { ++path_no; //fprintf(stderr, "Starting a path %d @ %d\n", b, a); rdfs(b); } else if (scc[b] == scc_no) { while (!lower.empty() && d[lower.front()].second > d[b].second) { lower.pop_front(); } } } if (!higher.empty() && higher.back() == a) { lower.push_front(a); higher.pop_back(); } if (path_no) { needed.swap(lower); } //fprintf(stderr, "Got %d paths @ %d\n", path_no, a); return path_no; } return false; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; ++i) { int a, b; scanf("%d%d", &a, &b); adj[a].push_back(b); radj[b].push_back(a); } for (int i = 1; i <= n; ++i) { if (!v[i]) { dfs(i); } } bool cycle = false; while (!s.empty()) { int a = s.back(); //fprintf(stderr, "Trying to start from %d\n", a); if (rdfs_start(a)) { if (cycle) { printf("0\n\n"); return 0; } else { cycle = true; } } s.pop_back(); } if (!cycle) { printf("NIE\n"); return 0; } printf("%d\n", (int)needed.size()); for (int a : needed) { is_needed[a] = true; } for (int i = 1; i <= n; ++i) { if (is_needed[i]) { printf("%d", i); for (++i; i <= n; ++i) { if (is_needed[i]) { printf(" %d", i); } } } } printf("\n"); }
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 | #include <cstdio> #include <vector> #include <queue> const int MAX_N = 590009; const int MAX_M = 1000009; int n, m; std::vector<int> adj[MAX_N]; std::vector<int> radj[MAX_N]; std::vector<int> s; bool is_needed[MAX_N]; bool v[MAX_N]; void dfs(int a) { v[a] = true; for (int b : adj[a]) { if (!v[b]) { dfs(b); } } s.push_back(a); } int t; int scc_no; int scc[MAX_N]; std::pair<int, int> d[MAX_N]; std::deque<int> lower; std::deque<int> higher; std::deque<int> needed; int path_no; bool first_cycle; void rdfs(int a) { d[a].first = ++t; scc[a] = scc_no; if (first_cycle) higher.push_back(a); for (int b : radj[a]) { if (scc[b] && scc[b] != scc_no) continue; if (!d[b].first) { // tree edge //fprintf(stderr, "tree\t%d -> %d\n", a, b); rdfs(b); } else if (!d[b].second) { // backward edge //fprintf(stderr, "back\t%d -> %d\n", a, b); first_cycle = false; while (!higher.empty() && d[higher.front()].first < d[b].first) { higher.pop_front(); } lower.clear(); /*fprintf(stderr, "higher:"); for (int b : higher) { fprintf(stderr, " %d", b); } fprintf(stderr, "\n");*/ } else { // forward or cross edge //fprintf(stderr, "forw\t%d -> %d\n", a, b); while (!lower.empty() && d[lower.front()].second > d[b].second) { lower.pop_front(); } } } /*fprintf(stderr, "higher@%d:", a); for (int b : higher) { fprintf(stderr, " %d", b); } fprintf(stderr, "\n");*/ if (!higher.empty() && higher.back() == a) { lower.push_front(a); higher.pop_back(); } d[a].second = ++t; } bool rdfs_start(int a) { if (!d[a].first) { lower.clear(); higher.clear(); path_no = 0; first_cycle = true; scc[a] = ++scc_no; d[a].first = t = 1; higher.push_back(a); /*fprintf(stderr, "Radj[%d]:", a); for (int c : radj[a]) { fprintf(stderr, " %d", c); } fprintf(stderr, "\n");*/ for (int b : radj[a]) { if (!d[b].first) { ++path_no; //fprintf(stderr, "Starting a path %d @ %d\n", b, a); rdfs(b); } else if (scc[b] == scc_no) { while (!lower.empty() && d[lower.front()].second > d[b].second) { lower.pop_front(); } } } if (!higher.empty() && higher.back() == a) { lower.push_front(a); higher.pop_back(); } if (path_no) { needed.swap(lower); } //fprintf(stderr, "Got %d paths @ %d\n", path_no, a); return path_no; } return false; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; ++i) { int a, b; scanf("%d%d", &a, &b); adj[a].push_back(b); radj[b].push_back(a); } for (int i = 1; i <= n; ++i) { if (!v[i]) { dfs(i); } } bool cycle = false; while (!s.empty()) { int a = s.back(); //fprintf(stderr, "Trying to start from %d\n", a); if (rdfs_start(a)) { if (cycle) { printf("0\n\n"); return 0; } else { cycle = true; } } s.pop_back(); } if (!cycle) { printf("NIE\n"); return 0; } printf("%d\n", (int)needed.size()); for (int a : needed) { is_needed[a] = true; } for (int i = 1; i <= n; ++i) { if (is_needed[i]) { printf("%d", i); for (++i; i <= n; ++i) { if (is_needed[i]) { printf(" %d", i); } } } } printf("\n"); } |