#include <bits/stdc++.h> constexpr size_t MAX_N = 200000 + 44; std::unordered_set<int> connections[MAX_N]; int component[MAX_N]; 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); connections[a].insert(b); connections[b].insert(a); } std::unordered_set<int> browseQueue; for (int i = 1; i <= n; ++i) { browseQueue.insert(i); } while (browseQueue.size()) { const auto it = browseQueue.begin(); int city = *it; browseQueue.erase(it); if (connections[city].size() < d) { for (const int neigh : connections[city]) { connections[neigh].erase(city); browseQueue.insert(neigh); } connections[city].clear(); } } //scc int bestComponent = 0; int bestComponentSize = 0; for (int i = 1; i <= n; ++i) { if (component[i] != 0) continue; std::unordered_set<int> visited; std::unordered_set<int> toVisit; toVisit.insert(i); while (toVisit.size()) { const auto it = toVisit.begin(); int city = *it; toVisit.erase(it); component[city] = i; visited.insert(city); for (const int neigh : connections[city]) { if (!visited.count(neigh)) toVisit.insert(neigh); } } if (visited.size() > bestComponentSize || bestComponentSize == 0) { bestComponent = i; bestComponentSize = visited.size(); } } if (bestComponentSize <= 1) { printf("NIE\n"); } else { printf("%d\n", bestComponentSize); for (int i = 1; i<=n; ++i) { if (component[i] == bestComponent) printf("%d ", 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 | #include <bits/stdc++.h> constexpr size_t MAX_N = 200000 + 44; std::unordered_set<int> connections[MAX_N]; int component[MAX_N]; 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); connections[a].insert(b); connections[b].insert(a); } std::unordered_set<int> browseQueue; for (int i = 1; i <= n; ++i) { browseQueue.insert(i); } while (browseQueue.size()) { const auto it = browseQueue.begin(); int city = *it; browseQueue.erase(it); if (connections[city].size() < d) { for (const int neigh : connections[city]) { connections[neigh].erase(city); browseQueue.insert(neigh); } connections[city].clear(); } } //scc int bestComponent = 0; int bestComponentSize = 0; for (int i = 1; i <= n; ++i) { if (component[i] != 0) continue; std::unordered_set<int> visited; std::unordered_set<int> toVisit; toVisit.insert(i); while (toVisit.size()) { const auto it = toVisit.begin(); int city = *it; toVisit.erase(it); component[city] = i; visited.insert(city); for (const int neigh : connections[city]) { if (!visited.count(neigh)) toVisit.insert(neigh); } } if (visited.size() > bestComponentSize || bestComponentSize == 0) { bestComponent = i; bestComponentSize = visited.size(); } } if (bestComponentSize <= 1) { printf("NIE\n"); } else { printf("%d\n", bestComponentSize); for (int i = 1; i<=n; ++i) { if (component[i] == bestComponent) printf("%d ", i); } printf("\n"); } } |