Anti-Plagiarism

시간 제한5초메모리 제한2048 MB

요약
각 트리 쌍마다 큰 트리가 작은 트리를 부분그래프로 포함하는지, 즉 부분트리 동형인지 판정한다.
난이도

어려움10점 중 9점

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

문제

As a homework, the teacher asked all the students of the art class to draw a beautiful, and most importantly original, tree. After everyone has submitted their work, the teacher began to suspect some students of cheating.

The teacher considers a tree T_1T\_1 to be copied from a tree T_2T\_2 if it is possible to add some (possibly zero) vertices and edges to T_2T\_2 and relabel its vertices so that it becomes the same as T_1T\_1.

In total, she suspects tt pairs of students. For each given pair of trees, check if first tree could be copied from the second tree.

입력

The first line contains an integer tt (1≤t≤1041 \le t \le 10^4): the number of suspicious pairs of students.

After that, there are tt descriptions of pairs of trees.

The first line of each description contains an integer nn (2≤n≤1052 \le n \le 10^5). Each of the next n−1n-1 lines contains two integers uu and vv (1≤u,v≤n1 \le u, v \le n): the edges of the first student's tree.

The next line of each description contains an integer mm (2≤m≤n2 \le m \le n). Each of the next m−1m-1 lines contains two integers uu and vv (1≤u,v≤m1 \le u, v \le m): the edges of the second student's tree.

It is guaranteed that the sum of nn over all pairs of students does not exceed 5⋅1055 \cdot 10^5, and the sum of n⋅mn \cdot m does not exceed 10710^7.

출력

For each of the tt pairs of trees, print a line containing a single word (case-insensitive): "Yes" if the first tree could be copied from the second tree, or "No" otherwise.

예제1

  1. 예제 1

    입력
    2
    5
    1 2
    1 5
    2 3
    2 4
    4
    1 2
    1 3
    1 4
    6
    1 2
    1 3
    1 4
    5 1
    6 1
    4
    1 2
    2 3
    3 4
    
    예상 출력
    Yes
    No