提出 #29616126


ソースコード 拡げる

def EulerTour(n, graph, root):
    """
    (n: int, graph: List[List[int]], root: int) -> Tuple[List[int], List[int], List[int]]:

    :param n: the number of vertex points
    :param graph: 2D matrix of N vertices given by the edges
    :param root: start node index

    :return tour: order of visited vertex
    :return in_time: first visiting time of each vertex
    :return out_time: last visiting time of each vertex

    example:
    graph = [[] for _ in range(n)]
    for _ in range(n):
        a, b = map(int, input().split())
        graph[a].append(b)
        graph[b].append(a)

    tour, in_time, out_time = EulerTour(n, graph, 0)
    """

    parent = [-1] * n
    stack = [~root, root]  # postorder, preorder
    curr_time = -1
    tour = []
    in_time = [-1] * n
    out_time = [-1] * n
    while stack:
        curr_node = stack.pop()
        curr_time += 1
        if curr_node >= 0:  # preorder
            tour.append(curr_node)
            if in_time[curr_node] == -1:
                in_time[curr_node] = curr_time

            for next_node in graph[curr_node][::-1]:
                if next_node != parent[curr_node]:
                    parent[next_node] = curr_node
                    stack.append(~next_node)
                    stack.append(next_node)
        elif curr_node < 0:  # postorder
            out_time[~curr_node] = curr_time
            if parent[~curr_node] != -1:
                tour.append(parent[~curr_node])

    return tour, in_time, out_time


n = int(input())

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

for i in range(n):
    graph[i] = sorted(graph[i])

tour, in_time, out_time = EulerTour(n, graph, 0)

tour = [i + 1 for i in tour]
print(*tour)

提出情報

提出日時
問題 D - Takahashi Tour
ユーザ cocoinit23
言語 PyPy3 (7.3.0)
得点 400
コード長 1883 Byte
結果 AC
実行時間 644 ms
メモリ 187684 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 400 / 400
結果
AC × 2
AC × 17
セット名 テストケース
Sample sample_01.txt, sample_02.txt
All hand_01.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, sample_01.txt, sample_02.txt
ケース名 結果 実行時間 メモリ
hand_01.txt AC 61 ms 62036 KiB
random_01.txt AC 436 ms 139736 KiB
random_02.txt AC 625 ms 167584 KiB
random_03.txt AC 133 ms 81232 KiB
random_04.txt AC 644 ms 167480 KiB
random_05.txt AC 162 ms 88928 KiB
random_06.txt AC 620 ms 165744 KiB
random_07.txt AC 628 ms 180384 KiB
random_08.txt AC 630 ms 185680 KiB
random_09.txt AC 613 ms 167200 KiB
random_10.txt AC 618 ms 166692 KiB
random_11.txt AC 550 ms 184684 KiB
random_12.txt AC 523 ms 184536 KiB
random_13.txt AC 565 ms 187684 KiB
random_14.txt AC 558 ms 184208 KiB
sample_01.txt AC 48 ms 61984 KiB
sample_02.txt AC 52 ms 62068 KiB