#include<cstdio> #include<vector> #include<queue> using namespace std; int main(){ int n, m, d, a, b; scanf("%d %d %d", &n, &m, &d); bool out[n + 1]; int p[n + 1]; vector<int> t[n + 1]; queue<int> q; for(int i = 0; i < m; i++){ scanf("%d %d", &a, &b); t[a].push_back(b); t[b].push_back(a); } for(int i = 1; i <= n; i++){ p[i] = t[i].size(); if(p[i] < d){ out[i] = true; q.push(i); } else out[i] = false; } while(!q.empty()){ a = q.front(); q.pop(); for(int i = 0; i < t[a].size(); i++){ p[t[a][i]]--; if(!out[t[a][i]] && p[t[a][i]] < d){ out[t[a][i]] = true; q.push(t[a][i]); } } } int max = 0, max_nr = 0, nr[n + 1]; for(int i = 1; i <= n; i++) nr[i] = 0; for(int i = 1; i <= n; i++){ if(!out[i]){ q.push(i); out[i] = true; nr[i] = i; b = 1; while(!q.empty()){ a = q.front(); q.pop(); for(int j = 0; j < t[a].size(); j++){ if(!out[t[a][j]]){ out[t[a][j]] = true; nr[t[a][j]] = i; b++; q.push(t[a][j]); } } } if(b > max){ max = b; max_nr = i; } } } if(max == 0) printf("NIE"); else{ printf("%d\n", max); for(int i = 1; i <= n; i++) if(nr[i] == max_nr) printf("%d ", i); } 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<queue> using namespace std; int main(){ int n, m, d, a, b; scanf("%d %d %d", &n, &m, &d); bool out[n + 1]; int p[n + 1]; vector<int> t[n + 1]; queue<int> q; for(int i = 0; i < m; i++){ scanf("%d %d", &a, &b); t[a].push_back(b); t[b].push_back(a); } for(int i = 1; i <= n; i++){ p[i] = t[i].size(); if(p[i] < d){ out[i] = true; q.push(i); } else out[i] = false; } while(!q.empty()){ a = q.front(); q.pop(); for(int i = 0; i < t[a].size(); i++){ p[t[a][i]]--; if(!out[t[a][i]] && p[t[a][i]] < d){ out[t[a][i]] = true; q.push(t[a][i]); } } } int max = 0, max_nr = 0, nr[n + 1]; for(int i = 1; i <= n; i++) nr[i] = 0; for(int i = 1; i <= n; i++){ if(!out[i]){ q.push(i); out[i] = true; nr[i] = i; b = 1; while(!q.empty()){ a = q.front(); q.pop(); for(int j = 0; j < t[a].size(); j++){ if(!out[t[a][j]]){ out[t[a][j]] = true; nr[t[a][j]] = i; b++; q.push(t[a][j]); } } } if(b > max){ max = b; max_nr = i; } } } if(max == 0) printf("NIE"); else{ printf("%d\n", max); for(int i = 1; i <= n; i++) if(nr[i] == max_nr) printf("%d ", i); } return 0; } |