Submission #55347016
Source Code Expand
import sys
input = sys.stdin.readline
def find_centroid(T):
N = len(T)
size = [1] * N
st = [(0, 0, 0)]
while st:
v, p, t = st.pop()
if t == 0:
st.append((v, p, 1))
for c in T[v]:
if c != p:
st.append((c, v, 0))
else:
is_centroid = True
for c in T[v]:
if c != p:
size[v] += size[c]
if size[c] > N // 2:
is_centroid = False
if is_centroid and N - size[v] <= N // 2:
return v
N = int(input())
T = [[] for _ in range(N)]
for i in range(N - 1):
u, v = map(int, input().split())
u -= 1
v -= 1
T[u].append(v)
T[v].append(u)
g = find_centroid(T)
comp = [[] for _ in range(N)]
st = []
for v in T[g]:
st.append((v, g, v))
while st:
v, p, i = st.pop()
comp[i].append(v)
for c in T[v]:
if c != p:
st.append((c, v, i))
vs = []
for i in range(N):
vs += comp[i]
if N % 2 == 0:
vs.append(g)
for i in range(N // 2):
print(vs[i] + 1, vs[i + N // 2] + 1)
Submission Info
| Submission Time | |
|---|---|
| Task | F - Perfect Matching on a Tree |
| User | sotanishy |
| Language | Python (PyPy 3.10-v7.3.12) |
| Score | 550 |
| Code Size | 1198 Byte |
| Status | AC |
| Exec Time | 427 ms |
| Memory | 163396 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 550 / 550 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00_sample_01.txt, 00_sample_02.txt |
| All | 00_sample_01.txt, 00_sample_02.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt, 02_handmade_07.txt, 02_handmade_08.txt, 02_handmade_09.txt, 02_handmade_10.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00_sample_01.txt | AC | 59 ms | 76232 KiB |
| 00_sample_02.txt | AC | 60 ms | 76320 KiB |
| 01_random_01.txt | AC | 290 ms | 110916 KiB |
| 01_random_02.txt | AC | 370 ms | 122112 KiB |
| 01_random_03.txt | AC | 173 ms | 95104 KiB |
| 01_random_04.txt | AC | 371 ms | 123536 KiB |
| 01_random_05.txt | AC | 140 ms | 90868 KiB |
| 01_random_06.txt | AC | 389 ms | 123520 KiB |
| 01_random_07.txt | AC | 169 ms | 96044 KiB |
| 01_random_08.txt | AC | 392 ms | 122992 KiB |
| 01_random_09.txt | AC | 236 ms | 108180 KiB |
| 01_random_10.txt | AC | 368 ms | 123720 KiB |
| 01_random_11.txt | AC | 273 ms | 108836 KiB |
| 01_random_12.txt | AC | 393 ms | 123984 KiB |
| 01_random_13.txt | AC | 199 ms | 102424 KiB |
| 01_random_14.txt | AC | 386 ms | 123200 KiB |
| 01_random_15.txt | AC | 171 ms | 96128 KiB |
| 01_random_16.txt | AC | 392 ms | 121816 KiB |
| 01_random_17.txt | AC | 126 ms | 89376 KiB |
| 01_random_18.txt | AC | 384 ms | 122680 KiB |
| 01_random_19.txt | AC | 373 ms | 120416 KiB |
| 01_random_20.txt | AC | 379 ms | 128132 KiB |
| 02_handmade_01.txt | AC | 420 ms | 125580 KiB |
| 02_handmade_02.txt | AC | 427 ms | 127644 KiB |
| 02_handmade_03.txt | AC | 330 ms | 162860 KiB |
| 02_handmade_04.txt | AC | 342 ms | 163396 KiB |
| 02_handmade_05.txt | AC | 362 ms | 127408 KiB |
| 02_handmade_06.txt | AC | 401 ms | 125096 KiB |
| 02_handmade_07.txt | AC | 384 ms | 128720 KiB |
| 02_handmade_08.txt | AC | 397 ms | 123284 KiB |
| 02_handmade_09.txt | AC | 410 ms | 125112 KiB |
| 02_handmade_10.txt | AC | 406 ms | 124492 KiB |