#include <cstdio> #include <vector> #define scanf(args...) (scanf(args) ? : 0) const int MAXN = 500005; std::vector<int> adj[MAXN]; bool visited[MAXN]; int value[MAXN]; int edge_count[MAXN]; int upper_edge_count; void dfs(int u) { if (visited[u]) return; visited[u] = true; for (int v: adj[u]) if (!visited[v]) { dfs(v); value[u] += value[v] - edge_count[v]; } else { value[u]++; edge_count[v]++; upper_edge_count++; } } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); u--, v--; adj[u].push_back(v); } for (int i = 0; i < n; i++) dfs(i); std::vector<int> result; for (int i = 0; i < n; i++) { if (value[i] == upper_edge_count) result.push_back(i); } if (upper_edge_count == 0) printf("NIE\n"); else { printf("%d\n", (int)result.size()); for (int i = 0; i < result.size(); i++) printf("%d ", result[i] + 1); 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 | #include <cstdio> #include <vector> #define scanf(args...) (scanf(args) ? : 0) const int MAXN = 500005; std::vector<int> adj[MAXN]; bool visited[MAXN]; int value[MAXN]; int edge_count[MAXN]; int upper_edge_count; void dfs(int u) { if (visited[u]) return; visited[u] = true; for (int v: adj[u]) if (!visited[v]) { dfs(v); value[u] += value[v] - edge_count[v]; } else { value[u]++; edge_count[v]++; upper_edge_count++; } } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); u--, v--; adj[u].push_back(v); } for (int i = 0; i < n; i++) dfs(i); std::vector<int> result; for (int i = 0; i < n; i++) { if (value[i] == upper_edge_count) result.push_back(i); } if (upper_edge_count == 0) printf("NIE\n"); else { printf("%d\n", (int)result.size()); for (int i = 0; i < result.size(); i++) printf("%d ", result[i] + 1); printf("\n"); } } |