#include <cstdio> #include <vector> #include <algorithm> #include <set> const int MAXN = 1000000; std::vector<int> adj[MAXN]; std::vector<int> path, best_path; int deg[MAXN]; bool visited[MAXN]; int n, m, d; struct CmpSet { bool operator()(int a, int b) const { if (deg[a] == deg[b]) return a < b; return deg[a] < deg[b]; } }; void dfs(int u) { if (visited[u]) return; visited[u] = true; path.push_back(u); for (int i = 0; i < adj[u].size(); i++) dfs(adj[u][i]); } int main() { scanf("%d%d%d", &n, &m, &d); for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); u--, v--; adj[u].push_back(v); adj[v].push_back(u); deg[u]++, deg[v]++; } std::set<int, CmpSet> set; for (int i = 0; i < n; i++) set.insert(i); while (!set.empty() && deg[*set.begin()] < d) { int u = *set.begin(); set.erase(set.begin()); visited[u] = true; for (int i = 0; i < adj[u].size(); i++) { if (set.find(adj[u][i]) != set.end()) { set.erase(set.find(adj[u][i])); deg[adj[u][i]]--; set.insert(adj[u][i]); } } } for (int i = 0; i < n; i++) if (!visited[i]) { path.clear(); dfs(i); if (path.size() > best_path.size()) best_path = path; } if (best_path.empty()) printf("NIE\n"); else { std::sort(best_path.begin(), best_path.end()); printf("%d\n", (int)best_path.size()); for (int i = 0; i < (int)best_path.size(); i++) printf("%d ", best_path[i] + 1); printf("\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 | #include <cstdio> #include <vector> #include <algorithm> #include <set> const int MAXN = 1000000; std::vector<int> adj[MAXN]; std::vector<int> path, best_path; int deg[MAXN]; bool visited[MAXN]; int n, m, d; struct CmpSet { bool operator()(int a, int b) const { if (deg[a] == deg[b]) return a < b; return deg[a] < deg[b]; } }; void dfs(int u) { if (visited[u]) return; visited[u] = true; path.push_back(u); for (int i = 0; i < adj[u].size(); i++) dfs(adj[u][i]); } int main() { scanf("%d%d%d", &n, &m, &d); for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); u--, v--; adj[u].push_back(v); adj[v].push_back(u); deg[u]++, deg[v]++; } std::set<int, CmpSet> set; for (int i = 0; i < n; i++) set.insert(i); while (!set.empty() && deg[*set.begin()] < d) { int u = *set.begin(); set.erase(set.begin()); visited[u] = true; for (int i = 0; i < adj[u].size(); i++) { if (set.find(adj[u][i]) != set.end()) { set.erase(set.find(adj[u][i])); deg[adj[u][i]]--; set.insert(adj[u][i]); } } } for (int i = 0; i < n; i++) if (!visited[i]) { path.clear(); dfs(i); if (path.size() > best_path.size()) best_path = path; } if (best_path.empty()) printf("NIE\n"); else { std::sort(best_path.begin(), best_path.end()); printf("%d\n", (int)best_path.size()); for (int i = 0; i < (int)best_path.size(); i++) printf("%d ", best_path[i] + 1); printf("\n"); } } |