목차

Kingpin Escape

ps
링크acmicpc.net/…
출처BOJ
문제 번호16314
문제명Kingpin Escape
레벨플래티넘 2
분류

BCC

시간복잡도O(n)
인풋사이즈n<=10^5
사용한 언어Python 3.11
제출기록51000KB / 336ms
최고기록336ms
해결날짜2023/03/11

풀이

코드

"""Solution code for "BOJ 16314. Kingpin Escape".

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

Tags: [graph]
"""

import sys
from teflib import graph as tgraph


def main():
    n, h = [
        int(x) for x in sys.stdin.readline().split()
    ]  # pylint: disable=unused-variable
    tree = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = [int(x) for x in sys.stdin.readline().split()]
        tree[a].append(b)
        tree[b].append(a)

    leaves = [u for u in tgraph.dfs(tree) if len(tree[u]) == 1]

    print((len(leaves) + 1) // 2)
    for u, v in zip(leaves, leaves[(len(leaves) + 1) // 2 :]):
        print(u, v)
    if len(leaves) % 2 == 1:
        print(leaves[len(leaves) // 2], leaves[0])


if __name__ == '__main__':
    main()