Skip to content

Commit

Permalink
2024-07-19
Browse files Browse the repository at this point in the history
  • Loading branch information
tgyuuAn committed Jul 19, 2024
1 parent 2472c41 commit 53a3ffd
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 @@ -68,4 +68,5 @@
| 61차시 | 2024.06.20 | 크루스칼 | <a href="https://www.acmicpc.net/problem/1774">우주신과의 교감</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/212
| 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
| 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 53a3ffd

Please sign in to comment.