CF1889E.Doremy's Swapping Trees
NOI/NOI+/CTSC
通过率:0%
时间限制:2.00s
内存限制:1024MB
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.
考虑两个无向图 G1 和 G2。G1 和 G2 中的每个节点均有一个标签。Doremy 称 G1 和 G2 相似,当且仅当满足以下条件:
- G1 中所有节点的标签互不相同,且 G2 中所有节点的标签也互不相同;
- G1 的标签集合 S 与 G2 的标签集合完全相同;
- 对于 S 中任意两个不同的标签 u 和 v,它们在 G1 中对应的节点处于同一连通分量,当且仅当它们在 G2 中对应的节点也处于同一连通分量。
现在 Doremy 给你两棵含 n 个节点的树 T1 和 T2,节点标签为 1 到 n。你可以执行如下操作任意多次:
- 从 T1 中选取一个边集 E1,从 T2 中选取一个边集 E2,使得 E1 和 E2 相似。其中 E 表示仅保留原树 T 中边集 E 所构成的图(即边导出子图);换言之,E 是从 T 中删去所有不属于 E 的边,并进一步删去所有孤立顶点后所得的图。
- 将 T1 中的边集 E1 与 T2 中的边集 E2 交换。
现在 Doremy 想知道:经过任意次上述操作后,能获得多少种互不相同的 T1?你能帮她求出答案吗?请将答案对 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.
输入包含多个测试用例。第一行包含一个整数 t(1≤t≤2⋅104),表示测试用例的数量。随后是各测试用例的描述。
第一行包含一个整数 n(2≤n≤105),表示树 T1 和 T2 的节点数量。
接下来的 n−1 行中,每行包含两个整数 u,v(1≤u,v≤n),表示树 T1 中的一条无向边。保证这些边构成一棵树。
再接下来的 n−1 行中,每行包含两个整数 u,v(1≤u,v≤n),表示树 T2 中的一条无向边。保证这些边构成一棵树。
保证所有测试用例的 n 之和不超过 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.
对于每个测试用例,你应该输出一行一个整数,表示经过任意次操作后不同的 T1 的数量,对 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.

在第一个测试用例中,至多存在一个不同的 T1,其唯一边为 (1,2)。
在第二个测试用例中,你可以在 T1 中选择边集 (1,3),(2,3),在 T2 中选择边集 (1,2),(2,3),并交换它们。因此 T1 可以是 1−3−2 或 1−2−3。
在第三个测试用例中,共有 4 个不同的 T1,如下图所示。

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