#include <cstdio>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
const int N = 2e5+3;
set<int> v[N];
vector<int> tree;
int vis[N], it;
void dfs(int a) {
vis[a] = it;
tree.push_back(a);
vector<int> toRemove;
for (int b: v[a]) {
if (vis[b] == it) continue;
toRemove.push_back(b);
dfs(b);
}
for (int b: toRemove)
v[a].erase(b);
}
int main() {
int n, m, d, a, b;
scanf("%d%d%d", &n,&m,&d);
while (m--) {
scanf("%d%d", &a,&b);
v[a].insert(b);
v[b].insert(a);
}
set<int> notIsolated;
for (int i=1; i<=n; i++)
if (!v[i].empty())
notIsolated.insert(i);
vector<int> maxTree;
while (d--) {
maxTree.clear();
vector<int> newIsolated;
it++;
for (int x: notIsolated) {
if (vis[x] == it) continue;
tree.clear();
dfs(x);
if (tree.size() == 1) newIsolated.push_back(x);
else if (tree.size() > maxTree.size()) maxTree = tree;
}
for (int x: newIsolated) notIsolated.erase(x);
}
if (maxTree.empty()) {
printf("NIE\n");
return 0;
}
printf("%d\n", maxTree.size());
sort(maxTree.begin(), maxTree.end());
for (int x: maxTree) printf("%d ", x);
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 | #include <cstdio> #include <vector> #include <set> #include <algorithm> using namespace std; const int N = 2e5+3; set<int> v[N]; vector<int> tree; int vis[N], it; void dfs(int a) { vis[a] = it; tree.push_back(a); vector<int> toRemove; for (int b: v[a]) { if (vis[b] == it) continue; toRemove.push_back(b); dfs(b); } for (int b: toRemove) v[a].erase(b); } int main() { int n, m, d, a, b; scanf("%d%d%d", &n,&m,&d); while (m--) { scanf("%d%d", &a,&b); v[a].insert(b); v[b].insert(a); } set<int> notIsolated; for (int i=1; i<=n; i++) if (!v[i].empty()) notIsolated.insert(i); vector<int> maxTree; while (d--) { maxTree.clear(); vector<int> newIsolated; it++; for (int x: notIsolated) { if (vis[x] == it) continue; tree.clear(); dfs(x); if (tree.size() == 1) newIsolated.push_back(x); else if (tree.size() > maxTree.size()) maxTree = tree; } for (int x: newIsolated) notIsolated.erase(x); } if (maxTree.empty()) { printf("NIE\n"); return 0; } printf("%d\n", maxTree.size()); sort(maxTree.begin(), maxTree.end()); for (int x: maxTree) printf("%d ", x); printf("\n"); return 0; } |
English