-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathE.cpp
106 lines (99 loc) · 1.83 KB
/
E.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <bits/stdc++.h>
using namespace std;
const int mxN=5e5, M=1e9+7;
int n, p[mxN], d[mxN], a[mxN], b[mxN], g1[mxN], g2[mxN], vis[mxN];
vector<int> adj[mxN], adj2[mxN];
vector<array<int, 2>> e[mxN];
bool c1[mxN], c2[mxN];
long long ans=1;
void fk() {
cout << 0;
exit(0);
}
void dfs(int u=0, int pu=-1) {
p[u]=pu;
d[u]=~pu?d[pu]+1:0;
for(int v : adj[u])
if(v^pu)
dfs(v, u);
}
void pr(int u, int v) {
vector<int> w1, w2;
while(u^v) {
if(d[u]>d[v]) {
if(c1[u])
fk();
c1[u]=1;
w1.push_back(u);
++b[u];
u=p[u];
} else {
if(c2[v])
fk();
c2[v]=1;
w2.push_back(v);
++b[v];
v=p[v];
}
}
++b[u];
w1.push_back(u);
w1.insert(w1.end(), w2.rbegin(), w2.rend());
g1[w1[0]]=w1[1];
for(int i=0; i+2<w1.size(); ++i)
e[w1[i+1]].push_back({w1[i], w1[i+2]});
g2[w1.back()]=w1[w1.size()-2];
}
int dfs2(int u, int r) {
if(vis[u]==r)
fk();
if(vis[u])
return 0;
vis[u]=r;
int s=1;
for(int v : adj2[u])
s+=dfs2(v, r);
return s;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i=1, u, v; i<n; ++i) {
cin >> u >> v, --u, --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs();
memset(g1, -1, 4*n);
memset(g2, -1, 4*n);
for(int i=0; i<n; ++i) {
cin >> a[i], --a[i];
if(i==a[i])
fk();
if(~a[i])
pr(i, a[i]);
}
for(int i=0; i<n; ++i) {
vector<int> v;
for(array<int, 2> f : e[i]) {
adj2[f[0]].push_back(f[1]);
v.push_back(f[0]);
v.push_back(f[1]);
}
if(~g1[i])
v.push_back(g1[i]);
if(~g1[i]&&~g2[i]&&dfs2(g1[i], g1[i]+1)<adj[i].size()&&vis[g2[i]])
fk();
for(int w : v)
if(!vis[w])
dfs2(w, w+1);
for(int w : v) {
vis[w]=0;
adj2[w].clear();
}
for(int j=2; j<=(int)adj[i].size()-b[i]; ++j)
ans=ans*j%M;
}
cout << ans;
}