https://www.acmicpc.net/problem/5021
import sys
def init():
ipt = sys.stdin.readline
n, m = map(int, ipt().split())
king = ipt().rstrip()
adj_dict = dict()
for _ in range(n):
c, p1, p2 = ipt().split()
for name in [c, p1, p2]:
if name not in adj_dict:
adj_dict[name] = []
adj_dict[c] = [p1, p2]
candidates = [ipt().rstrip() for _ in range(m)]
return candidates, adj_dict, king
def dfs(curr):
if curr == king:
return 1
if curr not in adj_dict:
return 0
if not adj_dict[curr]:
return 0
return (dfs(adj_dict[curr][0]) + dfs(adj_dict[curr][1])) / 2
candidates, adj_dict, king = init()
max_blood = float('-inf')
max_name = None
for name in candidates:
blood = dfs(name)
if blood > max_blood:
max_blood = blood
max_name = name
print(max_name)