//============================================================================ // Name : mis.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <cstdint> #include <unordered_set> using namespace std; int n, m, d; // miasta, drogi, d struct { int a,b; } roads[200005]; int num_roads[200005]; int connections[400005]; int city_con_start_index[200005]; int city_con_end_index[200005]; int city_done[200005]; unordered_set<int> wywalic; int main() { cin >> n >> m >> d; int i, a, b; for (i = 0; i < m; i++) { cin >> a >> b; a--; b--; roads[i].a = a; roads[i].b = b; num_roads[a] ++; num_roads[b] ++; } for (i = 0; i < n; i++) { city_con_start_index[i+1] = city_con_end_index[i+1] = city_con_end_index[i] + num_roads[i]; } for (i = 0; i < m; i++) { connections[city_con_end_index[roads[i].a]] = roads[i].b; city_con_end_index[roads[i].a]++; connections[city_con_end_index[roads[i].b]] = roads[i].a; city_con_end_index[roads[i].b]++; } for (i = 0; i < n; i++) { if (num_roads[i] < d) wywalic.insert(i); } while (!wywalic.empty()) { auto it = wywalic.begin(); int wi = *it; wywalic.erase(it); city_done[wi] = -1; for (i = city_con_start_index[wi]; i < city_con_end_index[wi]; i++) { num_roads[connections[i]]--; if ((num_roads[connections[i]] < d) && (city_done[connections[i]] == 0)) { wywalic.insert(connections[i]); } } } int skladowa = 0; int max_size = 0; int max_skladowa = -1; int size = 0; int j; for (j = 0; j < n; j++) { if (city_done[j] != 0) continue; skladowa++; size = 0; wywalic.insert(j); city_done[j] = skladowa; size ++; while(!wywalic.empty()) { auto it = wywalic.begin(); int wi = *it; wywalic.erase(it); for (i = city_con_start_index[wi]; i < city_con_end_index[wi]; i++) { if (city_done[connections[i]] == 0) { wywalic.insert(connections[i]); city_done[connections[i]] = skladowa; size ++; } } } if (size > max_size) { max_size = size; max_skladowa = skladowa; } } if (max_skladowa == -1) { cout << "NIE" << endl; return 0; } cout << max_size << endl; for (i = 0; i < n; i++) { if (city_done[i] == max_skladowa) cout << i+1 << ' '; } cout << endl; 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 | //============================================================================ // Name : mis.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <cstdint> #include <unordered_set> using namespace std; int n, m, d; // miasta, drogi, d struct { int a,b; } roads[200005]; int num_roads[200005]; int connections[400005]; int city_con_start_index[200005]; int city_con_end_index[200005]; int city_done[200005]; unordered_set<int> wywalic; int main() { cin >> n >> m >> d; int i, a, b; for (i = 0; i < m; i++) { cin >> a >> b; a--; b--; roads[i].a = a; roads[i].b = b; num_roads[a] ++; num_roads[b] ++; } for (i = 0; i < n; i++) { city_con_start_index[i+1] = city_con_end_index[i+1] = city_con_end_index[i] + num_roads[i]; } for (i = 0; i < m; i++) { connections[city_con_end_index[roads[i].a]] = roads[i].b; city_con_end_index[roads[i].a]++; connections[city_con_end_index[roads[i].b]] = roads[i].a; city_con_end_index[roads[i].b]++; } for (i = 0; i < n; i++) { if (num_roads[i] < d) wywalic.insert(i); } while (!wywalic.empty()) { auto it = wywalic.begin(); int wi = *it; wywalic.erase(it); city_done[wi] = -1; for (i = city_con_start_index[wi]; i < city_con_end_index[wi]; i++) { num_roads[connections[i]]--; if ((num_roads[connections[i]] < d) && (city_done[connections[i]] == 0)) { wywalic.insert(connections[i]); } } } int skladowa = 0; int max_size = 0; int max_skladowa = -1; int size = 0; int j; for (j = 0; j < n; j++) { if (city_done[j] != 0) continue; skladowa++; size = 0; wywalic.insert(j); city_done[j] = skladowa; size ++; while(!wywalic.empty()) { auto it = wywalic.begin(); int wi = *it; wywalic.erase(it); for (i = city_con_start_index[wi]; i < city_con_end_index[wi]; i++) { if (city_done[connections[i]] == 0) { wywalic.insert(connections[i]); city_done[connections[i]] = skladowa; size ++; } } } if (size > max_size) { max_size = size; max_skladowa = skladowa; } } if (max_skladowa == -1) { cout << "NIE" << endl; return 0; } cout << max_size << endl; for (i = 0; i < n; i++) { if (city_done[i] == max_skladowa) cout << i+1 << ' '; } cout << endl; return 0; } |