#include<algorithm> #include<cstdio> #include<unordered_set> #include<list> #include<queue> using namespace std; unordered_set<int> T[200010]; int V[200010]; queue<int> Q; unordered_set<int> S; unordered_set<int> S2; int main() { int n,m,d,k,a,b; scanf("%d", &n); scanf("%d", &m); scanf("%d", &d); for(int i=0;i<m;i++) { scanf("%d", &a); scanf("%d", &b); T[a].insert(b); T[b].insert(a); } for(int i=1;i<=n;i++) { if(T[i].size() < d && T[i].size() > 0) { S.insert(i); } } bool changed = false; do { changed = false; for(int i : S) { for(int edge : T[i]) { if(T[edge].size() == d && T[edge].size() > 1) S2.insert(edge); T[edge].erase(i); } T[i].clear(); changed = true; } S = S2; S2.clear(); } while(changed); S2.clear(); int max_count = 0; int count = 0; int current = 0; int max_current = 0; for(int i=1;i<=n;i++) { if(V[i]) continue; current = i; count = 0; Q.push(i); while(!Q.empty()) { int x = Q.front(); Q.pop(); if(V[x]) continue; V[x] = current; count++; for(int edge : T[x]) { if(!V[edge]) { Q.push(edge); } } } if(count > max_count) { max_count = count; max_current = current; } } if(max_count > d) { printf("%d\n", max_count); for(int i=1;i<=n;i++) { if(V[i] == max_current) 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 | #include<algorithm> #include<cstdio> #include<unordered_set> #include<list> #include<queue> using namespace std; unordered_set<int> T[200010]; int V[200010]; queue<int> Q; unordered_set<int> S; unordered_set<int> S2; int main() { int n,m,d,k,a,b; scanf("%d", &n); scanf("%d", &m); scanf("%d", &d); for(int i=0;i<m;i++) { scanf("%d", &a); scanf("%d", &b); T[a].insert(b); T[b].insert(a); } for(int i=1;i<=n;i++) { if(T[i].size() < d && T[i].size() > 0) { S.insert(i); } } bool changed = false; do { changed = false; for(int i : S) { for(int edge : T[i]) { if(T[edge].size() == d && T[edge].size() > 1) S2.insert(edge); T[edge].erase(i); } T[i].clear(); changed = true; } S = S2; S2.clear(); } while(changed); S2.clear(); int max_count = 0; int count = 0; int current = 0; int max_current = 0; for(int i=1;i<=n;i++) { if(V[i]) continue; current = i; count = 0; Q.push(i); while(!Q.empty()) { int x = Q.front(); Q.pop(); if(V[x]) continue; V[x] = current; count++; for(int edge : T[x]) { if(!V[edge]) { Q.push(edge); } } } if(count > max_count) { max_count = count; max_current = current; } } if(max_count > d) { printf("%d\n", max_count); for(int i=1;i<=n;i++) { if(V[i] == max_current) printf("%d ", i); } printf("\n"); } else { printf("NIE\n"); } return 0; } |