//Daniel Grzegorzewski #include <bits/stdc++.h> #define MP make_pair #define PB push_back #define ST first #define ND second using namespace std; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<PII> VII; typedef long long LL; void init_ios() { ios_base::sync_with_stdio(0); cin.tie(0); } const int N = 2*(int)1e5 + 10; int n, m, d, si[N], p[N]; VI child[N]; set<PII> s; bool jest[N]; VI res[N]; int Find(int x) { if (x == p[x]) return x; return p[x] = Find(p[x]); } void Union(int a, int b) { a = Find(a); b = Find(b); if (a == b) return; if (res[b].size() <= res[a].size()) { p[b] = a; for (int i = 0; i < res[b].size(); ++i) res[a].PB(res[b][i]); res[b].clear(); } else { p[a] = b; for (int i = 0; i < res[a].size(); ++i) res[b].PB(res[a][i]); res[a].clear(); } } int main() { init_ios(); cin >> n >> m >> d; for (int i = 1; i <= m; ++i) { int x, y; cin >> x >> y; child[x].PB(y); child[y].PB(x); ++si[x]; ++si[y]; } for (int i = 1; i <= n; ++i) { jest[i] = true; p[i] = i; res[i].PB(i); s.insert(MP(si[i], i)); } while (s.size() > 0 && (*s.begin()).ST < d) { int v = (*s.begin()).ND; s.erase(s.begin()); jest[v] = false; for (int i = 0; i < child[v].size(); ++i) { int u = child[v][i]; s.erase(MP(si[u], u)); --si[u]; if (jest[u]) s.insert(MP(si[u], u)); } } if (s.size() == 0) { cout<<"NIE\n"; return 0; } for (set<PII>::iterator it = s.begin(); it != s.end(); ++it) { int v = (*it).ND; for (int i = 0; i < child[v].size(); ++i) if (jest[child[v][i]]) Union(v, child[v][i]); } int wyn = -1; for (int i = 1; i <= n; ++i) if (jest[i] && (wyn == -1 || res[Find(i)].size() > res[Find(wyn)].size())) wyn = i; cout<<res[Find(wyn)].size()<<"\n"; for (int i = 0; i < res[Find(wyn)].size(); ++i) cout<<res[Find(wyn)][i]<<" "; cout<<"\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 93 94 95 96 97 98 99 100 101 102 103 104 105 | //Daniel Grzegorzewski #include <bits/stdc++.h> #define MP make_pair #define PB push_back #define ST first #define ND second using namespace std; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<PII> VII; typedef long long LL; void init_ios() { ios_base::sync_with_stdio(0); cin.tie(0); } const int N = 2*(int)1e5 + 10; int n, m, d, si[N], p[N]; VI child[N]; set<PII> s; bool jest[N]; VI res[N]; int Find(int x) { if (x == p[x]) return x; return p[x] = Find(p[x]); } void Union(int a, int b) { a = Find(a); b = Find(b); if (a == b) return; if (res[b].size() <= res[a].size()) { p[b] = a; for (int i = 0; i < res[b].size(); ++i) res[a].PB(res[b][i]); res[b].clear(); } else { p[a] = b; for (int i = 0; i < res[a].size(); ++i) res[b].PB(res[a][i]); res[a].clear(); } } int main() { init_ios(); cin >> n >> m >> d; for (int i = 1; i <= m; ++i) { int x, y; cin >> x >> y; child[x].PB(y); child[y].PB(x); ++si[x]; ++si[y]; } for (int i = 1; i <= n; ++i) { jest[i] = true; p[i] = i; res[i].PB(i); s.insert(MP(si[i], i)); } while (s.size() > 0 && (*s.begin()).ST < d) { int v = (*s.begin()).ND; s.erase(s.begin()); jest[v] = false; for (int i = 0; i < child[v].size(); ++i) { int u = child[v][i]; s.erase(MP(si[u], u)); --si[u]; if (jest[u]) s.insert(MP(si[u], u)); } } if (s.size() == 0) { cout<<"NIE\n"; return 0; } for (set<PII>::iterator it = s.begin(); it != s.end(); ++it) { int v = (*it).ND; for (int i = 0; i < child[v].size(); ++i) if (jest[child[v][i]]) Union(v, child[v][i]); } int wyn = -1; for (int i = 1; i <= n; ++i) if (jest[i] && (wyn == -1 || res[Find(i)].size() > res[Find(wyn)].size())) wyn = i; cout<<res[Find(wyn)].size()<<"\n"; for (int i = 0; i < res[Find(wyn)].size(); ++i) cout<<res[Find(wyn)][i]<<" "; cout<<"\n"; } |