Submission #29616126


Source Code Expand

Copy
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)
"""
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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)

Submission Info

Submission Time
Task D - Takahashi Tour
User cocoinit23
Language PyPy3 (7.3.0)
Score 400
Code Size 1883 Byte
Status AC
Exec Time 644 ms
Memory 187684 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 2
AC × 17
Set Name Test Cases
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
Case Name Status Exec Time Memory
hand_01.txt AC 61 ms 62036 KB
random_01.txt AC 436 ms 139736 KB
random_02.txt AC 625 ms 167584 KB
random_03.txt AC 133 ms 81232 KB
random_04.txt AC 644 ms 167480 KB
random_05.txt AC 162 ms 88928 KB
random_06.txt AC 620 ms 165744 KB
random_07.txt AC 628 ms 180384 KB
random_08.txt AC 630 ms 185680 KB
random_09.txt AC 613 ms 167200 KB
random_10.txt AC 618 ms 166692 KB
random_11.txt AC 550 ms 184684 KB
random_12.txt AC 523 ms 184536 KB
random_13.txt AC 565 ms 187684 KB
random_14.txt AC 558 ms 184208 KB
sample_01.txt AC 48 ms 61984 KB
sample_02.txt AC 52 ms 62068 KB


2025-04-05 (Sat)
20:15:20 +00:00