#include<cstdio> #include<vector> #include<algorithm> #include<queue> using namespace std; const int MAXN = 2e5+5; int n, m, d; int outs[MAXN]; bool bylo[MAXN]; vector<int> v[MAXN], kochamtomka, temp; void dfs_spoj(int x) { bylo[x] = 1; temp.push_back(x); for (int j = 0; j < v[x].size(); j++) { if(!bylo[v[x][j]] && outs[v[x][j]] >= d) dfs_spoj(v[x][j]); } } bool dodany[MAXN]; queue <int> q; int main() { int a, b; scanf("%d %d %d", &n, &m, &d); for (int i = 0; i < m; i++) { scanf ("%d %d", &a, &b); v[a].push_back(b); v[b].push_back(a); } for (int i = 1; i <= n; i++){ outs[i] = v[i].size(); if (outs[i] < d) { q.push(i); dodany[i] = 1; } } int p; while(!q.empty()) { p = q.front(); q.pop(); for (int j = 0; j < v[p].size(); j++) { outs[v[p][j]]--; if (!dodany[v[p][j]] && outs[v[p][j]] < d){ dodany[v[p][j]] = 1; q.push(v[p][j]); } } } for (int i = 1; i <=n; i++) { if (bylo[i] < 2 && outs[i] >= d){ temp.clear(); dfs_spoj(i); if (temp.size() > kochamtomka.size()) { swap(temp, kochamtomka); } } } if (kochamtomka.size() == 0) { printf("NIE\n"); return 0; } sort(kochamtomka.begin(), kochamtomka.end()); printf("%lu\n", kochamtomka.size()); for (int j = 0; j < kochamtomka.size(); j++) printf("%d ", kochamtomka[j]); printf("\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 | #include<cstdio> #include<vector> #include<algorithm> #include<queue> using namespace std; const int MAXN = 2e5+5; int n, m, d; int outs[MAXN]; bool bylo[MAXN]; vector<int> v[MAXN], kochamtomka, temp; void dfs_spoj(int x) { bylo[x] = 1; temp.push_back(x); for (int j = 0; j < v[x].size(); j++) { if(!bylo[v[x][j]] && outs[v[x][j]] >= d) dfs_spoj(v[x][j]); } } bool dodany[MAXN]; queue <int> q; int main() { int a, b; scanf("%d %d %d", &n, &m, &d); for (int i = 0; i < m; i++) { scanf ("%d %d", &a, &b); v[a].push_back(b); v[b].push_back(a); } for (int i = 1; i <= n; i++){ outs[i] = v[i].size(); if (outs[i] < d) { q.push(i); dodany[i] = 1; } } int p; while(!q.empty()) { p = q.front(); q.pop(); for (int j = 0; j < v[p].size(); j++) { outs[v[p][j]]--; if (!dodany[v[p][j]] && outs[v[p][j]] < d){ dodany[v[p][j]] = 1; q.push(v[p][j]); } } } for (int i = 1; i <=n; i++) { if (bylo[i] < 2 && outs[i] >= d){ temp.clear(); dfs_spoj(i); if (temp.size() > kochamtomka.size()) { swap(temp, kochamtomka); } } } if (kochamtomka.size() == 0) { printf("NIE\n"); return 0; } sort(kochamtomka.begin(), kochamtomka.end()); printf("%lu\n", kochamtomka.size()); for (int j = 0; j < kochamtomka.size(); j++) printf("%d ", kochamtomka[j]); printf("\n"); return 0; } |