• 문제 링크
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
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
public class Main {
static Node[] tree;
static int[] level;
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int n = read();
tree = new Node[n];
level = new int[n];
for (int i = 0; i < n; i++) tree[i] = new Node(i);
int root = 0, node;
for (int i = 0; i < n; i++) {
if ((node = read() - 1) == 130) root = i;
else tree[node].addEdge(i);
}
dfs(root);
for (int i : level) sb.append(i).append("\n");
bw.write(sb.toString());
bw.flush();
}
private static void dfs(int src) {
for (int tgt : tree[src].adj) {
level[tgt] = level[src] + 1;
dfs(tgt);
}
}
private static class Node {
int src;
List<Integer> adj;
Node(int src) {
this.src = src;
this.adj = new ArrayList<>();
}
public void addEdge(int tgt) {
adj.add(tgt);
}
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 26170 사과 빨리 먹기 - Graph Theory / Java (0) | 2023.10.16 |
---|---|
[백준] 26169 세 번 이내에 사과를 먹자 - Graph Theory / Java (0) | 2023.10.15 |
[백준] 21316 스피카 - Graph Theory / Java (0) | 2023.10.13 |
[백준] 13399 Rearranging a Sequence - Data Structure / Java (0) | 2023.10.12 |
[백준] 17287 The Deeper, The Better - Data Structure / Java (0) | 2023.10.11 |
댓글