CC

自然万物都趋向从有序变得无序

0%

Manthan, Codefest 18 Valid BFS?(BFS)

题目链接


Problem Description

The BFS algorithm is defined as follows.

  1. Consider an undirected graph with vertices numbered from 1 to n. Initialize q as a new queue containing only vertex 1, mark the vertex 1 as used.
  2. Extract a vertex v from the head of the queue q.
  3. Print the index of vertex v.
  4. Iterate in arbitrary order through all such vertices uu that uu is a neighbor of vv and is not marked yet as used. Mark the vertex u as used and insert it into the tail of the queue q.
  1. If the queue is not empty, continue from step 2.

  2. Otherwise finish.

    Since the order of choosing neighbors of each vertex can vary, it turns out that there may be multiple sequences which BFS can print.

    In this problem you need to check whether a given sequence corresponds to some valid BFS traversal of the given tree starting from vertex 1. The tree is an undirected graph, such that there is exactly one simple path between any two vertices.

Input

The first line contains a single integer n (1≤n≤2⋅1e5) which denotes the number of nodes in the tree.

The following n−1n−1 lines describe the edges of the tree. Each of them contains two integers x and y (1≤x,y≤n) — the endpoints of the corresponding edge of the tree. It is guaranteed that the given graph is a tree.

The last line contains nn distinct integers a1,a2,…,ana1,a2,…,an (1≤ai≤n) — the sequence to check.

Output

Print “Yes” (quotes for clarity) if the sequence corresponds to some valid BFS traversal of the given tree and “No” (quotes for clarity) otherwise.

You can print each letter in any case (upper or lower).

Sample intput

4
1 2
1 3
2 4
1 2 3 4

Sample Output

Yes

Sample intput

4
1 2
1 3
2 4
1 2 4 3

Sample Output

No

Note

Both sample tests have the same tree in them.

In this tree, there are two valid BFS orderings:

  • 1,2,3,4
  • 1,3,2,4

The ordering 1,2,4,31,2,4,3 doesn’t correspond to any valid BFS order.


题意:

给定一颗树,问给定的序列是否可能是这颗树的BFS序列

思路:

我们将每个节点的儿子节点按照题目给定的序列的顺序进行排序,然后进行BFS,看生成的序列是否相同。

代码:

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
/*************************************************************************
> File Name: acm.cpp
> Author: wood
> Mail: cbcruel@gmail.com
> Created Time: 2018年08月20日 星期一 11时50分46秒
************************************************************************/
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const LL mod = 1e9+7;
const int maxn = 2e5+10;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define sqr(x) (x)*(x)
template <class T>
inline bool scan_d(T &ret) {
char c; int sgn;
if(c = getchar(),c == EOF) return 0; //EOF
while(c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c-'0');
while(c = getchar(),c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
inline void out(LL x) {
if(x > 9) out( x / 10);
putchar( x % 10 + '0');
}

vector<int> w[maxn];
int a[maxn],b[maxn], c[maxn];
bool vis[maxn];

int main(){
int n;
scanf("%d", &n);
for(int i = 1; i < n; ++i){
int x, y;
scanf("%d %d", &x,&y);
w[x].pb(y);
w[y].pb(x);
}for(int i = 1; i <= n; ++i){
scanf("%d",a+i);
b[a[i]] = i;
}for(int i = 1; i <= n; ++i)
sort(w[i].begin(), w[i].end(), [&](int x, int y){
return b[x] < b[y];
});

queue<int> q;
q.push(1);
vis[1] = true;
int k = 0;
while(!q.empty()){
int t = q.front();
q.pop();
c[++k] = t;
for(auto &x: w[t])
if(!vis[x]){
vis[x] = 1;
q.push(x);
}
}
bool flag = true;
for(int i = 1; i <= n; ++i)
if(a[i] != c[i])
flag = false;
if(flag) puts("Yes");
else puts("No");
return 0;
}