#include <iostream> #include <algorithm> #include <cstdio> #include <vector> #include <set> #include <stack> using namespace std; const int MAXN = 200001; int n, m, d; vector<int> AL[MAXN]; int deg[MAXN]; int visited[MAXN]; int visited1[MAXN]; int ccWinNr = -1; int ccWinSize; int actCC = 1; struct deg_compare { bool operator() (const int& lhs, const int& rhs) const { if (deg[lhs] < deg[rhs]) return true; if (deg[lhs] > deg[rhs]) return false; return lhs < rhs; } }; void dfs(int start, set<int, deg_compare> &S) { stack<int> rec; rec.push(start); while (!rec.empty()) { start = rec.top(); rec.pop(); visited[start] = actCC; S.insert(start); for (size_t i = 0; i < AL[start].size(); i++) if (visited[AL[start][i]] == 0) rec.push(AL[start][i]); } } void dfs1(int start) { stack<int> rec; rec.push(start); int count = 0; while (!rec.empty()) { start = rec.top(); rec.pop(); ++count; visited1[start] = actCC; for (size_t i = 0; i < AL[start].size(); i++) if (visited1[AL[start][i]] == 0 && visited[AL[start][i]] != -1) rec.push(AL[start][i]); } if (count > ccWinSize) { ccWinSize = count; ccWinNr = actCC; } ++actCC; } void go(int start) { set<int, deg_compare> S; dfs(start, S); while (!S.empty() && deg[*S.begin()] < d) { int v = *S.begin(); visited[v] = -1; S.erase(S.begin()); for (size_t i = 0; i < AL[v].size(); i++) { int w = AL[v][i]; if (visited[w] > 0) { S.erase(w); --deg[w]; S.insert(w); } } } } int main(int argc, char* argv[]) { int a, b; scanf("%d%d%d", &n, &m, &d); while (m--) { scanf("%d%d", &a, &b); AL[a].push_back(b); AL[b].push_back(a); ++deg[a]; ++deg[b]; } for (int i = 1; i <= n; i++) if (visited[i] == 0) go(i); for (int i = 1; i <= n; i++) if (visited[i] != -1 && visited1[i] == 0) dfs1(i); if (ccWinNr == -1) printf("NIE\n"); else { vector<int> ans; for (int i = 1; i <= n; i++) if (visited1[i] == ccWinNr) ans.push_back(i); printf("%d\n", (int)ans.size()); for (size_t i = 0; i < ans.size(); i++) printf("%d ", ans[i]); printf("\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 | #include <iostream> #include <algorithm> #include <cstdio> #include <vector> #include <set> #include <stack> using namespace std; const int MAXN = 200001; int n, m, d; vector<int> AL[MAXN]; int deg[MAXN]; int visited[MAXN]; int visited1[MAXN]; int ccWinNr = -1; int ccWinSize; int actCC = 1; struct deg_compare { bool operator() (const int& lhs, const int& rhs) const { if (deg[lhs] < deg[rhs]) return true; if (deg[lhs] > deg[rhs]) return false; return lhs < rhs; } }; void dfs(int start, set<int, deg_compare> &S) { stack<int> rec; rec.push(start); while (!rec.empty()) { start = rec.top(); rec.pop(); visited[start] = actCC; S.insert(start); for (size_t i = 0; i < AL[start].size(); i++) if (visited[AL[start][i]] == 0) rec.push(AL[start][i]); } } void dfs1(int start) { stack<int> rec; rec.push(start); int count = 0; while (!rec.empty()) { start = rec.top(); rec.pop(); ++count; visited1[start] = actCC; for (size_t i = 0; i < AL[start].size(); i++) if (visited1[AL[start][i]] == 0 && visited[AL[start][i]] != -1) rec.push(AL[start][i]); } if (count > ccWinSize) { ccWinSize = count; ccWinNr = actCC; } ++actCC; } void go(int start) { set<int, deg_compare> S; dfs(start, S); while (!S.empty() && deg[*S.begin()] < d) { int v = *S.begin(); visited[v] = -1; S.erase(S.begin()); for (size_t i = 0; i < AL[v].size(); i++) { int w = AL[v][i]; if (visited[w] > 0) { S.erase(w); --deg[w]; S.insert(w); } } } } int main(int argc, char* argv[]) { int a, b; scanf("%d%d%d", &n, &m, &d); while (m--) { scanf("%d%d", &a, &b); AL[a].push_back(b); AL[b].push_back(a); ++deg[a]; ++deg[b]; } for (int i = 1; i <= n; i++) if (visited[i] == 0) go(i); for (int i = 1; i <= n; i++) if (visited[i] != -1 && visited1[i] == 0) dfs1(i); if (ccWinNr == -1) printf("NIE\n"); else { vector<int> ans; for (int i = 1; i <= n; i++) if (visited1[i] == ccWinNr) ans.push_back(i); printf("%d\n", (int)ans.size()); for (size_t i = 0; i < ans.size(); i++) printf("%d ", ans[i]); printf("\n"); } return 0; } |