Tree Isomorphism

시간 제한1초메모리 제한1024 MB

요약
두 개의 트리가 주어질 때, 첫 번째 트리의 정수 이름을 바꾸어 두 번째 트리와 정확히 일치하게 만들 수 있는지 판정하고, 가능하면 그 이름 변경을 출력하는 문제다. 트리의 동형성(isomorphism)을 판정하고 구체적인 대응을 구성해야 한다.
난이도

어려움10점 중 8점

유형
트리, DFS, 해시맵, 정렬
정답자
아직 제출이 없습니다

문제

Today's assignment in art class was drawing labeled trees. To draw a labeled tree you first draw the integers 0…N−10 \ldots N-1 on a sheet of paper and then connect them by drawing N−1N-1 lines between some pairs of them. The lines must be drawn so that it is possible to get from any integer to any other by following the lines.

Two pupils drew trees with the exact same number of integers, which made the teacher suspect plagiarism. Namely, she thinks that one student copied the other's tree and just changed some of the integers, leaving the lines as is.

Write a program that, given two trees of size NN, will print whether it is possible to turn the first tree into the second one by changing some (possibly none or all) integers. If it is possible, then it must also print the changes.

입력

The first line of input contains NN---the number of integers in each tree (1≤N≤100,0001 \le N \le 100\\,000). Each of the next N−1N-1 lines contains two integers ss and tt which signify that the first tree has a line between integers ss and tt (0≤s<t<N0 \le s < t < N). Similarly, each of the following N−1N-1 lines contains two integers uu and vv which signify that the second tree has a line between integers uu and vv (0≤u<v<N0 \le u < v < N).

출력

The first line of output must contain EI if it is not possible to turn the first tree into the second. Otherwise, the first line must contain JAH and each of the following NN lines must contain one integer p_ip\_i, which signifies that the integer ii in the first tree must be changed into p_ip\_i.

예제2

  1. 예제 1

    입력
    5
    0 3
    3 4
    2 4
    1 4
    3 4
    0 2
    1 3
    0 3
    
    예상 출력
    JAH
    2
    1
    4
    0
    3
    
  2. 예제 2

    입력
    4
    0 1
    0 2
    0 3
    0 1
    0 2
    1 3
    
    예상 출력
    EI