#include <iostream> #include <cstdlib> #include <algorithm> #include <vector> #include <set> #include <queue> #include <stack> #define mp(x,y) make_pair(x,y) using namespace std; const int maxN = 300000; set<pair<int, int> > deg; int DEG[maxN]; vector<int> ne[maxN]; vector<int> res; int d; int n, m; int u, v; int cc[maxN]; int cc2[maxN]; int M = 0; void rachuj(int i) { if (DEG[i] == 0 || cc[i] != 0) return; queue <int> q; int last = 1; q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); if (cc[u] == 0) { cc[u] = last++; for (const int& v : ne[u]) { if (DEG[v] != 0 && cc[v] == 0) { q.push(v); } } } } } void rachuj2(int i) { if (DEG[i] == 0 || cc2[i] != 0) return; queue <int> q; int last = 1; q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); if (cc2[u] == 0) { cc2[u] = last++; res.push_back(u); for (const int& v : ne[u]) { if (DEG[v] != 0 && cc2[v] == 0) { q.push(v); } } } } } int main() { cin >> n >> m >> d; for (int i = 0; i < m; ++i) { cin >> u >> v; ++DEG[u]; ++DEG[v]; ne[u].push_back(v); ne[v].push_back(u); } for (int i = 1; i <= n; ++i) deg.insert(mp(DEG[i], i)); auto f = deg.begin(); auto u = *f; int ile = deg.size(); while (ile > 0 && (u = *(f = deg.begin())).first < d) { deg.erase(u); for (const auto& v : ne[u.second]) { if (DEG[v] > 0) { deg.erase(mp(DEG[v], v)); --DEG[v]; deg.insert(mp(DEG[v], v)); } } DEG[u.second] = 0; --ile; } for (int i = 1; i <= n; ++i) { rachuj(i); } int g = 0; for (int i = 1; i <= n; ++i) { if (cc[i] > M) { g = i; M = cc[i]; } } if (M > 0) { printf("%d\n", M); rachuj2(g); sort(res.begin(), res.end()); for (const int& k : res) { printf("%d ", k); } } else { printf("NIE\n"); } 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 | #include <iostream> #include <cstdlib> #include <algorithm> #include <vector> #include <set> #include <queue> #include <stack> #define mp(x,y) make_pair(x,y) using namespace std; const int maxN = 300000; set<pair<int, int> > deg; int DEG[maxN]; vector<int> ne[maxN]; vector<int> res; int d; int n, m; int u, v; int cc[maxN]; int cc2[maxN]; int M = 0; void rachuj(int i) { if (DEG[i] == 0 || cc[i] != 0) return; queue <int> q; int last = 1; q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); if (cc[u] == 0) { cc[u] = last++; for (const int& v : ne[u]) { if (DEG[v] != 0 && cc[v] == 0) { q.push(v); } } } } } void rachuj2(int i) { if (DEG[i] == 0 || cc2[i] != 0) return; queue <int> q; int last = 1; q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); if (cc2[u] == 0) { cc2[u] = last++; res.push_back(u); for (const int& v : ne[u]) { if (DEG[v] != 0 && cc2[v] == 0) { q.push(v); } } } } } int main() { cin >> n >> m >> d; for (int i = 0; i < m; ++i) { cin >> u >> v; ++DEG[u]; ++DEG[v]; ne[u].push_back(v); ne[v].push_back(u); } for (int i = 1; i <= n; ++i) deg.insert(mp(DEG[i], i)); auto f = deg.begin(); auto u = *f; int ile = deg.size(); while (ile > 0 && (u = *(f = deg.begin())).first < d) { deg.erase(u); for (const auto& v : ne[u.second]) { if (DEG[v] > 0) { deg.erase(mp(DEG[v], v)); --DEG[v]; deg.insert(mp(DEG[v], v)); } } DEG[u.second] = 0; --ile; } for (int i = 1; i <= n; ++i) { rachuj(i); } int g = 0; for (int i = 1; i <= n; ++i) { if (cc[i] > M) { g = i; M = cc[i]; } } if (M > 0) { printf("%d\n", M); rachuj2(g); sort(res.begin(), res.end()); for (const int& k : res) { printf("%d ", k); } } else { printf("NIE\n"); } return 0; } |