6987.Tree Isomorphism II

Time Limit: 1s Memory Limit: 512MB

Given two (not rooted) trees, your task is to find out if they are isomorphic , i.e., it is possible to draw them so that they look the same.

Input Format(From the terminal/stdin)

The first input line has an integer $t$ : the number of tests. Then, there are $t$ tests described as follows:

The first line has an integer $n$ : the number of nodes in both trees. The nodes are numbered $1,2,\dots,n$ .

Then, there are $n-1$ lines describing the edges of the first tree, and finally $n-1$ lines describing the edges of the second tree.

  • $1 \le t \le 1000$
  • $2 \le n \le 10^5$
  • the sum of all values of $n$ is at most $10^5$

Output Format(To the terminal/stdout)

For each test, print "YES", if the trees are isomorphic, and "NO" otherwise.

Sample Input

Copy
2
3
1 2
2 3
1 2
1 3
3
1 2
2 3
1 3
3 2
 \n
 \n
 · \n
 · \n
 · \n
 · \n
 \n
 · \n
 · \n
 · \n
 · \n

Sample Output

Copy
YES
YES
   \n
   \n
Source: CSES, Advanced Graph Problems, 1701

Submit

请先 登录

© 2025 FAQs