Skip to content

Commit

Permalink
Merge pull request #220 from AlgoLeadMe/65-tgyuuAn
Browse files Browse the repository at this point in the history
65-tgyuuAn
  • Loading branch information
tgyuuAn authored Aug 6, 2024
2 parents 294365d + 350ec2e commit 0e4216c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@
| 62차시 | 2024.07.01 | DP | <a href="https://www.acmicpc.net/problem/1949">우수 마을</a> | https://github.com/AlgoLeadMe/AlgoLeadMse-1/pull/214
| 63차시 | 2024.07.08 | BFS | <a href="https://www.acmicpc.net/problem/3108">로고</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/216
| 64차시 | 2024.07.12 | 최소 공통 조상 | <a href="https://www.acmicpc.net/problem/11812">K진 트리</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/217
| 65차시 | 2024.07.19 | 최소 공통 조상 | <a href="https://www.acmicpc.net/problem/3584">가장 가까운 공통 조상</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/220
---
39 changes: 39 additions & 0 deletions tgyuuAn/최소 공통 조상/가장 가까운 공통 조상.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import sys

def input(): return sys.stdin.readline().rstrip()

T = int(input())
for _ in range(T):
N = int(input())
parent = [0 for _ in range(N+1)]

for _ in range(N-1):
A, B = map(int, input().split())
parent[B] = A

x, y = map(int, input().split())
x_depth = 0
now_node = x
while now_node != 0:
now_node = parent[now_node]
x_depth += 1

y_depth = 0
now_node = y
while now_node != 0:
now_node = parent[now_node]
y_depth += 1

while x_depth > y_depth:
x_depth -= 1
x = parent[x]

while y_depth > x_depth:
y_depth -= 1
y = parent[y]

while x != y:
x = parent[x]
y = parent[y]

print(x)

0 comments on commit 0e4216c

Please sign in to comment.