#include <cstdio> #include <vector> #include <queue> using namespace std; const int MAXN=500010; int visited[MAXN], removed[MAXN]; vector<int> v[MAXN]; queue<int> rem; int deg[MAXN]; int DFS(int i, int boss) { visited[i] = boss; int sum = 1; for (vector<int>::iterator it = v[i].begin(); it != v[i].end(); ++it) { if (visited[*it] || removed[*it]) { continue; } sum += DFS(*it, boss); } return sum; } int main() { int n, m, d; scanf("%d%d%d", &n, &m, &d); while (m--) { int a, b; scanf("%d%d", &a, &b); v[a].push_back(b); v[b].push_back(a); deg[a]++; deg[b]++; } for (int i = 1; i <= n; ++i) { if (deg[i] < d) { rem.push(i); } } while (!rem.empty()) { int i = rem.front(); removed[i] = true; rem.pop(); for (vector<int>::iterator it = v[i].begin(); it != v[i].end(); ++it) { deg[*it]--; if (deg[*it] == d-1) { rem.push(*it); } } } int max_size = 0, i_max_size; for (int i = 1; i <= n; ++i) { if (!removed[i] && !visited[i]) { int size = DFS(i, i); if (size > max_size) { max_size = size; i_max_size = i; } } } if (max_size == 0) { printf("NIE\n"); return 0; } printf("%d\n", max_size); for (int i = 1; i <= n; ++i) { if (visited[i] == i_max_size) { 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 | #include <cstdio> #include <vector> #include <queue> using namespace std; const int MAXN=500010; int visited[MAXN], removed[MAXN]; vector<int> v[MAXN]; queue<int> rem; int deg[MAXN]; int DFS(int i, int boss) { visited[i] = boss; int sum = 1; for (vector<int>::iterator it = v[i].begin(); it != v[i].end(); ++it) { if (visited[*it] || removed[*it]) { continue; } sum += DFS(*it, boss); } return sum; } int main() { int n, m, d; scanf("%d%d%d", &n, &m, &d); while (m--) { int a, b; scanf("%d%d", &a, &b); v[a].push_back(b); v[b].push_back(a); deg[a]++; deg[b]++; } for (int i = 1; i <= n; ++i) { if (deg[i] < d) { rem.push(i); } } while (!rem.empty()) { int i = rem.front(); removed[i] = true; rem.pop(); for (vector<int>::iterator it = v[i].begin(); it != v[i].end(); ++it) { deg[*it]--; if (deg[*it] == d-1) { rem.push(*it); } } } int max_size = 0, i_max_size; for (int i = 1; i <= n; ++i) { if (!removed[i] && !visited[i]) { int size = DFS(i, i); if (size > max_size) { max_size = size; i_max_size = i; } } } if (max_size == 0) { printf("NIE\n"); return 0; } printf("%d\n", max_size); for (int i = 1; i <= n; ++i) { if (visited[i] == i_max_size) { printf("%d ", i); } } printf("\n"); } |