Skip to content

Commit ce9a0a5

Browse files
committed
[Tree] baekjoon-1167
1 parent b030ac0 commit ce9a0a5

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

โ€ŽREADME.md

+1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
| 01 | | [Baekjoon-1991 ํŠธ๋ฆฌ ์ˆœํšŒ](./src/Tree/P1991) | |
154154
| 02 | | [Baekjoon-1068 ํŠธ๋ฆฌ](./src/Tree/P1068) | |
155155
| 03 | | [SWEA-1233 ์‚ฌ์น™์—ฐ์‚ฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ](./src/Tree/swea1233) | |
156+
| 04 | โญ๏ธ | [Baekjoon-1167 ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„](./src/Tree/P1167) | |
156157

157158
### Heap
158159

โ€Žsrc/Tree/P1167/Main.java

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package Tree.P1167;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
static int V, max = Integer.MIN_VALUE, root, dist;
9+
static ArrayList<Node>[] tree;
10+
static boolean[] visited;
11+
12+
public static void main(String[] args) throws Exception {
13+
System.setIn(new FileInputStream("src/Tree/P1167/input.txt"));
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
16+
V = Integer.parseInt(br.readLine());
17+
tree = new ArrayList[V+1];
18+
19+
for (int i = 1; i <= V; i++) {
20+
StringTokenizer st = new StringTokenizer(br.readLine());
21+
int from = Integer.parseInt(st.nextToken()), to, dist;
22+
tree[from] = new ArrayList<>();
23+
while ((to = Integer.parseInt(st.nextToken())) != -1) {
24+
dist = Integer.parseInt(st.nextToken());
25+
tree[from].add(new Node(to, dist));
26+
}
27+
}
28+
29+
visited = new boolean[V+1];
30+
dfs(1, 0);
31+
visited = new boolean[V+1];
32+
dfs(root, 0);
33+
34+
System.out.println(max);
35+
}
36+
37+
static void dfs(int cur, int dist) {
38+
visited[cur] = true;
39+
for (Node node : tree[cur]) {
40+
if (!visited[node.to]) {
41+
dfs(node.to, dist + node.dist);
42+
}
43+
}
44+
if (dist > max) {
45+
max = dist;
46+
root = cur;
47+
}
48+
}
49+
50+
static class Node {
51+
int to, dist;
52+
53+
public Node(int to, int dist) {
54+
this.to = to;
55+
this.dist = dist;
56+
}
57+
}
58+
}

โ€Žsrc/Tree/P1167/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## [baekjoon-1167] ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„
2+
3+
![image](https://user-images.githubusercontent.com/22045163/108088159-7dff2300-70bb-11eb-9da5-d20461b4ae76.png)
4+
5+
### ํ’€์ด ๊ณผ์ •
6+
7+
์ด ๋ฌธ์ œ ํ’€๋‹ค๊ฐ€ ์šธ ๋ป” ํ–ˆ๋‹ค. ๋‹จ์ˆœํžˆ ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ์ฒ˜๋Ÿผ ํ’€ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ํŠธ๋ฆฌ์˜ ์„ฑ์งˆ์„ ์ด์šฉํ•ด ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ƒ๊ฐํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.
8+
9+
1. ํŠธ๋ฆฌ์˜ ๊ฐ„์„ ์—๋Š” ๋ฐฉํ–ฅ์ด ์—†๋‹ค.
10+
2. ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ๋Š” ๋ชจ๋‘ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค.
11+
12+
๋”ฐ๋ผ์„œ ์ž„์˜์˜ ๋…ธ๋“œ์—์„œ ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ ์ฐพ๊ณ (๋ฃจํŠธ ๋˜๋Š” ๋ฆฌํ”„ ๋…ธ๋“œ), ๊ทธ ๋…ธ๋“œ์—์„œ ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ ๋‹ค์‹œ ์ฐพ์œผ๋ฉด
13+
์ž„์˜์˜ ๋‘ ์  ์‚ฌ์ด์˜ ๊ฐ€์žฅ ๊ธด ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
14+
15+
ํŠธ๋ฆฌ ๋ฌธ์ œ๋ฅผ ๋” ๋งŽ์ด ์—ฐ์Šตํ•ด๋ณด์ž.
16+
17+
![image](https://user-images.githubusercontent.com/22045163/108088199-89eae500-70bb-11eb-8860-60b133114747.png)

โ€Žsrc/Tree/P1167/input.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
5
2+
5 4 6 -1
3+
4 2 4 3 3 5 6 -1
4+
3 1 2 4 3 -1
5+
2 4 4 -1
6+
1 3 2 -1

0 commit comments

Comments
ย (0)