D. Valid BFS

3 篇文章 0 订阅

题目链接:https://codeforces.com/contest/1037/problem/D

D. Valid BFS?

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The BFS algorithm is defined as follows.

  1. Consider an undirected graph with vertices numbered from 11 to nn. Initialize qq as a new queue containing only vertex 11, mark the vertex 11 as used.
  2. Extract a vertex vv from the head of the queue qq.
  3. Print the index of vertex vv.
  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 uu as used and insert it into the tail of the queue qq.
  5. If the queue is not empty, continue from step 2.
  6. 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 11. 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 nn (1≤n≤2⋅1051≤n≤2⋅105) 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 xx and yy (1≤x,y≤n1≤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≤n1≤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).

Examples

input

Copy

4
1 2
1 3
2 4
1 2 3 4

output

Copy

Yes

input

Copy

4
1 2
1 3
2 4
1 2 4 3

output

Copy

No

Note

Both sample tests have the same tree in them.

In this tree, there are two valid BFS orderings:

  • 1,2,3,41,2,3,4,
  • 1,3,2,41,3,2,4.

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

题目大意:给出一颗树,节点1为根节点,再给出一段序列,如果这个序列能用 bfs 遍历顺序得出,输出Yes 否则,输出No;

要训练将转换问题的能力,如果按照所给的序列以个一个比对,可能很难实现,

代码代码代码代码代码代码代码代码代码代码代码代码代码

我给你讲啊,这个人,大佬--->

 

#include<iostream>
#include<queue>
#include<vector>
#include<set>
using namespace std;
int  const  MAXN=2*1e5+10;
queue<int> q;
vector< set<int > > vec(MAXN);//定义一个vector数组,里面是set<int>类型;
int dis[MAXN];
int main(){
    int n;
    cin>>n;
    int a,b;
    for(int i=0;i<n-1;i++){
        cin>>a>>b;
        vec[a].insert(b);//无向图存图;
        vec[b].insert(a);
    }
    for(int i=0;i<n;i++){
        cin>>dis[i];
    }
    q.push(1);
    if(dis[0]!=1){//开头一定是1,如果不是1,就没有必要往下进行了;
        cout<<"No"<<endl;
        return 0;
    }
    int i=1;
    while(!q.empty()){
        int s=q.front();//取出队列中的元素;
        q.pop();
        //vec[s].find(dis[i] 表示以点s为父节点遍历所有他的孩子,再将每个孩子放入到队列中;
        while(i<n && vec[s].find(dis[i])!=vec[s].end()){
            q.push(dis[i]);
            i++;//i一直往前走,把dis[i]的值放入队列之中,
        }
    }
    if(i==n){//如果i能够一直走到头,说明给出的序列满足bfs的定义方式;
        cout<<"Yes"<<endl;
    }else{
        cout<<"No"<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值