-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11725.cpp
More file actions
37 lines (37 loc) · 744 Bytes
/
11725.cpp
File metadata and controls
37 lines (37 loc) · 744 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
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int d[100001];
vector<int> v[100001];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,x,y;
memset(d,-1,sizeof(d));
d[1]=1;
cin>>n;
for(int i=0;i<n-1;i++){
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
queue<int> q;
q.push(1);
d[1]=1;
while(!q.empty()){
int cur = q.front();
q.pop();
for(int i=0;i<v[cur].size();i++){
int next = v[cur][i];
if(d[next]==-1){
d[next]=cur;
q.push(next);
}
}
}
for(int i=2;i<=n;i++){
cout<<d[i]<<"\n";
}
}