CF1037D Valid BFS?
Valid BFS?
The BFS algorithm is defined as follows.
- 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.
- Extract a vertex vv from the head of the queue q.
- Print the index of vertex v.
- 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.
- If the queue is not empty, continue from step 2.
- 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
4
1 2
1 3
2 4
1 2 3 4
Yes
4
1 2
1 3
2 4
1 2 4 3
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?的更多相关文章
- cf1037D. Valid BFS?(BFS?)
题意 题目链接 Sol 非常妙的一道题.. 可以这样想,在BFS序中较早出现的一定是先访问的,所以把每个点连出去的边按出现的前后顺序排个序 看一下按顺序遍历出来的序列与给出的是否相同就行了 #incl ...
- [题解] [CF1037D] Valid BFS?
题面 题解 一个是模拟BFS的过程 还有一个是可以根据给出的BFS序构树, 再看两棵树是否相同 判断相同的话, 以同一个点为根, 看两棵树中1−
- CF 1037 D. Valid BFS?
D. Valid BFS? http://codeforces.com/contest/1037/problem/D 题意: 给一个序列,一棵树,判断能否bfs这棵树,得到这个序列. 分析: 将每个点 ...
- Codeforces | CF1037D 【Valid BFS?】
题目大意:给定一个\(n(1\leq n\leq 2\cdot10^5)\)个节点的树的\(n-1\)条边和这棵树的一个\(BFS\)序\(a_1,a_2,\dots,a_n\),判断这个\(BFS\ ...
- 题解 CF1037D 【Valid BFS?】
不管怎么说,这都不是道紫题吧... 这里采用的思想有点类似轻重链剖分. 我们按照每个节点在序列里面出现的顺序,把每一个节点连出去的边都排一个序. 这样(如果序列没错)肯定会按照序列的方式遍历完全图. ...
- 「CF1037D」Valid BFS?
传送门 Luogu 解题思路 考虑直接模拟 \(\text{BFS}\) 的过程. 对于每一个节点的儿子,先遍历在输入序列中靠前的,判断 \(\text{BFS}\) 是否匹配即可. 细节注意事项 注 ...
- Valid BFS? CodeForces - 1037D(思维 bfs)
我真是一只菜狗......emm... 题意: 判断一个从1开始的队列是否可以按照bfs的顺序 进行遍历..必须从1开始...然后后边依次是bfs顺序 解析: 看代码能看懂吧...emm...就是把每 ...
- [Codeforces 1037D] Valid BFS?
[题目链接] http://codeforces.com/problemset/problem/1037/D [算法] 首先求出每个点的父节点 , 每棵子树的大小 然后判断BFS序是否合法即可 时间复 ...
- 【Codeforces 1037D】Valid BFS?
[链接] 我是链接,点我呀:) [题意] 让你判断一个序列是否可能为一个bfs的序列 [题解] 先dfs出来每一层有多少个点,以及每个点是属于哪一层的. 每一层的bfs如果有先后顺序的话,下一层的节点 ...
随机推荐
- Caused by: java.lang.IllegalStateException: commit already called
出现的情况,当FragmentTransaction是全局变量时,fragment智能调用一次commint: 异常的意思:commint已经被调用过了. 所以不要重复调用它就好了.
- RBAC 基于权限的访问控制 serviceaccount -- clusterRole clusterRoleBinding
1.Role , RoleBinding 的作用对象都是namespace. 2.通过RoleRef,可以看到,RoleBinding对象通过名字,直接引用前面定义的Role,实现subject(us ...
- SkylineGlobe 如何实现二次开发加载KML文件
示例代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- ueditor保存出现 从客户端(Note="<p>12345</p>")中检测到有潜在危险的 Request.Form 值
检测到有潜在危险的 Request.Form 值 这种问题是因为你提交的Form中有HTML字符串,例如你在TextBox中输入了html标签,或者在页面中使用了HtmlEditor组件等,解决办 ...
- 搭建SSH服务
1.安装 ssh-server 通过命令进行安装:sudo apt-get install openssh-server 在安装时遇到问题,根据提示,执行命令:sudo apt-get update, ...
- 字符串阵列String[]转换为整型阵列Int[]
原始数据: string input = "3,7,2,8,1,9,1,34,67,78,22"; 要处理为: " }; 最终处理为: , , , , , , , , , ...
- phpstorm 2018.1.2的安装和破解
1.什么是phpstorm? PhpStorm是一个轻量级且便捷的PHP IDE,其旨在提高用户效率,可深刻理解用户的编码,提供智能代码补全,快速导航以及即时错误检查.但是phpstorm是商业软件, ...
- Scala学习(八)---Scala继承
Scala继承 摘要: 在本篇中,你将了解到Scala的继承与Java和C++最显著的不同.要点包括: 1. extends.final关键字和Java中相同 2. 重写方法时必须用override ...
- python语言程序设计9
1, 数字转换形式中有很多东西都不会,但是总不能放仍不管把? 总结点东西吧,比如 print()中增加end=""参数表示输入后不增加换行,多个print可以连续输出. 2, 我还 ...
- centos7.4下Jira6环境部署及破解操作记录(完整版)
废话不多说,以下记录了Centos7针对Jira6的安装,汉化,破解的操作过程,作为运维笔记留存. 0) 基础环境 192.168.10.212 Centos7.4 mysql 5.6 jdk 1.8 ...