#include <iostream> #include <set> #include <vector> #include <queue> using namespace std; #define DEBUG false #define log if (DEBUG) cerr typedef long long ll; typedef unsigned long long ull; struct City { int id; int group; set<int> neighbours; }; int n, m; unsigned d; vector<City> cities; void readData() { int i; cin >> n >> m >> d; cities.resize(n + 1); for (i = 0; i <= n; i++) { cities[i].id = i; cities[i].group = 0; } for (i = 0; i < m ; i++) { int a, b; cin >> a >> b; cities[a].neighbours.insert(b); cities[b].neighbours.insert(a); } } void stripNet() { set<int> candidates; set<int> newCandidates; for (const City& city : cities) { if (city.neighbours.size() < d) { candidates.insert(city.id); } } while (!candidates.empty()) { newCandidates.clear(); for (int sourceId : candidates) { for (int destId : cities[sourceId].neighbours) { City& destCity = cities[destId]; destCity.neighbours.erase(sourceId); if (destCity.neighbours.size() < d) { newCandidates.insert(destId); } } cities[sourceId].neighbours.clear(); } candidates = newCandidates; } if (DEBUG) { for (const City& city : cities) { if (!city.neighbours.empty()) { log << city.id << ", "; } } log << endl; } } void groupAndCount() { int currentGroup = 0; vector<set<int>> groupedIds; for (City& startCity : cities) { if (startCity.neighbours.size() < d || startCity.group != 0) { continue; } currentGroup ++; queue<int> toVisit; set<int> ids; startCity.group = currentGroup; toVisit.push(startCity.id); ids.insert(startCity.id); while (!toVisit.empty()) { int visitCityId = toVisit.front(); toVisit.pop(); for (int cityId : cities[visitCityId].neighbours) { City& city = cities[cityId]; if (city.group == 0) { city.group = currentGroup; toVisit.push(cityId); ids.insert(city.id); } } } groupedIds.push_back(ids); } unsigned maxCount = 0; int maxIdx; for (size_t i = 0; i < groupedIds.size(); i++) { if (groupedIds[i].size() > maxCount) { maxCount = groupedIds[i].size(); maxIdx = i; } } if (maxCount == 0) { cout << "NIE"; } else { cout << maxCount << endl; for (int id : groupedIds[maxIdx]) { cout << id << ' '; } } } int main() { readData(); stripNet(); groupAndCount(); 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | #include <iostream> #include <set> #include <vector> #include <queue> using namespace std; #define DEBUG false #define log if (DEBUG) cerr typedef long long ll; typedef unsigned long long ull; struct City { int id; int group; set<int> neighbours; }; int n, m; unsigned d; vector<City> cities; void readData() { int i; cin >> n >> m >> d; cities.resize(n + 1); for (i = 0; i <= n; i++) { cities[i].id = i; cities[i].group = 0; } for (i = 0; i < m ; i++) { int a, b; cin >> a >> b; cities[a].neighbours.insert(b); cities[b].neighbours.insert(a); } } void stripNet() { set<int> candidates; set<int> newCandidates; for (const City& city : cities) { if (city.neighbours.size() < d) { candidates.insert(city.id); } } while (!candidates.empty()) { newCandidates.clear(); for (int sourceId : candidates) { for (int destId : cities[sourceId].neighbours) { City& destCity = cities[destId]; destCity.neighbours.erase(sourceId); if (destCity.neighbours.size() < d) { newCandidates.insert(destId); } } cities[sourceId].neighbours.clear(); } candidates = newCandidates; } if (DEBUG) { for (const City& city : cities) { if (!city.neighbours.empty()) { log << city.id << ", "; } } log << endl; } } void groupAndCount() { int currentGroup = 0; vector<set<int>> groupedIds; for (City& startCity : cities) { if (startCity.neighbours.size() < d || startCity.group != 0) { continue; } currentGroup ++; queue<int> toVisit; set<int> ids; startCity.group = currentGroup; toVisit.push(startCity.id); ids.insert(startCity.id); while (!toVisit.empty()) { int visitCityId = toVisit.front(); toVisit.pop(); for (int cityId : cities[visitCityId].neighbours) { City& city = cities[cityId]; if (city.group == 0) { city.group = currentGroup; toVisit.push(cityId); ids.insert(city.id); } } } groupedIds.push_back(ids); } unsigned maxCount = 0; int maxIdx; for (size_t i = 0; i < groupedIds.size(); i++) { if (groupedIds[i].size() > maxCount) { maxCount = groupedIds[i].size(); maxIdx = i; } } if (maxCount == 0) { cout << "NIE"; } else { cout << maxCount << endl; for (int id : groupedIds[maxIdx]) { cout << id << ' '; } } } int main() { readData(); stripNet(); groupAndCount(); return 0; } |