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. AI 线性回归

    线性回归(Linear Regression),顾名思义,输出是输入的线性函数.因为通常会附加偏置(bias)参数,所以实际是仿射函数. 参考链接: http://cs229.stanford.edu ...

  2. #ifdef __cplusplus extern "C" { #endif”的定义

      平时我们在linux c平台开发的时候,引用了一些Cpp或者C的代码库,发现一些头文件有如下代码条件编译. #ifdef __cplusplus extern "C" { #e ...

  3. GPXReader工具代码简析

    完整的文件在TerraExplorer Pro的默认安装目录下C:\Program Files (x86)\Skyline\TerraExplorer Pro\Tools\GPXReader: 如果你 ...

  4. 1-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案升级篇(方案总揽)

    我的这个升级篇的代码适用于自己所有的带WIFI和GPRS模块的开发板,升级功能实质上是通过MQTT把数据发给WIFI和GPRS模块,然后模块进行保存和运行. 这个升级程序是当时自己花了两个星期的时间写 ...

  5. C#以管理员用户打开某个程序

    static void Main(string[] args) { string path = @"C:\Windows\AppPatch\AppLoc.exe"; Process ...

  6. LiveCharts文档-3开始-3类型和设置

    原文:LiveCharts文档-3开始-3类型和设置 LiveCharts文档-3开始-3类型和设置 类型和设置 这一部分非常的重要,涉及到LiveCharts的基本构成单元的介绍 LiveChart ...

  7. WPF中反转3D列表项

    原文:WPF中反转3D列表项 WPF中反转3D列表项                                                         周银辉记得在苹果电脑中有一个很酷的 ...

  8. Ionic Contoller类与Service类分开需要注意的问题

    看了别人的项目把Controller类和Service类都写在了app.js文件里面,这不符合我的风格,想把他们分开成单独的文件,确遇见以下错误提示: ionic.bundle.min.js:133 ...

  9. RuntimeError: Python is not installed as a framework 错误解决方案

    在virtualenv环境下使用matplotlib绘图时遇到了这样的问题: >>> import matplotlib.pyplot as pltTraceback (most r ...

  10. Android环境准备

    Android环境准备: 1.安装Java环境(自行安装) 2.下载Android SDK包 3.配置系统环境变量 新建ANDROID_HOME 变量值:SDK安装路径(如:D:\android-sd ...