CF1889E.Doremy's Swapping Trees

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Consider two undirected graphs G1G_1 and G2G_2 . Every node in G1G_1 and in G2G_2 has a label. Doremy calls G1G_1 and G2G_2 similar if and only if:

  • The labels in G1G_1 are distinct, and the labels in G2G_2 are distinct.
  • The set SS of labels in G1G_1 coincides with the set of labels in G2G_2 .
  • For every pair of two distinct labels uu and vv in SS , the corresponding nodes are in the same connected component in G1G_1 if and only if they are in the same connected component in G2G_2 .

Now Doremy gives you two trees T1T_1 and T2T_2 with nn nodes, labeled from 11 to nn . You can do the following operation any number of times:

  • Choose an edge set E1E_1 from T1T_1 and an edge set E2E_2 from T2T_2 , such that E1\overline{E_1} and E2\overline{E_2} are similar. Here E\overline{E} represents the graph which is given by only reserving the edge set EE from TT (i.e., the edge-induced subgraph). In other words, E\overline{E} is obtained from TT by removing all edges not included in EE and further removing all isolated vertices.
  • Swap the edge set E1E_1 in T1T_1 with the edge set E2E_2 in T2T_2 .

Now Doremy is wondering how many distinct T1T_1 you can get after any number of operations. Can you help her find the answer? Output the answer modulo 109+710^9+7 .

输入格式

The input consists of multiple test cases. The first line contains a single integer tt ( 1t21041\le t\le 2\cdot 10^4 ) — the number of test cases. The description of the test cases follows.

The first line contains an integer nn ( 2n1052\le n\le 10^5 ) — the number of nodes in the trees T1T_1 and T2T_2 .

Each of the following n1n-1 lines contain two integers u,vu,v ( 1u,vn1\le u,v\le n ), representing an undirected edge in T1T_1 . It is guaranteed these edges form a tree.

Each of the following n1n-1 lines contain two integers u,vu,v ( 1u,vn1\le u,v\le n ), representing an undirected edge in T2T_2 . It is guaranteed these edges form a tree.

It is guaranteed that the sum of nn does not exceed 21052\cdot 10^5 .

输出格式

For each test case, you should output a single line with an integer, representing the number of distinct T1T_1 after any number of operations, modulo 109+710^9+7 .

输入输出样例

  • 输入#1

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

    输出#1

    1
    2
    4

说明/提示

In the first test case, there is at most one distinct T1T_1 having the only edge (1,2)(1,2) .

In the second test case, you can choose the edge set {(1,3),(2,3)}\{(1,3),(2,3)\} in T1T_1 , the edge set {(1,2),(2,3)}\{(1,2),(2,3)\} in T2T_2 and swap them. So T1T_1 can be 1321-3-2 or 1231-2-3 .

In the third test case, there are 44 distinct T1T_1 , as the following pictures.

首页