-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11725.cpp
More file actions
39 lines (34 loc) · 692 Bytes
/
Copy path11725.cpp
File metadata and controls
39 lines (34 loc) · 692 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
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<int> graph[100001];
int visited[100001];
queue<int> q;
int N;
void BFS(){
q.push(1);
visited[1]=1;
while(!q.empty()){
int v=q.front();
q.pop();
for(int i=0;i<graph[v].size();i++){
if(visited[graph[v][i]]==0){
q.push(graph[v][i]);
visited[graph[v][i]]=v;
}
}
}
}
int main(){
cin >> N;
for(int i=0; i<N-1;i++){
int s,e; cin >> s >> e;
graph[s].push_back(e);
graph[e].push_back(s);
}
BFS();
for(int i=2; i<=N; i++){
cout << visited[i] << "\n";
}
}