Valid BFS?

CodeForces - 1037D

The BFS algorithm is defined as follows.

  1. Consider an undirected graph with vertices numbered from 11 to nn. Initialize qqas a new queue containing only vertex 1, mark the vertex 1 as used.
  2. Extract a vertex vv 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 uu as used and insert it into the tail of the queue q.
  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⋅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,…,an (1≤ai≤n) — the sequence to check.

Output

Print "Yes" (quotes for clarity) if the sequence corresponds to some valid BFStraversal of the given tree and "No" (quotes for clarity) otherwise.

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

Examples

Input
4
1 2
1 3
2 4
1 2 3 4
Output
Yes
Input
4
1 2
1 3
2 4
1 2 4 3
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,3 doesn't correspond to any valid BFS order.

题意:给一棵树,和一串序列,问是否可以有一种从1开始 bfs的顺序可以使得bfs序为给出的序列

sol:复制来的题面给英语好的大佬看

按着题意大力模拟,按着序列的优先级确定加边的顺序,复杂度介于O(n)到O(n*logn)之间

Ps:序列第一个不是1直接No

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=,M=;
int n,Id[N],Pos[N];
vector<int>E[N];
struct Tree
{
int tot,Next[M],to[M],head[N];
inline void add(int x,int y)
{
Next[++tot]=head[x];
to[tot]=y;
head[x]=tot;
return;
}
bool Arr[N];
queue<int>Que;
inline bool bfs(int S)
{
memset(Arr,,sizeof Arr);
int i,Now=;
Que.push(S);
Arr[S]=;
while(!Que.empty())
{
int x=Que.front(); Que.pop();
// printf("x=%d\n",x);
if(x==(Id[Now+])) Now++; else return ;
for(i=head[x];i;i=Next[i]) if(!Arr[to[i]])
{
Arr[to[i]]=;
Que.push(to[i]);
}
}
return ;
}
inline void Init()
{
tot=;
memset(head,,sizeof head);
memset(Arr,,sizeof Arr);
return;
}
}T;
inline bool cmp(int x,int y)
{
return Pos[x]<Pos[y];
}
int Duilie[N];
int main()
{
int i,j;
T.Init();
R(n);
for(i=;i<n;i++)
{
int x=read(),y=read();
E[x].push_back(y);
E[y].push_back(x);
}
for(i=;i<=n;i++)
{
Pos[Id[i]=read()]=i;
}
for(i=;i<=n;i++)
{
*Duilie=;
for(j=;j<E[i].size();j++) Duilie[++*Duilie]=E[i][j];
sort(Duilie+,Duilie+*Duilie+,cmp);
for(j=*Duilie;j>=;j--)
{
// printf("%d %d\n",i,Duilie[j]);
T.add(i,Duilie[j]);
}
}
(T.bfs())?puts("Yes"):puts("No");
return ;
}
/*
input
4
1 2
1 3
2 4
1 2 3 4
output
Yes input
4
1 2
1 3
2 4
1 2 4 3
output
No input
6
1 2
1 5
2 3
2 4
5 6
1 2 5 3 4 6
output
Yes input
6
1 2
1 5
2 3
2 4
5 6
1 5 2 3 4 6
output
No
*/

CF1037D Valid BFS?的更多相关文章

  1. cf1037D. Valid BFS?(BFS?)

    题意 题目链接 Sol 非常妙的一道题.. 可以这样想,在BFS序中较早出现的一定是先访问的,所以把每个点连出去的边按出现的前后顺序排个序 看一下按顺序遍历出来的序列与给出的是否相同就行了 #incl ...

  2. [题解] [CF1037D] Valid BFS?

    题面 题解 一个是模拟BFS的过程 还有一个是可以根据给出的BFS序构树, 再看两棵树是否相同 判断相同的话, 以同一个点为根, 看两棵树中1−

  3. CF 1037 D. Valid BFS?

    D. Valid BFS? http://codeforces.com/contest/1037/problem/D 题意: 给一个序列,一棵树,判断能否bfs这棵树,得到这个序列. 分析: 将每个点 ...

  4. Codeforces | CF1037D 【Valid BFS?】

    题目大意:给定一个\(n(1\leq n\leq 2\cdot10^5)\)个节点的树的\(n-1\)条边和这棵树的一个\(BFS\)序\(a_1,a_2,\dots,a_n\),判断这个\(BFS\ ...

  5. 题解 CF1037D 【Valid BFS?】

    不管怎么说,这都不是道紫题吧... 这里采用的思想有点类似轻重链剖分. 我们按照每个节点在序列里面出现的顺序,把每一个节点连出去的边都排一个序. 这样(如果序列没错)肯定会按照序列的方式遍历完全图. ...

  6. 「CF1037D」Valid BFS?

    传送门 Luogu 解题思路 考虑直接模拟 \(\text{BFS}\) 的过程. 对于每一个节点的儿子,先遍历在输入序列中靠前的,判断 \(\text{BFS}\) 是否匹配即可. 细节注意事项 注 ...

  7. Valid BFS? CodeForces - 1037D(思维 bfs)

    我真是一只菜狗......emm... 题意: 判断一个从1开始的队列是否可以按照bfs的顺序 进行遍历..必须从1开始...然后后边依次是bfs顺序 解析: 看代码能看懂吧...emm...就是把每 ...

  8. [Codeforces 1037D] Valid BFS?

    [题目链接] http://codeforces.com/problemset/problem/1037/D [算法] 首先求出每个点的父节点 , 每棵子树的大小 然后判断BFS序是否合法即可 时间复 ...

  9. 【Codeforces 1037D】Valid BFS?

    [链接] 我是链接,点我呀:) [题意] 让你判断一个序列是否可能为一个bfs的序列 [题解] 先dfs出来每一层有多少个点,以及每个点是属于哪一层的. 每一层的bfs如果有先后顺序的话,下一层的节点 ...

随机推荐

  1. java 桥接模式

    桥接(Bridge)是用于把抽象化与实现化解耦,使得二者可以独立变化,它通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦. 1)适配器:改变已有的两个接口,让他们相容. 2)桥接模式:分离抽象化 ...

  2. [04] Bean的实例化和多个配置文件

    之前我们已经提到过,Spring的核心能力之一就是IOC机制,从上章的示例中我们也可以看到,Spring中的ApplicationContext充当了一个实例化对象的容器的角色,并管理着它们的生命周期 ...

  3. 上传头像,layui上传图片

    layui上传与bootstrap上传相似,只是不需要下插件, layui自带的已够用 先看一下前台界面,这里是用到的上传头像 先点击开始上传,头像上传至服务器中, 返回json添加至form表单中, ...

  4. 一篇文章让你彻底掌握 shell 语言

    一篇文章让你彻底掌握 shell 语言 由于 bash 是 Linux 标准默认的 shell 解释器,可以说 bash 是 shell 编程的基础. 本文主要介绍 bash 的语法,对于 linux ...

  5. 【小程序】页面无法更新tabbar角标属性时

    在小程序论坛上找答案,一同问了,截图如下

  6. C# 根据部分属性来判断俩个对象是否相同

    根据部分属性来判断俩个对象是否相同 代码是第一版本 可能不牢固 有问题请反馈一下 3QU 效果图: public static class CustomExpand { public static b ...

  7. CentOS 6下gcc升级的操作记录(由默认的4.4.7升级到6.4.0版本)

    机房一台centos6.9机器部署了jenkins发布系统,开发人员在用node编译js,发现依赖的gcc版本低了,故需要将gcc升级到高版本(至少5.0版本以上),这里选择升级到6.4.0版本,下面 ...

  8. Linux下性能调试工具运维笔记

    作为一名资深的linux运维工程师,为方便了解和追求服务器的高性能,如cpu.内存.io.网络等等使用情况,要求运维工程师必须要熟练运用一些必要的系统性能调试工具,liunx下提供了众多命令方便查看各 ...

  9. 解决error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file

    原文地址:http://blog.csdn.net/yjk13703623757/article/details/53217377 一.问题 运行hydra时,提示错误: hydra : error ...

  10. 变量 var &函数new

    声明变量 变量:变量是存储信息的容器,创建变量通常称为"声明"变量 变量必须以字母开头(小驼峰式myName): 变量也能以 $ 和 _ 符号开头(不过我们不推荐这么做): 变量名 ...