일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- dfs
- 가끔은 말로
- pytorch
- 우선 순위 큐
- NEXT
- 미래는_현재와_과거로
- 백트래킹
- 너비 우선 탐색
- 세그먼트 트리
- 조합론
- BFS
- 이분 탐색
- 자바스크립트
- 2023
- lazy propagation
- tensorflow
- DP
- dropout
- 분할 정복
- 회고록
- object detection
- 문자열
- back propagation
- Overfitting
- 가끔은_말로
- 플로이드 와샬
- 다익스트라
- 크루스칼
- c++
- 알고리즘
Archives
- Today
- Total
Doby's Lab
백준 13237번: Binary tree (C++) 본문
https://www.acmicpc.net/problem/13237
13237번: Binary tree
A binary tree is a mathematical structure made of nodes where each node can have up to two children nodes. One child node will be called left child and the other right child. ch If node B is a child node of A, then we say that A is the parent node of B. In
www.acmicpc.net
Solved By: DFS
#include <iostream>
#include <vector>
#define MAX 21
using namespace std;
int n;
vector<int> adj[MAX];
int level[MAX];
int root;
void dfs(int now){
for(int i = 0; i < adj[now].size(); i++){
int next = adj[now][i];
level[next] = level[now] + 1;
dfs(next);
}
}
int main(){
cin >> n;
for(int i = 1, v; i <= n; i++){
cin >> v;
if(v == -1) root = i;
else adj[v].push_back(i);
}
dfs(root);
for(int i = 1; i <= n; i++) cout << level[i] << '\n';
return 0;
}
728x90
'PS > BOJ' 카테고리의 다른 글
백준 4779번: 칸토어 집합 (C++) (0) | 2022.07.30 |
---|---|
백준 17362번: 수학은 체육과목 입니다 2 (Python) (0) | 2022.07.26 |
백준 2210번: 숫자판 점프 (C++) (0) | 2022.07.24 |
백준 17103번: 골드바흐 파티션 (C++) (0) | 2022.07.24 |
백준 24309번: 2021은 무엇이 특별할까? (C++) (0) | 2022.07.21 |