-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11725.cpp
51 lines (46 loc) · 986 Bytes
/
11725.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
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
vector<int>graph[100001];
int answer[100001] = {0, };
int visited[100001] = {0, };
int n;
void input();
void bfs();
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
input();
bfs();
for(int i = 2; i <= n; i++){
cout << answer[i] << '\n';
}
}
void input(){
cin>> n;
int tmp1, tmp2;
for(int i = 0; i < n-1; i++){
cin >> tmp1 >> tmp2;
graph[tmp1].push_back(tmp2);
graph[tmp2].push_back(tmp1);
}
}
void bfs(){
queue<int>q;
visited[1] = 1;
q.push(1);
while(!q.empty()){
int nowNode = q.front();
q.pop();
for(int i = 0; i < graph[nowNode].size(); i++){
int nextNode = graph[nowNode][i];
if(visited[nextNode] == 0){
answer[nextNode] = nowNode;
visited[nextNode] = 1;
q.push(nextNode);
}
}
}
}