#include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; const int maxn = 2 * 1e5; vector<int> g[maxn]; int eleft[maxn]; bool visited[maxn]; queue<int> q; vector<vector<int> > ss; void dfs(int x, int ix) { visited[x] = true; ss[ix].push_back(x); for (int i = 0; i < g[x].size(); i++) { int y = g[x][i]; if (eleft[y] != -1 && !visited[y]) { dfs(y, ix); } } } int main() { ios_base::sync_with_stdio(0); int n, m, d; cin >> n >> m >> d; int a, b; for (int i = 0; i < m; i++) { cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } for (int i = 0; i < n; i++) { eleft[i] = g[i].size(); visited[i] = false; if (eleft[i] < d) { eleft[i] = -1; q.push(i); } } int x, y; while (!q.empty()) { x = q.front(); q.pop(); for (int i = 0; i < g[x].size(); i++) { y = g[x][i]; if (eleft[y] == -1) continue; eleft[y]--; if (eleft[y] < d) { eleft[y] = -1; q.push(y); } } } for (int i = 0; i < n; i++) { if (eleft[i] != -1 && !visited[i]) { int ix = ss.size(); vector<int> v; ss.push_back(v); dfs(i, ix); } } int bx = 0; int bc = 0; int cs; for (int i = 0; i < ss.size(); i++) { cs = ss[i].size(); if (cs > bc) { bc = cs; bx = i; } } if (bc == 0) { cout << "NIE"; } else { sort(ss[bx].begin(), ss[bx].end()); cout << bc << endl; for (int i = 0; i < ss[bx].size(); i++) { cout << ss[bx][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 | #include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; const int maxn = 2 * 1e5; vector<int> g[maxn]; int eleft[maxn]; bool visited[maxn]; queue<int> q; vector<vector<int> > ss; void dfs(int x, int ix) { visited[x] = true; ss[ix].push_back(x); for (int i = 0; i < g[x].size(); i++) { int y = g[x][i]; if (eleft[y] != -1 && !visited[y]) { dfs(y, ix); } } } int main() { ios_base::sync_with_stdio(0); int n, m, d; cin >> n >> m >> d; int a, b; for (int i = 0; i < m; i++) { cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } for (int i = 0; i < n; i++) { eleft[i] = g[i].size(); visited[i] = false; if (eleft[i] < d) { eleft[i] = -1; q.push(i); } } int x, y; while (!q.empty()) { x = q.front(); q.pop(); for (int i = 0; i < g[x].size(); i++) { y = g[x][i]; if (eleft[y] == -1) continue; eleft[y]--; if (eleft[y] < d) { eleft[y] = -1; q.push(y); } } } for (int i = 0; i < n; i++) { if (eleft[i] != -1 && !visited[i]) { int ix = ss.size(); vector<int> v; ss.push_back(v); dfs(i, ix); } } int bx = 0; int bc = 0; int cs; for (int i = 0; i < ss.size(); i++) { cs = ss[i].size(); if (cs > bc) { bc = cs; bx = i; } } if (bc == 0) { cout << "NIE"; } else { sort(ss[bx].begin(), ss[bx].end()); cout << bc << endl; for (int i = 0; i < ss[bx].size(); i++) { cout << ss[bx][i]+1 << ' '; } } cout << endl; return 0; } |