-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_11725.cpp
More file actions
46 lines (37 loc) · 721 Bytes
/
BOJ_11725.cpp
File metadata and controls
46 lines (37 loc) · 721 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int n;
int answer[100001];
bool visited[100001];
vector<int> graph[100001];
queue<int> q;
void BFS() {
visited[1] = true;
q.push(1);
while (!q.empty()) {
int parent = q.front();
q.pop();
for (int i = 0; i < (int)graph[parent].size(); i++) {
int child = graph[parent][i];
if (visited[child] == false) {
answer[child] = parent;
visited[child] = true;
q.push(child);
}
}
}
}
int main() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int first, second;
cin >> first >> second;
graph[first].push_back(second);
graph[second].push_back(first);
}
BFS();
for (int i = 2; i <= n; i++) {
cout << answer[i] << '\n';
}
return 0;
}