-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6da8ae
commit 003b755
Showing
4 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[SW Expert Academy 1248](<https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV15PTkqAPYCFAYD&categoryId=AV15PTkqAPYCFAYD&categoryType=CODE>). | ||
|
||
Problem : 공통조상 **D5** | ||
|
||
임의의 이진 트리가 주어지고, 두 정점이 명시될 때 이들의 공통 조상 중 이들에 가장 가까운 정점을 찾고, 그 정점을 루트로 하는 서브 트리의 크기를 알아내는 프로그램 작성 | ||
|
||
Flow : | ||
|
||
1. 두 정점이 서로 조상과 자손 관계인 경우는 없다. | ||
2. 각 케이스의 첫줄에는 트리의 정점의 총 수 V와 간선의 총 수 E, 공통 조상을 찾는 두 개의 정점 번호가 주어짐 (정점의 수 V는 10 <= V <= 10000) | ||
|
||
|
||
|
||
Solution : | ||
|
||
1. |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#1 3 8 | ||
#2 1 10 | ||
#3 21 35 | ||
#4 1 100 | ||
#5 168 107 | ||
#6 1 500 | ||
#7 398 840 | ||
#8 747 1359 | ||
#9 498 3141 | ||
#10 7165 2435 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
def compare_route(route1, route2): | ||
len_1 = len(route1) | ||
len_2 = len(route2) | ||
|
||
if len_1 < len_2: | ||
length = len_1 | ||
else: | ||
length = len_2 | ||
|
||
result = 1 | ||
|
||
for i in range(length): | ||
if route1[len_1 - 1 - i] != route2[len_2 - 1 - i]: | ||
result = route1[len_1 - i] | ||
break | ||
|
||
return result | ||
|
||
|
||
def build(dict, node): | ||
current = node | ||
route = [] | ||
while current != 1: | ||
route.append(dict[current]) | ||
current = dict[current] | ||
|
||
return route | ||
|
||
|
||
def subtree_size(dict, node): | ||
size = 0 | ||
|
||
stack = [node] | ||
|
||
while len(stack) != 0: | ||
current = stack.pop() | ||
size += 1 | ||
|
||
count_check_child = 0 | ||
for child, mother in dict.items(): | ||
if mother == current: | ||
stack.append(child) | ||
count_check_child += 1 | ||
if count_check_child == 2: | ||
break | ||
|
||
return size | ||
|
||
|
||
if __name__ == '__main__': | ||
# number of case | ||
case = int(input()) | ||
|
||
for n in range(1, 11): | ||
tmp = list(map(int, input().strip().split())) | ||
vertex, edge, node_1, node_2 = tmp[0], tmp[1], tmp[2], tmp[3] | ||
|
||
tmp = list(map(int, input().strip().split())) | ||
|
||
dict = {1: 0} | ||
|
||
for i in range(0, edge * 2, 2): | ||
dict[tmp[i + 1]] = tmp[i] | ||
|
||
print(dict) | ||
route1 = build(dict, node_1) | ||
route2 = build(dict, node_2) | ||
|
||
result_node = compare_route(route1, route2) | ||
|
||
print("#" + str(n) + " " + str(result_node) + " " + str(subtree_size(dict, result_node))) | ||
|
||
|
||
|