#include <cstdio> #include <deque> #include <list> #include <set> using namespace std; #undef DEBUG #define FOR(E, I) for(auto E = I.begin(); E != I.end(); E++) const int MAX_N = 200000; const int MAX_M = 200000; struct City; typedef struct City { int printableIndex; bool visited; set<City*> connections; }; City city[MAX_N]; bool sortListOfIntegerDesc(const list<int>& first, const list<int>& second) { return first.size() > second.size(); } int main() { int n, m, d; scanf("%d%d%d", &n, &m, &d); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); city[a - 1].connections.insert(city + b - 1); city[b - 1].connections.insert(city + a - 1); } set<City*> candidates; deque<City*> q; for (int i = 0; i < n; i++) { city[i].printableIndex = i + 1; candidates.insert(candidates.end(), city + i); q.push_back(city + i); } while (!q.empty()) { City* candidate = q.front(); if (candidate->connections.size() < d) { FOR(c, candidate->connections) { (*c)->connections.erase(candidate); if ((*c)->connections.size() < d) { q.push_back(*c); } } candidate->connections.clear(); candidates.erase(candidate); } q.pop_front(); } #ifdef DEBUG printf("Candidate filtering completed, cities left: %d\n", candidates.size()); #endif list<list<int>> groups; FOR(i, candidates) { City* firstCity = *i; if (firstCity->visited) break; firstCity->visited = true; q.push_back(firstCity); list<int> group; while(!q.empty()) { City* city = q.front(); FOR(c, city->connections) { if (!(*c)->visited) { (*c)->visited = true; q.push_back(*c); } } group.push_back(city->printableIndex); q.pop_front(); } groups.push_back(group); #ifdef DEBUG printf("Group #%d : %d cities\n", groups.size(), group.size()); #endif } groups.sort(sortListOfIntegerDesc); if (!groups.empty()) { list<int> largestGroup = groups.front(); largestGroup.sort(); printf("%d\n", largestGroup.size()); FOR(c, largestGroup) { printf("%d ", *c); } printf("\n"); } else { puts("NIE"); } 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | #include <cstdio> #include <deque> #include <list> #include <set> using namespace std; #undef DEBUG #define FOR(E, I) for(auto E = I.begin(); E != I.end(); E++) const int MAX_N = 200000; const int MAX_M = 200000; struct City; typedef struct City { int printableIndex; bool visited; set<City*> connections; }; City city[MAX_N]; bool sortListOfIntegerDesc(const list<int>& first, const list<int>& second) { return first.size() > second.size(); } int main() { int n, m, d; scanf("%d%d%d", &n, &m, &d); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); city[a - 1].connections.insert(city + b - 1); city[b - 1].connections.insert(city + a - 1); } set<City*> candidates; deque<City*> q; for (int i = 0; i < n; i++) { city[i].printableIndex = i + 1; candidates.insert(candidates.end(), city + i); q.push_back(city + i); } while (!q.empty()) { City* candidate = q.front(); if (candidate->connections.size() < d) { FOR(c, candidate->connections) { (*c)->connections.erase(candidate); if ((*c)->connections.size() < d) { q.push_back(*c); } } candidate->connections.clear(); candidates.erase(candidate); } q.pop_front(); } #ifdef DEBUG printf("Candidate filtering completed, cities left: %d\n", candidates.size()); #endif list<list<int>> groups; FOR(i, candidates) { City* firstCity = *i; if (firstCity->visited) break; firstCity->visited = true; q.push_back(firstCity); list<int> group; while(!q.empty()) { City* city = q.front(); FOR(c, city->connections) { if (!(*c)->visited) { (*c)->visited = true; q.push_back(*c); } } group.push_back(city->printableIndex); q.pop_front(); } groups.push_back(group); #ifdef DEBUG printf("Group #%d : %d cities\n", groups.size(), group.size()); #endif } groups.sort(sortListOfIntegerDesc); if (!groups.empty()) { list<int> largestGroup = groups.front(); largestGroup.sort(); printf("%d\n", largestGroup.size()); FOR(c, largestGroup) { printf("%d ", *c); } printf("\n"); } else { puts("NIE"); } return 0; } |