#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
#define MAX_SIZE 100001
vector<int> graph[MAX_SIZE];
int arr[MAX_SIZE];
bool vis[MAX_SIZE];
int main()
{
int n; cin >> n;
for (int i = 1; i < n; i++)
{
int y, x; cin >> y >> x;
graph[y].push_back(x);
graph[x].push_back(y);
}
stack<int> s; s.push(1);
vis[1] = true;
while (!s.empty())
{
auto idx = s.top(); s.pop();
for (int i = 0; i < graph[idx].size(); i++)
{
auto next = graph[idx][i];
if (!vis[next])
{
vis[next] = true;
arr[next] = idx;
s.push(next);
}
}
}
for (int i = 2; i <= n; i++)
cout << arr[i] << "\n";
}
'코딩테스트 > 백준' 카테고리의 다른 글
[실4] 1120 - 문자열 (0) | 2022.10.24 |
---|---|
[실4] 3986 - 좋은 단어 (0) | 2022.10.23 |
[실3] 15654 - N과 M (5) (0) | 2022.10.07 |
[골4] 1922 - 네트워크 연결 (0) | 2022.10.07 |
[골5] 2493 - 탑 (0) | 2022.09.28 |