CF1800G.Symmetree

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Kid was gifted a tree of nn vertices with the root in the vertex 11. Since he really like symmetrical objects, Kid wants to find out if this tree is symmetrical.

For example, the trees in the picture above are symmetrical.

And the trees in this picture are not symmetrical.

Formally, a tree is symmetrical if there exists an order of children such that:

  • The subtree of the leftmost child of the root is a mirror image of the subtree of the rightmost child;
  • the subtree of the second-left child of the root is a mirror image of the subtree of the second-right child of the root;
  • ...
  • if the number of children of the root is odd, then the subtree of the middle child should be symmetrical.

Kid 收到一棵包含 nn 个顶点的树,根节点为顶点 11。由于他非常喜欢对称的物体,Kid 想要判断这棵树是否对称。

例如,上图中的树是对称的。

而此图中的树则不对称。

形式化地,一棵树是对称的,当且仅当存在一种子节点的排列顺序,使得:

  • 根节点最左侧子节点的子树与最右侧子节点的子树互为镜像;
  • 根节点第二左子节点的子树与第二右子节点的子树互为镜像;
  • ……
  • 若根节点的子节点数目为奇数,则中间那个子节点所对应的子树本身必须是对称的。

输入格式

The first line of input data contains single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases in the test.

The first line of each case contains an integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the number of vertices in the tree.

The following n−1n-1 lines contain two integers each uu and vv (1≤u,v≤n1 \le u, v \le n, u≠vu \neq v) — indices of vertices connected by an edge.

It is guaranteed that the sum of nn over all cases does not exceed 2⋅1052 \cdot 10^5.

输入数据的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 表示树中顶点的数量。

接下来的 n−1n-1 行每行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n,且 u≠vu \neq v)—— 表示由一条边连接的两个顶点的编号。

保证所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

Output tt strings, each of which is the answer to the corresponding test case. As an answer, output "YES" if this tree is symmetrical, and "NO" otherwise.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

输出 tt 个字符串,每个字符串对应一个测试用例的答案。若该树是轴对称的,则输出 "YES";否则输出 "NO"。

你可以以任意大小写形式输出答案(例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 均会被识别为肯定回答)。

输入输出样例

  • 输入#1

    6
    6
    1 5
    1 6
    1 2
    2 3
    2 4
    7
    1 5
    1 3
    3 6
    1 4
    4 7
    4 2
    9
    1 2
    2 4
    2 3
    3 5
    1 7
    7 6
    7 8
    8 9
    10
    2 9
    9 10
    2 3
    6 7
    4 3
    1 2
    3 8
    2 5
    6 5
    10
    3 2
    8 10
    9 7
    4 2
    8 2
    2 1
    4 5
    6 5
    5 7
    1

    输出#1

    YES
    NO
    YES
    NO
    NO
    YES

输入解题思路,AI测评打分。不知道怎么写?

首页