-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathE_Valid_BFS.cpp
More file actions
107 lines (85 loc) · 1.74 KB
/
E_Valid_BFS.cpp
File metadata and controls
107 lines (85 loc) · 1.74 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
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
107
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <limits>
#include <numeric>
#include <climits>
#define int long long
using namespace std;
int n;
vector<vector<int>> adj;
vector<int> check;
vector<int> bfs;
vector<int> dist;
map<int,int> mp;
bool cmp(int a , int b){
return mp[a] < mp[b];
}
void solve() {
cin >> n;
adj.assign(n+1 , vector<int>());
dist.assign(n+1 , INT_MAX);
for(int i=0 ; i<n-1 ; i++){
int u , v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i=0 ; i<n ; i++){
int x;
cin >> x;
check.push_back(x);
mp[x] = i;
}
for(int i=0 ; i<adj.size() ; i++){
sort(adj[i].begin() , adj[i].end() , cmp);
}
queue<int> q;
q.push(1);
dist[1] = 0;
while(!q.empty()){
int curr = q.front();
bfs.push_back(curr);
q.pop();
for(int nb : adj[curr]){
if(dist[nb] > 1+dist[curr]){
dist[nb] = 1+dist[curr];
q.push(nb);
}
}
}
if(bfs.size() != check.size()){
cout << "No" << endl;
return;
}
for(int i=0 ; i<bfs.size() ; i++){
if(bfs[i] != check[i]){
cout << "No" << endl;
return;
}
}
cout << "Yes" << endl;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int _t = 1;
// cin >> _t;
while(_t--){
solve();
}
return 0;
}