#include <cstdio> #include <vector> #include <set> using namespace std; int main() { int n,m,d; scanf("%d%d%d",&n,&m,&d); vector<vector<int>> g(n); set<int> d_cities; for (int i = 0; i < m; ++i) { int a, b; scanf("%d%d", &a, &b); g[a-1].push_back(b-1); if (g[a-1].size() == d) { d_cities.insert(a-1); } g[b-1].push_back(a-1); if (g[b-1].size() == d) { d_cities.insert(b - 1); } } vector<int> neigh_count(n); int d_n = 0; for (int c : d_cities) { for(int n : g[c]) { if (++neigh_count[n] == d) { d_n++; } } } vector<int> decr_d; decr_d.reserve(d_cities.size() - d_n); for (int c : d_cities) { if (neigh_count[c] < d) { decr_d.push_back(c); } } while (!decr_d.empty()) { vector<int> new_decr; for (int c : decr_d) { d_cities.erase(c); for (int nc : g[c]) { if (--neigh_count[nc] == d-1) { new_decr.push_back(nc); } } } decr_d.swap(new_decr); } set<int> visited; set<int> best_set; for (int c : d_cities) { if (visited.count(c)) { continue; } set<int> curr_set, fringe; fringe.insert(c); while (!fringe.empty()) { int cc = *fringe.begin(); fringe.erase(fringe.begin()); curr_set.insert(cc); for (int nc : g[cc]) { visited.insert(nc); if (curr_set.count(nc) == 0 && d_cities.count(nc)) { fringe.insert(nc); } } } if (curr_set.size() > best_set.size()) { best_set = curr_set; } } if (best_set.empty()) { printf("NIE\n"); } else { printf("%lu\n", best_set.size()); for (int c : best_set) { printf("%d ", c+1); } 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 | #include <cstdio> #include <vector> #include <set> using namespace std; int main() { int n,m,d; scanf("%d%d%d",&n,&m,&d); vector<vector<int>> g(n); set<int> d_cities; for (int i = 0; i < m; ++i) { int a, b; scanf("%d%d", &a, &b); g[a-1].push_back(b-1); if (g[a-1].size() == d) { d_cities.insert(a-1); } g[b-1].push_back(a-1); if (g[b-1].size() == d) { d_cities.insert(b - 1); } } vector<int> neigh_count(n); int d_n = 0; for (int c : d_cities) { for(int n : g[c]) { if (++neigh_count[n] == d) { d_n++; } } } vector<int> decr_d; decr_d.reserve(d_cities.size() - d_n); for (int c : d_cities) { if (neigh_count[c] < d) { decr_d.push_back(c); } } while (!decr_d.empty()) { vector<int> new_decr; for (int c : decr_d) { d_cities.erase(c); for (int nc : g[c]) { if (--neigh_count[nc] == d-1) { new_decr.push_back(nc); } } } decr_d.swap(new_decr); } set<int> visited; set<int> best_set; for (int c : d_cities) { if (visited.count(c)) { continue; } set<int> curr_set, fringe; fringe.insert(c); while (!fringe.empty()) { int cc = *fringe.begin(); fringe.erase(fringe.begin()); curr_set.insert(cc); for (int nc : g[cc]) { visited.insert(nc); if (curr_set.count(nc) == 0 && d_cities.count(nc)) { fringe.insert(nc); } } } if (curr_set.size() > best_set.size()) { best_set = curr_set; } } if (best_set.empty()) { printf("NIE\n"); } else { printf("%lu\n", best_set.size()); for (int c : best_set) { printf("%d ", c+1); } printf("\n"); } } |