CF1254E.Send Tree to Charlie

NOI/NOI+/CTSC

通过率:0%

时间限制:4.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Christmas was knocking on the door, and our protagonist, Bob, was preparing a spectacular present for his long-time second best friend Charlie. As chocolate boxes are lame, he decided to decorate a tree instead. Bob's tree can be represented as an undirected connected graph with nn nodes (numbered 11 to nn) and n−1n-1 edges. Initially, Bob placed a decoration with label ii on node ii, for each 1≤i≤n1 \le i \le n. However, as such a simple arrangement is lame, he decided to shuffle the decorations a bit. Formally, Bob did the following steps:

  • First, he listed the n−1n-1 edges in some order.
  • Then, he considered the edges one by one in that order. For each edge (u,v)(u, v), he swapped the decorations of node uu with the one of node vv.

After finishing, Bob seemed satisfied with the arrangement, so he went to sleep.

The next morning, Bob wakes up only to find out that his beautiful arrangement has been ruined! Last night, Bob's younger brother Bobo dropped some of the decorations on the floor while he was playing with the tree. Fortunately, no decorations were lost, so Bob can repair the tree in no time. However, he completely forgets how the tree looked like yesterday. Therefore, given the labels of the decorations still on the tree, Bob wants to know the number of possible configurations of the tree. As the result can be quite large, Bob will be happy if you can output the result modulo 10000000071000000007 (109+710^9+7). Note that, it is possible that there exists no possible configurations.

圣诞节即将到来,我们的主角鲍勃(Bob)正在为他多年来的第二好朋友查理(Charlie)准备一份惊艳的礼物。由于巧克力礼盒太过普通,他决定改用一棵树来装饰。鲍勃的这棵树可被表示为一个具有 nn 个节点(编号为 11 至 nn)和 n−1n-1 条边的无向连通图。最初,鲍勃在节点 ii 上放置了一个标号为 ii 的装饰品,其中 1≤i≤n1 \le i \le n。然而,如此简单的布置太过普通,于是他决定对装饰品稍作打乱。具体而言,鲍勃执行了以下步骤:

  • 首先,他以某种顺序列出了全部 n−1n-1 条边;
  • 然后,他按该顺序依次考虑每条边 (u,v)(u, v);对于每条边 (u,v)(u, v),他将节点 uu 与节点 vv 上的装饰品互换。

完成所有操作后,鲍勃对最终的布置感到满意,便去睡觉了。

第二天早晨,鲍勃醒来却发现他精心布置的树已被毁!原来昨晚鲍勃的弟弟鲍博(Bobo)在玩这棵树时,不小心将部分装饰品碰落在地。幸运的是,没有任何装饰品丢失,因此鲍勃可以很快修复这棵树。但他完全忘记了昨天树的布置方式。因此,给定目前仍留在树上的装饰品标号,鲍勃想知道这棵树可能的布置方案数。由于结果可能非常大,你只需输出结果对 10000000071000000007(即 109+710^9+7)取模的值即可。注意:有可能不存在任何可行的布置方案。

输入格式

The first line contains a single integer nn (2≤n≤500 0002 \leq n \leq 500\,000) — the number of nodes.

Each of the next n−1n - 1 lines contains two integers uu and vv (1≤u,v≤n1 \leq u, v \leq n), denoting that there is an edge connecting two nodes uu and vv. It is guaranteed that the given edges form a tree.

The last line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai≤n0 \le a_i \le n). For each ii, ai=0a_i = 0 means that the decoration of node ii has been dropped on the floor. Otherwise, aia_i is the label of the decoration of node ii. It is guaranteed that no label appears more than once.

第一行包含一个整数 nn(2≤n≤500 0002 \leq n \leq 500\,000)—— 节点的数量。

接下来的 n−1n - 1 行,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \leq u, v \leq n),表示节点 uu 与节点 vv 之间存在一条边。保证所给的边构成一棵树。

最后一行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai≤n0 \le a_i \le n)。对每个 ii,若 ai=0a_i = 0,表示节点 ii 的装饰物已掉落在地上;否则,aia_i 表示节点 ii 的装饰物的编号。保证任意编号至多出现一次。

输出格式

Output the number of possible configurations modulo 10000000071000000007 (109+710^9+7).

输出可能的配置数对 10000000071000000007(即 109+710^9+7)取模的结果。

输入输出样例

  • 输入#1

    4
    3 4
    2 4
    4 1
    0 4 0 0

    输出#1

    2
  • 输入#2

    5
    1 2
    2 4
    3 4
    5 4
    0 0 0 0 0

    输出#2

    12
  • 输入#3

    3
    1 2
    1 3
    1 0 0

    输出#3

    0

说明/提示

In the first example, the possible configurations of the tree are [2,4,1,3][2, 4, 1, 3] and [3,4,2,1][3, 4, 2, 1].

In the second example, note that while there are 4!=244! = 24 possible permutations of the edges, each of them results in a possible configuration, there are only 1212 different configurations.

In the third example, it is easy to see that the decoration 11 cannot stay at node 11 after the swaps.

在第一个例子中,树的可能构型为 [2,4,1,3][2, 4, 1, 3] 和 [3,4,2,1][3, 4, 2, 1]。

在第二个例子中,注意:尽管边共有 4!=244! = 24 种可能的排列,且每种排列均对应一种可能的构型,但其中仅有 1212 种不同的构型。

在第三个例子中,容易看出装饰编号 11 在交换操作后无法停留在节点 11 上。

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

首页