博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Manthan, Codefest 18 (rated, Div. 1 + Div. 2) D.Valid BFS? (模拟)
阅读量:5221 次
发布时间:2019-06-14

本文共 2796 字,大约阅读时间需要 9 分钟。

D. Valid BFS?
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The  algorithm is defined as follows.

  1. Consider an undirected graph with vertices numbered from 11 to nn. Initialize qq as a new  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  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 (1n21051≤n≤2⋅105) which denotes the number of nodes in the tree.

The following n1n−1 lines describe the edges of the tree. Each of them contains two integers xx and yy (1x,yn1≤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 (1ain1≤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一定是第一个数,就从1开始扩展,它连接2,3,那么后面一定是2,3,所以需要扫描2,3位置,然后2连接4,扫描4位置

我们可以发现即使有很多数,扫描过程也是从前往后线性进行的,所以可以直接根据题意进行模拟,先标记该位置连接的数,再扫描具体的区间看是否相符。

代码如下:

#include 
#include
#include
#include
#include
using namespace std;#define INF 0x3f3f3f3fconst int MAXN=2e5+100;int n,x,y;vector
V[MAXN];int vis[MAXN];int a[MAXN];int main(){ scanf("%d",&n); for(int i=1;i<=n-1;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]); vis[1]=1; int p=2; bool flag=true; if(a[1]!=1) flag=false; int len=V[a[1]].size(); for(int i=1;i<=n;i++) { for(int j=0;j

 

转载于:https://www.cnblogs.com/a249189046/p/9643189.html

你可能感兴趣的文章
创建spring boot项目
查看>>
Behave + Selenium(Python) 四
查看>>
系统的横向结构(AOP)
查看>>
linux常用命令
查看>>
有序链表的归并 分类: 链表 2015-06-...
查看>>
A Plug for UNIX 分类: POJ ...
查看>>
寒假作业01
查看>>
Linux常用命令
查看>>
正确适配苹果ATS审核要求的姿势
查看>>
NHibernate.3.0.Cookbook第四章第6节的翻译
查看>>
例1-1
查看>>
Java 8 新特性之 Stream&forEach&map&filter&limit&sorted&统计函数&Collectors&并行(parallel)程序(转)...
查看>>
Windows建立Cucumber和Ruby测试环境
查看>>
HBase中MVCC的实现机制及应用情况
查看>>
马达调速器,直流马达调速器,直流调速器
查看>>
【转】概要设计怎么写
查看>>
前端编码规范小记
查看>>
C#编程(二十五)----------接口
查看>>
c如何弹出保存路径/保存文件对话框
查看>>
HTML标签二
查看>>