-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeforces-1037D.cc
72 lines (60 loc) · 1.04 KB
/
Codeforces-1037D.cc
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
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <queue>
#include <unordered_map>
using namespace std;
unordered_map <int, int> pos;
bool cmp(int a, int b){
return pos[a] < pos[b];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
vector <set<int>> g(n + 1);
for(int i = 0; i < n - 1; i++){
int u, v;
cin >> u >> v;
g[u].insert(v);
g[v].insert(u);
}
vector <int> a(n + 1);
for(int i = 1; i <= n; i++){
cin >> a[i];
pos[a[i]] = i;
}
if(a[1] != 1){
cout << "No";
return 0;
}
queue <int> Q;
unordered_map <int, int> p;
p[1] = 1;
Q.push(1);
int cur = 1;
while(!Q.empty()){
int t = Q.front();
Q.pop();
if(t != a[cur]){
cout << "No";
return 0;
}
cur++;
vector <int> v;
for(int e: g[t]){
if(!p[e]){
v.push_back(e);
p[e] = 1;
}
}
sort(v.begin(), v.end(), cmp);
for(int e: v){
Q.push(e);
}
}
cout << "Yes";
return 0;
}