목차

Go Go Gorelians

ps
링크acmicpc.net/…
출처BOJ
문제 번호4618
문제명Go Go Gorelians
레벨골드 3
분류

트리의 지름

시간복잡도O(n^2)
인풋사이즈n<=1000
사용한 언어Python 3.13
제출기록36604KB / 200ms
최고기록200ms
해결날짜2025/10/31

풀이

코드

"""Solution code for "BOJ 4618. Go Go Gorelians".

- Problem link: https://www.acmicpc.net/problem/4618
- Solution link: http://www.teferi.net/ps/problems/boj/4618

Tags: [tree diameter]
"""

import math
import sys

from teflib import psutils
from teflib import tree as ttree


@psutils.run_until_all_zero
def main():
    N = int(sys.stdin.readline())
    tree = [[] for _ in range(N)]
    coords, ids = [], []
    for u in range(N):
        ID, *coord = [int(x) for x in sys.stdin.readline().split()]
        coords.append(coord)
        ids.append(ID)
        if u == 0:
            continue
        p = min(range(u), key=lambda x: math.dist(coord, coords[x]))
        tree[u].append(p)
        tree[p].append(u)

    answer = sorted(ids[u] for u in ttree.DistanceMeasures(tree).centers)
    print(*answer)


if __name__ == '__main__':
    main()