CF1889E.Doremy's Swapping Trees
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Consider two undirected graphs G1 and G2 . Every node in G1 and in G2 has a label. Doremy calls G1 and G2 similar if and only if:
- The labels in G1 are distinct, and the labels in G2 are distinct.
- The set S of labels in G1 coincides with the set of labels in G2 .
- For every pair of two distinct labels u and v in S , the corresponding nodes are in the same connected component in G1 if and only if they are in the same connected component in G2 .
Now Doremy gives you two trees T1 and T2 with n nodes, labeled from 1 to n . You can do the following operation any number of times:
- Choose an edge set E1 from T1 and an edge set E2 from T2 , such that E1 and E2 are similar. Here E represents the graph which is given by only reserving the edge set E from T (i.e., the edge-induced subgraph). In other words, E is obtained from T by removing all edges not included in E and further removing all isolated vertices.
- Swap the edge set E1 in T1 with the edge set E2 in T2 .
Now Doremy is wondering how many distinct T1 you can get after any number of operations. Can you help her find the answer? Output the answer modulo 109+7 .
输入格式
The input consists of multiple test cases. The first line contains a single integer t ( 1≤t≤2⋅104 ) — the number of test cases. The description of the test cases follows.
The first line contains an integer n ( 2≤n≤105 ) — the number of nodes in the trees T1 and T2 .
Each of the following n−1 lines contain two integers u,v ( 1≤u,v≤n ), representing an undirected edge in T1 . It is guaranteed these edges form a tree.
Each of the following n−1 lines contain two integers u,v ( 1≤u,v≤n ), representing an undirected edge in T2 . It is guaranteed these edges form a tree.
It is guaranteed that the sum of n does not exceed 2⋅105 .
输出格式
For each test case, you should output a single line with an integer, representing the number of distinct T1 after any number of operations, modulo 109+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 T1 having the only edge (1,2) .
In the second test case, you can choose the edge set {(1,3),(2,3)} in T1 , the edge set {(1,2),(2,3)} in T2 and swap them. So T1 can be 1−3−2 or 1−2−3 .
In the third test case, there are 4 distinct T1 , as the following pictures.