#include <iostream> #include <cstdio> #include <vector> #include <queue> using namespace std; vector<int> sas[200005]; vector<int> sas2[200005]; int good[200005]; int count[200005]; int vis[200005]; priority_queue<pair<int, int> > Q; priority_queue<int> results; int n, m, d; int a, b, x, y, value; int result = 0, res, lider; void GO(int v) { vis[v] = 1; res++; for(int k = 0; k < sas2[v].size(); k++) { int u = sas2[v][k]; if(vis[u] == 0) GO(u); } } void GOGO(int v) { vis[v] = 2; results.push(-v); for(int k = 0; k < sas2[v].size(); k++) { int u = sas2[v][k]; if(vis[u] == 1) GOGO(u); } } int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> d; for(int i = 1; i <= n; i++) { good[i] = 1; } int goods = n; for(int i = 1; i <= m; i++) { cin >> a >> b; sas[a].push_back(b); sas[b].push_back(a); count[a] = count[a] + 1; count[b] = count[b] + 1; } for(int i = 1; i <= n; i++) { Q.push(make_pair((-1)*count[i], i)); } while(!Q.empty() && (-1)*Q.top().first < d) { x = Q.top().second; value = (-1)*Q.top().first; Q.pop(); if(good[x] == 1) { good[x] = 0; goods--; for(int i = 0; i < sas[x].size(); i++) { y = sas[x][i]; count[y]--; if(good[y] == 1) Q.push(make_pair((-1)*count[y], y)); } } } if(goods == 0) { cout << "NIE\n"; return 0; } for(int i = 1; i <= n; i++) { if(good[i] == 0) continue; for(int j = 0; j < sas[i].size(); j++) { y = sas[i][j]; if(good[y] == 1) sas2[i].push_back(y); } } for(int i = 1; i <= n; i++) { if(good[i] == 1 && vis[i] == 0) { res = 0; GO(i); if(res > result) { result = res; lider = i; } } } cout << result << endl; GOGO(lider); while(!results.empty()) { cout << -results.top() << " "; results.pop(); } 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 | #include <iostream> #include <cstdio> #include <vector> #include <queue> using namespace std; vector<int> sas[200005]; vector<int> sas2[200005]; int good[200005]; int count[200005]; int vis[200005]; priority_queue<pair<int, int> > Q; priority_queue<int> results; int n, m, d; int a, b, x, y, value; int result = 0, res, lider; void GO(int v) { vis[v] = 1; res++; for(int k = 0; k < sas2[v].size(); k++) { int u = sas2[v][k]; if(vis[u] == 0) GO(u); } } void GOGO(int v) { vis[v] = 2; results.push(-v); for(int k = 0; k < sas2[v].size(); k++) { int u = sas2[v][k]; if(vis[u] == 1) GOGO(u); } } int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> d; for(int i = 1; i <= n; i++) { good[i] = 1; } int goods = n; for(int i = 1; i <= m; i++) { cin >> a >> b; sas[a].push_back(b); sas[b].push_back(a); count[a] = count[a] + 1; count[b] = count[b] + 1; } for(int i = 1; i <= n; i++) { Q.push(make_pair((-1)*count[i], i)); } while(!Q.empty() && (-1)*Q.top().first < d) { x = Q.top().second; value = (-1)*Q.top().first; Q.pop(); if(good[x] == 1) { good[x] = 0; goods--; for(int i = 0; i < sas[x].size(); i++) { y = sas[x][i]; count[y]--; if(good[y] == 1) Q.push(make_pair((-1)*count[y], y)); } } } if(goods == 0) { cout << "NIE\n"; return 0; } for(int i = 1; i <= n; i++) { if(good[i] == 0) continue; for(int j = 0; j < sas[i].size(); j++) { y = sas[i][j]; if(good[y] == 1) sas2[i].push_back(y); } } for(int i = 1; i <= n; i++) { if(good[i] == 1 && vis[i] == 0) { res = 0; GO(i); if(res > result) { result = res; lider = i; } } } cout << result << endl; GOGO(lider); while(!results.empty()) { cout << -results.top() << " "; results.pop(); } return 0; } |