#K53832. Isomorphic Trees

    ID: 29619 Type: Default 1000ms 256MiB

Isomorphic Trees

Isomorphic Trees

In this problem, you are given two trees, and your task is to determine whether the two trees are isomorphic. Two trees are isomorphic if there exists a one‐to‐one correspondence between their nodes, preserving the parent-child relationships. The trees are provided as edge lists and are to be considered rooted at node (1).

A common approach to solve this problem is to compute a canonical representation of the tree. For example, one can define the canonical form of a tree rooted at (r) recursively as follows:

[ \text{canon}(r) = \Big( \text{sort}({\text{canon}(c) : c \text{ is a child of } r}) \Big) ]

If the canonical forms of the two trees are identical, then the trees are isomorphic. Otherwise, they are not. Your solution should read input from standard input (stdin) and output the results to standard output (stdout).

inputFormat

The input begins with an integer (T) (the number of test cases). Each test case consists of the following:

  1. An integer (N_1) representing the number of nodes in the first tree, followed by (N_1 - 1) lines each containing two integers (u) and (v) representing an edge of the first tree.
  2. An integer (N_2) representing the number of nodes in the second tree, followed by (N_2 - 1) lines each containing two integers representing an edge of the second tree.

    Note: Nodes are numbered starting from 1. All input is read from stdin.

outputFormat

For each test case, print a single line with the word "Yes" if the two trees are isomorphic, or "No" otherwise. Output should be written to stdout.## sample

2
3
1 2
1 3
3
1 2
1 3
4
1 2
2 3
3 4
4
1 2
1 3
1 4
Yes

No

</p>