#include <cstdio> #include <vector> using namespace std; int N, M, D, a, b, f, k, l, o, p; vector<int> v, s, c; vector<vector<int>> e; int main() { scanf("%d %d %d", &N, &M, &D); v.resize(N + 1); e.resize(N + 1); c.resize(N + 1); f = N; for (int i = 0; i < M; i++) { scanf("%d %d", &a, &b); v[a]++; e[a].push_back(b); v[b]++; e[b].push_back(a); } for (int i = 1; i <= N; i++) { if (v[i] != -1 && v[i] < D) { v[i] = -1; f--; s.insert(s.end(), e[i].begin(), e[i].end()); while (!s.empty()) { int a = s.back(); s.pop_back(); if (v[a] != -1) { v[a]--; if (v[a] < D) { v[a] = -1; f--; s.insert(s.end(), e[a].begin(), e[a].end()); } } } } } if (f > 0) { for (int i = 1; i <= N; i++) { if (v[i] != -1 && c[i] == 0) { k++; c[i] = k; l = 1; s.insert(s.end(), e[i].begin(), e[i].end()); while (!s.empty()) { int a = s.back(); s.pop_back(); if (v[a] != -1 && c[a] == 0) { c[a] = k; l++; s.insert(s.end(), e[a].begin(), e[a].end()); } } if (l > o) { o = l; p = k; } } } } if (o > 0) { printf("%d\n", o); for (int i = 1; i <= N; i++) { if (c[i] == p) { printf("%d ", i); } } printf("\n"); } else { printf("NIE\n"); } 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 | #include <cstdio> #include <vector> using namespace std; int N, M, D, a, b, f, k, l, o, p; vector<int> v, s, c; vector<vector<int>> e; int main() { scanf("%d %d %d", &N, &M, &D); v.resize(N + 1); e.resize(N + 1); c.resize(N + 1); f = N; for (int i = 0; i < M; i++) { scanf("%d %d", &a, &b); v[a]++; e[a].push_back(b); v[b]++; e[b].push_back(a); } for (int i = 1; i <= N; i++) { if (v[i] != -1 && v[i] < D) { v[i] = -1; f--; s.insert(s.end(), e[i].begin(), e[i].end()); while (!s.empty()) { int a = s.back(); s.pop_back(); if (v[a] != -1) { v[a]--; if (v[a] < D) { v[a] = -1; f--; s.insert(s.end(), e[a].begin(), e[a].end()); } } } } } if (f > 0) { for (int i = 1; i <= N; i++) { if (v[i] != -1 && c[i] == 0) { k++; c[i] = k; l = 1; s.insert(s.end(), e[i].begin(), e[i].end()); while (!s.empty()) { int a = s.back(); s.pop_back(); if (v[a] != -1 && c[a] == 0) { c[a] = k; l++; s.insert(s.end(), e[a].begin(), e[a].end()); } } if (l > o) { o = l; p = k; } } } } if (o > 0) { printf("%d\n", o); for (int i = 1; i <= N; i++) { if (c[i] == p) { printf("%d ", i); } } printf("\n"); } else { printf("NIE\n"); } return 0; } |