#include <bits/stdc++.h> std::vector<std::vector<int>> adj; std::vector<int> deg; std::vector<int> rep; std::vector<int> dep; int d; void dfs(int n) { for (int i = 0; i < (int)adj[n].size(); ++i) { int v = adj[n][i]; if (deg[v] >= d) { deg[v] -= 1; if (deg[v] < d) { dfs(v); } } } } int find(int n) { if (rep[n] == n) return n; rep[n] = find(rep[n]); return rep[n]; } void uni(int a, int b) { if (find(a) == find(b)) return; if (dep[find(a)] < dep[find(b)]) rep[find(a)] = find(b); else if (dep[find(a)] > dep[find(b)]) rep[find(b)] = find(a); else { dep[find(a)] += 1; rep[find(b)] = find(a); } } std::vector<bool> vis; void unidfs(int n) { vis[n] = true; for (int i = 0; i < (int)adj[n].size(); ++i) { int v = adj[n][i]; if (deg[v] >= d && !vis[v]) { uni(n, v); unidfs(v); } } } void count() { for (int i = 0; i < (int)adj.size(); ++i) { if (deg[i] >= d) dep[find(i)] += 1; } } void report() { int id = -1; int c = 0; for (int i = 0; i < (int)dep.size(); ++i) { if (dep[i] > c) { id = i; c = dep[i]; } } if (id >= 0) { printf("%d\n", c); for (int i = 0; i < (int)dep.size(); ++i) { if (find(i) == id) printf("%d ", i + 1); } printf("\n"); } else printf("NIE\n"); } int main() { int n, m; scanf("%d %d %d", &n, &m, &d); adj.resize(n, std::vector<int>()); rep.resize(n, 0); dep.resize(n, 0); vis.resize(n, false); deg.resize(n, 0); for (int i = 0; i < n; ++i) { rep[i] = i; } for (int i = 0; i < m; ++i) { int a, b; scanf("%d %d", &a, &b); adj[a - 1].push_back(b - 1); adj[b - 1].push_back(a - 1); } for (int i = 0; i < n; ++i) { deg[i] = (int)adj[i].size(); } for (int i = 0; i < n; ++i) { if (deg[i] < d) { dfs(i); } } for (int i = 0; i < n; ++i) { if (deg[i] >= d) { unidfs(i); } } dep.clear(); dep.resize(n, 0); count(); report(); }
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 | #include <bits/stdc++.h> std::vector<std::vector<int>> adj; std::vector<int> deg; std::vector<int> rep; std::vector<int> dep; int d; void dfs(int n) { for (int i = 0; i < (int)adj[n].size(); ++i) { int v = adj[n][i]; if (deg[v] >= d) { deg[v] -= 1; if (deg[v] < d) { dfs(v); } } } } int find(int n) { if (rep[n] == n) return n; rep[n] = find(rep[n]); return rep[n]; } void uni(int a, int b) { if (find(a) == find(b)) return; if (dep[find(a)] < dep[find(b)]) rep[find(a)] = find(b); else if (dep[find(a)] > dep[find(b)]) rep[find(b)] = find(a); else { dep[find(a)] += 1; rep[find(b)] = find(a); } } std::vector<bool> vis; void unidfs(int n) { vis[n] = true; for (int i = 0; i < (int)adj[n].size(); ++i) { int v = adj[n][i]; if (deg[v] >= d && !vis[v]) { uni(n, v); unidfs(v); } } } void count() { for (int i = 0; i < (int)adj.size(); ++i) { if (deg[i] >= d) dep[find(i)] += 1; } } void report() { int id = -1; int c = 0; for (int i = 0; i < (int)dep.size(); ++i) { if (dep[i] > c) { id = i; c = dep[i]; } } if (id >= 0) { printf("%d\n", c); for (int i = 0; i < (int)dep.size(); ++i) { if (find(i) == id) printf("%d ", i + 1); } printf("\n"); } else printf("NIE\n"); } int main() { int n, m; scanf("%d %d %d", &n, &m, &d); adj.resize(n, std::vector<int>()); rep.resize(n, 0); dep.resize(n, 0); vis.resize(n, false); deg.resize(n, 0); for (int i = 0; i < n; ++i) { rep[i] = i; } for (int i = 0; i < m; ++i) { int a, b; scanf("%d %d", &a, &b); adj[a - 1].push_back(b - 1); adj[b - 1].push_back(a - 1); } for (int i = 0; i < n; ++i) { deg[i] = (int)adj[i].size(); } for (int i = 0; i < n; ++i) { if (deg[i] < d) { dfs(i); } } for (int i = 0; i < n; ++i) { if (deg[i] >= d) { unidfs(i); } } dep.clear(); dep.resize(n, 0); count(); report(); } |