-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreating Offices.cpp
More file actions
executable file
·79 lines (75 loc) · 1.22 KB
/
Copy pathCreating Offices.cpp
File metadata and controls
executable file
·79 lines (75 loc) · 1.22 KB
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
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
vector<int> v[200009];
vector<int> w[200009];
int dist[200009];
int t[200009];
int tt[200009];
vector<int> order, res;
int n, d, c = 1;
void set_depth() {
vector<bool> odw(n);
queue<int> q;
q.push(0);
odw[0] = true;
while (!q.empty()) {
int st = q.front();
q.pop();
order.push_back(t[st]);
for (auto x : v[st]) {
if (!odw[x]) {
t[x] = c;
tt[c] = x;
c++;
q.push(x);
odw[x] = true;
w[t[x]].push_back(t[st]);
w[t[st]].push_back(t[x]);
}
}
}
reverse(order.begin(), order.end());
}
void bfs(int st) {
queue<int> q;
q.push(st);
dist[st] = d;
while (!q.empty()) {
st = q.front();
q.pop();
for (auto x : w[st]) {
if (dist[x] < dist[st] - 1) {
dist[x] = dist[st] - 1;
q.push(x);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> d;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
v[a].push_back(b);
v[b].push_back(a);
}
set_depth();
for (auto o : order) {
if (dist[o] == 0) {
res.push_back(o);
bfs(o);
}
}
cout << int(res.size()) << "\n";
for (auto x : res) {
cout << tt[x] + 1 << " ";
}
cout << "\n";
return 0;
}