cgy12306

[백준 BoJ] 11725 - 트리의 부모 찾기 본문

Algorithm

[백준 BoJ] 11725 - 트리의 부모 찾기

cgy12306 2021. 7. 5. 17:00
// https://www.acmicpc.net/problem/11725
// 트리의 부모 찾기

#include<iostream>
#include<vector>
using namespace std;

int N;

void dfs(vector<vector<int>> &edge, vector<bool> &visited, vector<int> &parent, int start) {
	visited[start] = true;

	for (auto next : edge[start]) {
		if (!visited[next]) {
			parent[next] = start;
			dfs(edge, visited, parent, next);
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	
	cin >> N;

	vector<vector<int>> edge(N+1);
	vector<bool> visited(N+1, false);
	vector<int> parent(N + 1);

	for (int i = 0; i < N - 1; i++) {
		int from, to;
		cin >> from >> to;
		edge[from].push_back(to);
		edge[to].push_back(from);
	}

	for (int node = 1; node <= N; node++) {
		if (!visited[node]) {
			dfs(edge, visited, parent, node);
		}
	}

	for (int i = 2; i <= N; i++) {
		cout << parent[i] << "\n";
	}
}

'Algorithm' 카테고리의 다른 글

2021 카카오 신입공채 1차 문제 1번 - 아이디 추천  (0) 2021.09.08
Comments