提出 #10679447


ソースコード 拡げる

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
m = map(int,read().split())
AB = zip(m,m)

graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)

dist = [-1] * (N+1)
stack = [1]
dist[1] = 0
while stack:
    v = stack.pop()
    for w in graph[v]:
        if dist[w] == -1:
            dist[w] = dist[v] + 1
            stack.append(w)

ev = [i for i,x in enumerate(dist) if x % 2 == 0 and i > 0]
od = [i for i,x in enumerate(dist) if x % 2 == 1 and i > 0]

def make_perm(N, ev, od, x0, x1, x2, y0, y1, y2):
    assert len(ev) == x0 + x1 + x2
    assert len(od) == y0 + y1 + y2
    P = [0] * (N+1)
    for i in ev:
        if x0:
            P[i] = (x0 + y0) * 3
            x0 -= 1
            continue
        if x1:
            P[i] = (x1 + y1) * 3 - 2
            x1 -= 1
            continue
        if x2:
            P[i] = (x2 + y2) * 3 - 1
            x2 -= 1
            continue
    for i in od:
        if y0:
            P[i] = (x0 + y0) * 3
            y0 -= 1
            continue
        if y1:
            P[i] = (x1 + y1) * 3 - 2
            y1 -= 1
            continue
        if y2:
            P[i] = (x2 + y2) * 3 - 1
            y2 -= 1
            continue
    return P[1:]

def solve(N, ev, od):
    x0 = N // 3
    x1 = (N+2) // 3
    x2 = (N+1) // 3
    perm = [0] * (N+1)
    LE = len(ev)
    LO = len(od)
    if LE <= x0:
        return make_perm(N, ev, od, LE, 0, 0, x0-LE, x1, x2)
    if LO <= x0:
        return make_perm(N, ev, od, x0-LO, x1, x2, LO, 0, 0)
    if LE == x1:
        return make_perm(N, ev, od, 0, x1, 0, x0, 0, x2)
    if LO == x1:
        return make_perm(N, ev, od, x0, 0, x2, 0, x1, 0)
    # どちらも x1 よりたくさんある
    return make_perm(N, ev, od, LE-x1, x1, 0, LO-x2, 0, x2)

P = solve(N, ev, od)
print(' '.join(map(str, P)))

提出情報

提出日時
問題 C - ThREE
ユーザ maspy
言語 Python (3.4.3)
得点 600
コード長 2001 Byte
結果 AC
実行時間 785 ms
メモリ 73480 KiB

ジャッジ結果

セット名 All sample
得点 / 配点 600 / 600 0 / 0
結果
AC × 57
AC × 1
セット名 テストケース
All 00_sample_01, 20_small_01, 20_small_02, 20_small_03, 20_small_04, 20_small_05, 30_large_01, 30_large_02, 30_large_03, 40_line_01, 40_line_02, 40_line_03, 50_star_01, 50_star_02, 50_star_03, 60_hand_01, 60_hand_02, 60_hand_03, 60_hand_04, 60_hand_05, 60_hand_06, 60_hand_07, 60_hand_08, 60_hand_09, 60_hand_10, 60_hand_11, 60_hand_12, 60_hand_13, 60_hand_14, 60_hand_15, 60_hand_16, 60_hand_17, 60_hand_18, 60_hand_19, 60_hand_20, 60_hand_21, 60_hand_22, 60_hand_23, 60_hand_24, 60_hand_25, 60_hand_26, 60_hand_27, 60_hand_28, 60_hand_29, 60_hand_30, 70_dense_01, 70_dense_02, 70_dense_03, 70_dense_04, 70_dense_05, 70_dense_06, 70_dense_07, 70_dense_08, 70_dense_09, 70_dense_10, 90_hand_01, 90_hand_02
sample 00_sample_01
ケース名 結果 実行時間 メモリ
00_sample_01 AC 17 ms 3192 KiB
20_small_01 AC 18 ms 3192 KiB
20_small_02 AC 18 ms 3192 KiB
20_small_03 AC 18 ms 3192 KiB
20_small_04 AC 18 ms 3192 KiB
20_small_05 AC 18 ms 3192 KiB
30_large_01 AC 785 ms 73480 KiB
30_large_02 AC 704 ms 72096 KiB
30_large_03 AC 698 ms 73216 KiB
40_line_01 AC 399 ms 59860 KiB
40_line_02 AC 362 ms 43032 KiB
40_line_03 AC 255 ms 33568 KiB
50_star_01 AC 276 ms 39556 KiB
50_star_02 AC 321 ms 42432 KiB
50_star_03 AC 42 ms 6644 KiB
60_hand_01 AC 153 ms 23236 KiB
60_hand_02 AC 163 ms 22768 KiB
60_hand_03 AC 41 ms 6388 KiB
60_hand_04 AC 42 ms 6772 KiB
60_hand_05 AC 230 ms 31956 KiB
60_hand_06 AC 285 ms 37848 KiB
60_hand_07 AC 219 ms 30124 KiB
60_hand_08 AC 198 ms 28128 KiB
60_hand_09 AC 84 ms 12556 KiB
60_hand_10 AC 154 ms 22708 KiB
60_hand_11 AC 322 ms 41392 KiB
60_hand_12 AC 424 ms 51824 KiB
60_hand_13 AC 229 ms 31492 KiB
60_hand_14 AC 75 ms 11616 KiB
60_hand_15 AC 160 ms 23876 KiB
60_hand_16 AC 53 ms 8376 KiB
60_hand_17 AC 115 ms 17112 KiB
60_hand_18 AC 471 ms 55828 KiB
60_hand_19 AC 50 ms 8016 KiB
60_hand_20 AC 493 ms 58908 KiB
60_hand_21 AC 153 ms 22408 KiB
60_hand_22 AC 484 ms 58252 KiB
60_hand_23 AC 489 ms 57800 KiB
60_hand_24 AC 617 ms 72672 KiB
60_hand_25 AC 366 ms 45696 KiB
60_hand_26 AC 61 ms 9324 KiB
60_hand_27 AC 311 ms 40648 KiB
60_hand_28 AC 352 ms 45492 KiB
60_hand_29 AC 37 ms 6004 KiB
60_hand_30 AC 37 ms 5748 KiB
70_dense_01 AC 176 ms 25192 KiB
70_dense_02 AC 259 ms 31700 KiB
70_dense_03 AC 203 ms 27448 KiB
70_dense_04 AC 245 ms 31496 KiB
70_dense_05 AC 225 ms 30528 KiB
70_dense_06 AC 192 ms 26484 KiB
70_dense_07 AC 231 ms 29812 KiB
70_dense_08 AC 182 ms 25444 KiB
70_dense_09 AC 68 ms 10340 KiB
70_dense_10 AC 141 ms 20260 KiB
90_hand_01 AC 18 ms 3192 KiB
90_hand_02 AC 18 ms 3192 KiB