提出 #25123784


ソースコード 拡げる

from collections import deque
from typing import List, Tuple

def adj_list_from_edges(n: int, edges: List[Tuple[int, int]], directed: bool = False):
    """
    辺 -> 隣接頂点のリスト
    """
    g = [[] for _ in range(n)]
    for u, v in edges:
        g[u - 1].append(v - 1)
        if not directed:
            g[v - 1].append(u - 1)

    return g

n = int(input())
edges = []
for i in range(n - 1):
    a, b = list(map(int, input().split()))
    edges.append((a, b))

def bfs(n, g):
    q = deque()
    flags = [False for _ in range(n)]
    ans = [None for _ in range(n)]
    q.append((0, True))
    ans[0] = True
    while len(q) > 0:
        u, col = q.popleft()
        ans[u] = col
        # print(f'{u}: {col}')
        for v in g[u]:
            if not flags[v]:
                flags[v] = True
                q.append((v, not col))

    # print(ans)

    col_true = [i for i in range(n) if ans[i]]
    col_false = [i for i in range(n) if not ans[i]]
    if len(col_false) >= len(col_true):
        return col_false[:n // 2]
    else:
        return col_true[:n // 2]

a = bfs(n, adj_list_from_edges(n, edges))
for i in a:
    print(i + 1, end=' ')

提出情報

提出日時
問題 026 - Independent Set on a Tree(★4)
ユーザ wakameTech
言語 PyPy3 (7.3.0)
得点 4
コード長 1215 Byte
結果 AC
実行時間 348 ms
メモリ 117432 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 4 / 4
結果
AC × 2
AC × 24
セット名 テストケース
Sample sample01.txt, sample02.txt
All corner01.txt, corner02.txt, max.txt, random00.txt, random01.txt, random02.txt, random03.txt, random04.txt, random05.txt, random06.txt, random07.txt, random08.txt, random09.txt, sample01.txt, sample02.txt, specific01.txt, specific02.txt, specific03.txt, specific04.txt, specific05.txt, specific06.txt, specific07.txt, specific08.txt, specific09.txt
ケース名 結果 実行時間 メモリ
corner01.txt AC 108 ms 74584 KiB
corner02.txt AC 88 ms 74580 KiB
max.txt AC 345 ms 108600 KiB
random00.txt AC 204 ms 84264 KiB
random01.txt AC 310 ms 100880 KiB
random02.txt AC 334 ms 108328 KiB
random03.txt AC 247 ms 92016 KiB
random04.txt AC 132 ms 77400 KiB
random05.txt AC 183 ms 82144 KiB
random06.txt AC 260 ms 94076 KiB
random07.txt AC 326 ms 108576 KiB
random08.txt AC 170 ms 80152 KiB
random09.txt AC 227 ms 88896 KiB
sample01.txt AC 84 ms 74548 KiB
sample02.txt AC 87 ms 75064 KiB
specific01.txt AC 341 ms 108524 KiB
specific02.txt AC 330 ms 117028 KiB
specific03.txt AC 348 ms 108696 KiB
specific04.txt AC 331 ms 114340 KiB
specific05.txt AC 337 ms 109560 KiB
specific06.txt AC 326 ms 109440 KiB
specific07.txt AC 332 ms 117432 KiB
specific08.txt AC 347 ms 109044 KiB
specific09.txt AC 341 ms 109036 KiB