CF1889E.Doremy's Swapping Trees

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:1024MB

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.

考虑两个无向图 G1G_1 和 G2G_2。G1G_1 和 G2G_2 中的每个节点均有一个标签。Doremy 称 G1G_1 和 G2G_2 相似,当且仅当满足以下条件:

  • G1G_1 中所有节点的标签互不相同,且 G2G_2 中所有节点的标签也互不相同;
  • G1G_1 的标签集合 SS 与 G2G_2 的标签集合完全相同;
  • 对于 SS 中任意两个不同的标签 uu 和 vv,它们在 G1G_1 中对应的节点处于同一连通分量,当且仅当它们在 G2G_2 中对应的节点也处于同一连通分量。

现在 Doremy 给你两棵含 nn 个节点的树 T1T_1 和 T2T_2,节点标签为 11 到 nn。你可以执行如下操作任意多次:

  • 从 T1T_1 中选取一个边集 E1E_1,从 T2T_2 中选取一个边集 E2E_2,使得 E1‾\overline{E_1} 和 E2‾\overline{E_2} 相似。其中 E‾\overline{E} 表示仅保留原树 TT 中边集 EE 所构成的图(即边导出子图);换言之,E‾\overline{E} 是从 TT 中删去所有不属于 EE 的边,并进一步删去所有孤立顶点后所得的图。
  • 将 T1T_1 中的边集 E1E_1 与 T2T_2 中的边集 E2E_2 交换。

现在 Doremy 想知道:经过任意次上述操作后,能获得多少种互不相同的 T1T_1?你能帮她求出答案吗?请将答案对 109+710^9+7 取模后输出。

输入格式

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤2⋅1041\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 (2≤n≤1052\le n\le 10^5) — the number of nodes in the trees T1T_1 and T2T_2.

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

Each of the following n−1n-1 lines contain two integers u,vu,v (1≤u,v≤n1\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 2⋅1052\cdot 10^5.

输入包含多个测试用例。第一行包含一个整数 tt(1≤t≤2⋅1041\le t\le 2\cdot 10^4),表示测试用例的数量。随后是各测试用例的描述。

第一行包含一个整数 nn(2≤n≤1052\le n\le 10^5),表示树 T1T_1 和 T2T_2 的节点数量。

接下来的 n−1n-1 行中,每行包含两个整数 u,vu,v(1≤u,v≤n1\le u,v\le n),表示树 T1T_1 中的一条无向边。保证这些边构成一棵树。

再接下来的 n−1n-1 行中,每行包含两个整数 u,vu,v(1≤u,v≤n1\le u,v\le n),表示树 T2T_2 中的一条无向边。保证这些边构成一棵树。

保证所有测试用例的 nn 之和不超过 2⋅1052\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.

对于每个测试用例,你应该输出一行一个整数,表示经过任意次操作后不同的 T1T_1 的数量,对 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 1−3−21-3-2 or 1−2−31-2-3.

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

在第一个测试用例中,至多存在一个不同的 T1T_1,其唯一边为 (1,2)(1,2)。

在第二个测试用例中,你可以在 T1T_1 中选择边集 (1,3),(2,3){(1,3),(2,3)},在 T2T_2 中选择边集 (1,2),(2,3){(1,2),(2,3)},并交换它们。因此 T1T_1 可以是 1−3−21-3-2 或 1−2−31-2-3。

在第三个测试用例中,共有 44 个不同的 T1T_1,如下图所示。

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

首页