아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Hamilton

시간 제한2초메모리 제한512 MB

요약
부모 포인터로 주어진 트리에서 연속한 마을 사이 거리가 3 이하이면서 모든 마을을 정확히 한 번씩 방문하는 해밀턴 경로를 찾거나, 불가능하면 NO를 출력한다.
난이도

보통10점 중 7점

유형
트리, DFS, 그리디, 동적 계획법
정답자
아직 제출이 없습니다

문제

Tracker Smurf is planning his trip for next holidays.  He wants to spend exactly one night in each of the villages in SmurfLand. His trip can start and end in any village.  Villages in SmurfLand are connected by roads in such a way that there's exactly one path between any two villages.  The distance between any two directly connected villages is exactly one kilometer.  Tracker is so fast that he can travel up to three kilometers each day, but he is still not sure if that's enough to be able to spend a night in each village exactly once.  Help him find the answer.

입력

First line of input contains an integer nn (1≤n≤1051 \leq n \leq 10^5) -- the number of villages in SmurfLand. The next n−1n-1 lines describe the roads.  iith input line (i∈2,…,ni \in \\{ 2, \ldots, n \\}) contains an integer p_ip\_i (1≤p_i<i1 \leq p\_i < i) which means that there is a road connecting villages ii and p_ip\_i.

출력

On a single line output nn integers q_1,q_2,…,q_nq\_1, q\_2, \ldots, q\_n (1≤q_i≤n1 \leq q\_i \leq n) specifying the sequence of villages for Tracker to spend the nights in (Tracker starts in village q_1q\_1 then goes to village q_2q\_2 and so on, finishing in village q_nq\_n). If it is not possible to plan Tracker's trip then on a single line output the word "NO" (without quotes).

예제1

  1. 예제 1

    입력
    8
    1
    2
    3
    4
    4
    4
    7
    
    예상 출력
    1
    3
    5
    6
    7
    8
    4
    2