A12995 | Send Tree to Charlie
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $n$ nodes (numbered $1$ to $n$ ) and $n-1$ edges. Initially, Bob placed a decoration with label $i$ on node $i$ , for each $1 \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-1$ edges in some order.
- Then, he considered the edges one by one in that order. For each edge $(u, v)$ , he swapped the decorations of node $u$ with the one of node $v$ .
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 $1000000007$ ( $10^9+7$ ). Note that, it is possible that there exists no possible configurations.
- First, he listed the $n-1$ edges in some order.
- Then, he considered the edges one by one in that order. For each edge $(u, v)$ , he swapped the decorations of node $u$ with the one of node $v$ .
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 $1000000007$ ( $10^9+7$ ). Note that, it is possible that there exists no possible configurations.
输入格式
The first line contains a single integer $n$ ( $2 \leq n \leq 500\,000$ ) — the number of nodes.
Each of the next $n - 1$ lines contains two integers $u$ and $v$ ( $1 \leq u, v \leq n$ ), denoting that there is an edge connecting two nodes $u$ and $v$ . It is guaranteed that the given edges form a tree.
The last line contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $0 \le a_i \le n$ ). For each $i$ , $a_i = 0$ means that the decoration of node $i$ has been dropped on the floor. Otherwise, $a_i$ is the label of the decoration of node $i$ . It is guaranteed that no label appears more than once.
Each of the next $n - 1$ lines contains two integers $u$ and $v$ ( $1 \leq u, v \leq n$ ), denoting that there is an edge connecting two nodes $u$ and $v$ . It is guaranteed that the given edges form a tree.
The last line contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $0 \le a_i \le n$ ). For each $i$ , $a_i = 0$ means that the decoration of node $i$ has been dropped on the floor. Otherwise, $a_i$ is the label of the decoration of node $i$ . It is guaranteed that no label appears more than once.
输出格式
Output the number of possible configurations modulo $1000000007$ ( $10^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]$ and $[3, 4, 2, 1]$ .
In the second example, note that while there are $4! = 24$ possible permutations of the edges, each of them results in a possible configuration, there are only $12$ different configurations.
In the third example, it is easy to see that the decoration $1$ cannot stay at node $1$ after the swaps.
In the second example, note that while there are $4! = 24$ possible permutations of the edges, each of them results in a possible configuration, there are only $12$ different configurations.
In the third example, it is easy to see that the decoration $1$ cannot stay at node $1$ after the swaps.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted