#include <cstdio> #include <set> const int MAXN = 200010; std::set< int > v[MAXN]; int parent[MAXN]; int sizeOfSet[MAXN]; int getParent(int i) { if(parent[i] < 0) { return i; } else { parent[i] = getParent(parent[i]); return parent[i]; } } int mergeSets(int a, int b) { //printf("merging %d and %d\n", a, b); int pa = getParent(a); int pb = getParent(b); //printf("\tpa, pb: %d, %d\n", pa, pb); if(pa != pb) { if(-parent[pa] < -parent[pb]) { parent[pa] = pb; sizeOfSet[pb] += sizeOfSet[pa]; return sizeOfSet[pb]; } else { if(-parent[pa] == -parent[pb]) { parent[pa]--; } parent[pb] = pa; //printf("\tparent[%d]: %d\n", pb, parent[pb]); sizeOfSet[pa] += sizeOfSet[pb]; return sizeOfSet[pa]; } } else { return sizeOfSet[pa]; } } int main() { int n, m, d; scanf("%d%d%d", &n, &m, &d); for(int e = 0; e < m; e++) { int a, b; scanf("%d%d", &a, &b); v[a].insert(b); v[b].insert(a); } //printf("Creating toRemove.\n"); fflush(stdout); std::set< int > toRemove; for(int i = 1; i <= n; i++) { parent[i] = -1; sizeOfSet[i] = 1; if(v[i].size() < d) { toRemove.insert(i); //printf("toRemove.insert(%d)\n", i); fflush(stdout); } } //printf("Created toRemove.\n\n"); fflush(stdout); std::set< int > removed; while(!toRemove.empty()) { int i = *toRemove.begin(); toRemove.erase(i); removed.insert(i); //printf("Removing vertex %d\n", i); fflush(stdout); for(std::set< int >::iterator it = v[i].begin(); it != v[i].end(); it++) { if(removed.find(*it) == removed.end()) { v[*it].erase(i); //printf("Removing edge %d-%d\n", *it, i); fflush(stdout); if(v[*it].size() < d) { toRemove.insert(*it); //printf("toRemove.insert(%d)\n", *it); fflush(stdout); } } } } int largestSetSize = 0; int largestSetInd = -1; for(int i = 1; i <= n; i++) { if(removed.find(i) == removed.end()) { for(std::set< int >::iterator it = v[i].begin(); it != v[i].end(); it++) { int sizeOfCurrSet = mergeSets(i, *it); if(sizeOfCurrSet > largestSetSize) { largestSetSize = sizeOfCurrSet; //printf("largestSetSize = %d\n", largestSetSize); largestSetInd = getParent(i); } } } } if(largestSetSize == 0) { printf("NIE\n"); } else { printf("%d\n", largestSetSize); for(int i = 1; i <=n; i++) { if(getParent(i) == largestSetInd) { printf("%d ", i); } } 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 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 | #include <cstdio> #include <set> const int MAXN = 200010; std::set< int > v[MAXN]; int parent[MAXN]; int sizeOfSet[MAXN]; int getParent(int i) { if(parent[i] < 0) { return i; } else { parent[i] = getParent(parent[i]); return parent[i]; } } int mergeSets(int a, int b) { //printf("merging %d and %d\n", a, b); int pa = getParent(a); int pb = getParent(b); //printf("\tpa, pb: %d, %d\n", pa, pb); if(pa != pb) { if(-parent[pa] < -parent[pb]) { parent[pa] = pb; sizeOfSet[pb] += sizeOfSet[pa]; return sizeOfSet[pb]; } else { if(-parent[pa] == -parent[pb]) { parent[pa]--; } parent[pb] = pa; //printf("\tparent[%d]: %d\n", pb, parent[pb]); sizeOfSet[pa] += sizeOfSet[pb]; return sizeOfSet[pa]; } } else { return sizeOfSet[pa]; } } int main() { int n, m, d; scanf("%d%d%d", &n, &m, &d); for(int e = 0; e < m; e++) { int a, b; scanf("%d%d", &a, &b); v[a].insert(b); v[b].insert(a); } //printf("Creating toRemove.\n"); fflush(stdout); std::set< int > toRemove; for(int i = 1; i <= n; i++) { parent[i] = -1; sizeOfSet[i] = 1; if(v[i].size() < d) { toRemove.insert(i); //printf("toRemove.insert(%d)\n", i); fflush(stdout); } } //printf("Created toRemove.\n\n"); fflush(stdout); std::set< int > removed; while(!toRemove.empty()) { int i = *toRemove.begin(); toRemove.erase(i); removed.insert(i); //printf("Removing vertex %d\n", i); fflush(stdout); for(std::set< int >::iterator it = v[i].begin(); it != v[i].end(); it++) { if(removed.find(*it) == removed.end()) { v[*it].erase(i); //printf("Removing edge %d-%d\n", *it, i); fflush(stdout); if(v[*it].size() < d) { toRemove.insert(*it); //printf("toRemove.insert(%d)\n", *it); fflush(stdout); } } } } int largestSetSize = 0; int largestSetInd = -1; for(int i = 1; i <= n; i++) { if(removed.find(i) == removed.end()) { for(std::set< int >::iterator it = v[i].begin(); it != v[i].end(); it++) { int sizeOfCurrSet = mergeSets(i, *it); if(sizeOfCurrSet > largestSetSize) { largestSetSize = sizeOfCurrSet; //printf("largestSetSize = %d\n", largestSetSize); largestSetInd = getParent(i); } } } } if(largestSetSize == 0) { printf("NIE\n"); } else { printf("%d\n", largestSetSize); for(int i = 1; i <=n; i++) { if(getParent(i) == largestSetInd) { printf("%d ", i); } } printf("\n"); } return 0; } |