#include <bits/stdc++.h> using namespace std; int w[400100]; //wierzchołek int N[400100]; //next int V[400100]; //linki do drog int St[200100]; //stopnie poszczególnych miast int P[200100]; //parent int L[200100]; //liczba poszczególnych rodziców int lindex = 1; int n, m, d;//n- miasta, m- drogi void push(int a, int b) { w[lindex] = b; N[lindex] = V[a]; V[a] = lindex; ++lindex; ++St[a]; } int par(int a) { while(P[a] != a) a = P[a]; return a; } void uni(int a, int b) { P[par(a)] = P[a] = P[b] = par(b); } /*void piszrodzicow() { for(int i = 1; i <= n; ++i) { printf("%d: ", i); if(St[i] == -1) printf("-1"); else printf("%d", par(i)); printf("\n"); } }*/ int main() { scanf("%d%d%d", &n, &m, &d); int a,b; for(int i = 0; i < m; ++i) { scanf("%d%d", &a, &b); push(a,b); push(b,a); } queue<int> Q; for(int i = 1; i <= n; ++i) { if(St[i] < d) { St[i] = -1; Q.push(i); } } //puts("C1"); int T; while(!Q.empty()) { T = Q.front(); Q.pop(); int i = V[T]; while(i != 0) { if(St[w[i]] != -1) --St[w[i]]; if(St[w[i]] < d && St[w[i]] != -1) { St[w[i]] = -1; Q.push(w[i]); } i = N[i]; } } //puts("C2"); for(int i = 1; i <= n; ++i) P[i] = i; for(int i = 1; i <= n; ++i) { if(St[i] >= d) { int j = V[i]; while(j != 0) { if(St[w[j]] >= d) uni(i, w[j]); j = N[j]; } } } for(int i = 1; i <= n; ++i) if(St[i] >= d) ++L[P[i] = par(i)]; int max = 0; int maxn = 0; for(int i = 1; i <= n; ++i) { if(L[i] > max) { max = L[i]; maxn = i; } } if(max == 0) puts("NIE"); else { printf("%d\n", max); for(int i = 1; i <= n; ++i) { if(St[i] != -1 && P[i] == maxn) printf("%d\n", i); } } return 0; } /* void write(int a) { int i = V[a]; while(i != 0) { printf("%d ", w[i]); i = N[i]; } } void wall() { for(int i = 0; i < n; ++i) { printf("%d: ", i); write(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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | #include <bits/stdc++.h> using namespace std; int w[400100]; //wierzchołek int N[400100]; //next int V[400100]; //linki do drog int St[200100]; //stopnie poszczególnych miast int P[200100]; //parent int L[200100]; //liczba poszczególnych rodziców int lindex = 1; int n, m, d;//n- miasta, m- drogi void push(int a, int b) { w[lindex] = b; N[lindex] = V[a]; V[a] = lindex; ++lindex; ++St[a]; } int par(int a) { while(P[a] != a) a = P[a]; return a; } void uni(int a, int b) { P[par(a)] = P[a] = P[b] = par(b); } /*void piszrodzicow() { for(int i = 1; i <= n; ++i) { printf("%d: ", i); if(St[i] == -1) printf("-1"); else printf("%d", par(i)); printf("\n"); } }*/ int main() { scanf("%d%d%d", &n, &m, &d); int a,b; for(int i = 0; i < m; ++i) { scanf("%d%d", &a, &b); push(a,b); push(b,a); } queue<int> Q; for(int i = 1; i <= n; ++i) { if(St[i] < d) { St[i] = -1; Q.push(i); } } //puts("C1"); int T; while(!Q.empty()) { T = Q.front(); Q.pop(); int i = V[T]; while(i != 0) { if(St[w[i]] != -1) --St[w[i]]; if(St[w[i]] < d && St[w[i]] != -1) { St[w[i]] = -1; Q.push(w[i]); } i = N[i]; } } //puts("C2"); for(int i = 1; i <= n; ++i) P[i] = i; for(int i = 1; i <= n; ++i) { if(St[i] >= d) { int j = V[i]; while(j != 0) { if(St[w[j]] >= d) uni(i, w[j]); j = N[j]; } } } for(int i = 1; i <= n; ++i) if(St[i] >= d) ++L[P[i] = par(i)]; int max = 0; int maxn = 0; for(int i = 1; i <= n; ++i) { if(L[i] > max) { max = L[i]; maxn = i; } } if(max == 0) puts("NIE"); else { printf("%d\n", max); for(int i = 1; i <= n; ++i) { if(St[i] != -1 && P[i] == maxn) printf("%d\n", i); } } return 0; } /* void write(int a) { int i = V[a]; while(i != 0) { printf("%d ", w[i]); i = N[i]; } } void wall() { for(int i = 0; i < n; ++i) { printf("%d: ", i); write(i); printf("\n"); } }*/ |