-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD_Valid_BFS.cpp
More file actions
60 lines (51 loc) · 1.14 KB
/
D_Valid_BFS.cpp
File metadata and controls
60 lines (51 loc) · 1.14 KB
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
#include <bits/stdc++.h>
using namespace std;
// First you make it work, then you can always make it beautiful
void solve() {
int n;
cin >> n;
vector<int> adj[n + 1];
for(int i=0;i<n-1;i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> order(n);
vector<int> pos(n + 1);
for(int i=0;i<n;i++) {
cin >> order[i];
pos[order[i]] = i;
}
for(int i=1;i<=n;i++) {
sort(adj[i].begin(), adj[i].end(), [&](const int &a, const int &b) {
return pos[a] < pos[b];
});
}
queue<pair<int, int>> q;
q.push({1, -1});
int ptr = 0;
while(!q.empty()) {
auto [node, p] = q.front();
q.pop();
if(order[ptr++] != node) {
cout << "No\n";
return;
}
for(auto &j : adj[node]) if(j != p) {
q.push({j, node});
}
}
cout << "Yes\n";
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// int _;
// cin >> _;
// while (_-->0) {
solve();
// }
return 0;
}