-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathd.cc
65 lines (62 loc) · 1.08 KB
/
d.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
// https://codeforces.com/contest/1037/problem/D
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef unordered_set<int> si;
typedef vector<si> vsi;
int main() {
int n;
cin >> n;
vsi g(n);
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--; b--;
g[a].insert(b);
g[b].insert(a);
}
vi v(n, 0);
vi e(n);
for (int i = 0; i < n; i++) {
cin >> e[i];
e[i]--;
}
if (e[0]) {
cout << "No\n";
return 0;
}
if (n == 1) {
cout << "Yes\n";
return 0;
}
deque<int> q;
q.push_back(0);
v[0] = 1;
int i = 1;
while (!q.empty()) {
int u = q.front(); q.pop_front();
auto it = g[u].find(e[i]);
while (it != g[u].end()) {
if (v[*it]) {
cout << "No\n";
return 0;
}
v[*it] = 1;
q.push_back(*it);
g[u].erase(it);
i++;
if (i == n) {
cout << "Yes\n";
return 0;
}
it = g[u].find(e[i]);
}
for (auto z:g[u])
if (!v[z]) {
cout << "No\n";
return 0;
}
}
cout << "Yes\n";
return 0;
}