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如果有先后顺序的话,下一层的节点 ...
随机推荐
- linux问题总结
编写后台进程的管理脚本,使用service deamon-name stop的时候,出现如下提示:/sbin/service: line 66: 23299 Terminated env -i LAN ...
- Android学习之基础知识十五 — 最佳UI体验(Material Design实战)
一.前言 长久以来,大多数人都认为Android系统的UI并不美观,至少没有iOS系统的美观.以至于很多IT公司在进行应用界面设计的时候,为了保证双平台的统一性,强制要求Android端的界面风格必须 ...
- nginx-haproxy-lvs功能和性能对比 nginx - max-fail + 调度算法
优点(性能和功能两方面分析) 缺点 lvs 工作在4层,可以对http,MySQL等服务负载均衡.负责流量的分发,对io,cpu,mem的消耗少 功能比较少,没有正则匹配的功能 nginx 工作在7层 ...
- 盖茨基金会:如何使用Python拯救生命
每年全球都要花费数十亿美元来预防疾病,减少死亡,资助预防保健及治疗的各种研发项目,以及其他的健康方案.但资金毕竟是有限的,所以一些组织,比如全球卫生资金的主要捐助者比尔&梅林达·盖茨基金会(B ...
- Luogu4131 WC2005 友好的生物 状压DP
传送门 首先$C_i$是没有意义的,因为可以直接让$d_i \times= C_i$,答案也是一样的 所以我们现在考虑求$(\sum_{i=1}^{K-1} |d_{p,i}-d_{q,i}|) - ...
- Luogu2993 FJOI2014 最短路径树问题 最短路树、长链剖分
传送门 强行二合一最为致命 第一问直接最短路+$DFS$解决 考虑第二问,与深度相关,可以考虑长链剖分. 设$f_{i,j}$表示长度为$i$,经过边数为$j$时的最大边权和,考虑到每一次从重儿子转移 ...
- LiveCharts文档-3开始-5序列Series
原文:LiveCharts文档-3开始-5序列Series LiveCharts文档-3开始-5序列Series Strokes和Fills 笔触和填充 所有的Series都有笔触和填充属来处理颜色, ...
- 面试3——java集合类面试题总结
1.总结一下啊hashmap和hashtable的知识点? 1)关于hashmap的说法 HashMap实际上是一个“链表散列”的数据结构,在jdk1.8中添加了红黑树.HashMap底层结构是一个数 ...
- 【noi.ac】#309. Mas的童年
#309. Mas的童年 链接 分析: 求$max \{sj + (s_i \oplus s_j)\}$ 因为$a + b = a \oplus b + (a \& b) \times 2$ ...
- SNMP基础知识
注:本篇博客并非原创,仅是学习笔记 1. 概述1.1 诞生背景1.2 SNMP简介1.3 版本1.4 术语1.5 网络结构1.6 MIB简介2. Linux的SNMP安装 1. 概述 1.1 诞生背景 ...