diff --git "a/H0ngJu/DFS/\355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.py" "b/H0ngJu/DFS/\355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.py" new file mode 100644 index 00000000..7fb0fd9f --- /dev/null +++ "b/H0ngJu/DFS/\355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.py" @@ -0,0 +1,28 @@ +import sys +sys.setrecursionlimit(10**9) +def input() : return sys.stdin.readline().rstrip() + +n = int(input()) +tree = [[] for _ in range(n+1)] +visited = [-1] * (n+1) +visited[1] = 0 + +for i in range(n-1): + v, u, w = map(int, input().split()) + tree[v].append((u, w)) + tree[u].append((v, w)) + +def dfs(start, dis): + for node, node_dis in tree[start]: + if visited[node] == -1: + visited[node] = dis + node_dis + dfs(node, dis + node_dis) + +dfs(1,0) + +far_node = visited.index(max(visited)) +visited = [-1] * (n+1) +visited[far_node] = 0 +dfs(far_node, 0) + +print(max(visited)) \ No newline at end of file diff --git a/H0ngJu/README.md b/H0ngJu/README.md index 957950e0..6ac80a48 100644 --- a/H0ngJu/README.md +++ b/H0ngJu/README.md @@ -28,5 +28,6 @@ | 24차시 | 2024.08.17 | BFS | [아기상어](https://www.acmicpc.net/problem/16236) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/233 | | 25차시 | 2024.08.17 | DFS | [친구비](https://www.acmicpc.net/problem/16562) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/234 | | 26차시 | 2024.08.24 | 그리디 | [신입사원](https://www.acmicpc.net/problem/1946) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/237 | +| 27차시 | 2024.08.27 | DFS | [트리의 지름](https://www.acmicpc.net/problem/1967) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/240 | --- diff --git "a/H0ngJu/\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.py" "b/H0ngJu/\355\225\264\354\213\234/\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.py" similarity index 100% rename from "H0ngJu/\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.py" rename to "H0ngJu/\355\225\264\354\213\234/\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.py"