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()