-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11725.cpp
More file actions
49 lines (45 loc) · 714 Bytes
/
11725.cpp
File metadata and controls
49 lines (45 loc) · 714 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
47
48
49
// 11725. 트리의 부모 찾기
// 2019.09.26
// DFS
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> tree[100001];
int visit[100001];
int parents[100001];
void dfs(int cnt)
{
visit[cnt] = 1;
for (int i = 0; i < tree[cnt].size(); i++)
{
int child = tree[cnt][i];
if (!visit[child])
{
parents[child] = cnt;
dfs(child);
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n - 1; i++)
{
int x, y;
cin >> x >> y;
tree[x].push_back(y);
tree[y].push_back(x);
}
// 1이 루트이므로 1부터 DFS
dfs(1);
for (int i = 2; i <= n; i++)
{
cout << parents[i] << "\n";
}
return 0;
}