-
Notifications
You must be signed in to change notification settings - Fork 87
/
026.cpp
54 lines (48 loc) · 941 Bytes
/
026.cpp
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
#include <iostream>
#include <vector>
using namespace std;
// Input
int N;
int A[1 << 18], B[1 << 18];
// Graph
vector<int> G[1 << 18];
int col[1 << 18];
void dfs(int pos, int cur) {
col[pos] = cur;
for (int i : G[pos]) {
if (col[i] >= 1) continue;
dfs(i, 3 - cur);
}
}
int main() {
// Step #1. Input
cin >> N;
for (int i = 1; i <= N - 1; i++) {
cin >> A[i] >> B[i];
G[A[i]].push_back(B[i]);
G[B[i]].push_back(A[i]);
}
// Step #2. Graph Coloring
dfs(1, 1);
// Step #3. Get Answer
vector<int> G1, G2;
for (int i = 1; i <= N; i++) {
if (col[i] == 1) G1.push_back(i);
if (col[i] == 2) G2.push_back(i);
}
if (G1.size() > G2.size()) {
for (int i = 0; i < (N / 2); i++) {
if (i) cout << " ";
cout << G1[i];
}
cout << endl;
}
else {
for (int i = 0; i < (N / 2); i++) {
if (i) cout << " ";
cout << G2[i];
}
cout << endl;
}
return 0;
}