Manthan, Codefest 18 (rated, Div. 1 + Div. 2) D. Valid BFS?

D. Valid BFS?

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 11as 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

题意:给你一棵树的广搜遍历,问你是不是合法的广搜序列。

思路:刚开始会想到生成广搜序列,后来发现对于一层会有多个选择,所以序列是很多的,因此直接利用题目所给序列去遍历树即可,如果在给定序列中找不到这个节点,即可证明该序列错误。

具体就是在广搜的时候,对于第i层,我们可以标记所有的节点序号,然后根据树中的实际个数,去给定的数组里判断。

如果数组里有未被标记的,即判定No,如果没有问题,该层就是对的,按照数组顺序依次压入队列,顺序一定要按照数组,重复以上即可。

#include <bits/stdc++.h>
#define ll long long
using namespace std;
map<int,int>mp;
vector<int>v[200020];
int n,a[200020],vis[200020],pos,flag;
void bfs()
{
    queue<int>q;
    q.push(1);
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        vis[now]=1;
        mp.clear();   //用来标记节点,其实可以直接用vis数组
        int cnt=0;
        for(int i=0;i<v[now].size();i++)
        {
            int nex=v[now][i];
            if(vis[nex])continue;
            mp[nex]=1;    //该层全部标记
            cnt++;   //统计该层总数
        }
        cnt=cnt+pos-1;   //计算给定数组的新坐标
        while(pos<=cnt)
        {
            if(mp[a[pos]]==0)
            {
                flag=1;
                return ;
            }
            else q.push(a[pos]);
            pos++;
        }
    }
}
int main()
{
    int x,y;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<=200010;i++)v[i].clear();
        for(int i=1;i<n;i++)  
        {
            scanf("%d%d",&x,&y);
            v[x].push_back(y);
            v[y].push_back(x);
        }
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        if(a[1]!=1){printf("No\n");continue;}   //第一个数只能是1,否则立即判定No
        memset(vis,0,sizeof(vis));
        pos=2;   //用来扫描给定数组,直接从2开始
        flag=0;
        bfs();
        if(flag==0)printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值