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如果有先后顺序的话,下一层的节点 ...
随机推荐
- PAT A1108 Finding Average (20 分)——字符串,字符串转数字
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- C++ assert断言
assert断言通常用于调试,用法如下: assert(expr); // 当expr==0时,系统会调用abort来终止程序运行 调试完成后,可以在include <assert.h>之 ...
- kubernetes session回话保持
1.Nginx 版本 root@ingress-nginx-controller-4b75b:/# /usr/sbin/nginx -vnginx version: nginx/1.13.9 2.in ...
- SkylineGlobe Android 开发 面积计算示例代码
SkylineGlobe Android 开发 面积计算示例代码: 如果之前熟悉SkylineGlobe桌面端的二次开发,看这些代码应该不难理解. package com.skyline.terrae ...
- SkylineGlobe 如何二次开发获取三维模型的BBOX和设置Tint属性
测试模型类型选择TerrainModel和Feature两种,测试代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiti ...
- SVM的简单介绍
ng的MI-003中12 ——SVM 一.svm目标函数的由来 视频先将LR的损失函数: 在上图中,先将y等于0 和y等于1的情况集合到一起成为一个损失函数,然后分别讨论当y等于1的时候损失函数的结果 ...
- Django 学习 (第五部)
表设计: from django.db import models # Create your models here. # class Foo(models.Model): # name = mod ...
- odoo订餐系统之订单设计
订餐系统的主要功能便是用户下单部分,这里我们分为表头mylunch_order和表体mylunch_order_line两张主要的数据表,表头主要记录订单的一些通用信息,比如下单的操作人员 下单日期 ...
- 51Nod 1299 监狱逃离
这其实是一道树形DP的神仙题. 然后开始推推推,1 hour later样例都过不了 然后仔细一看题目,貌似像一个最小割模型,然后5min想了想建图: 首先拆点,将每个点拆成进和出两个,然后连边,边权 ...
- [Python]Hamming distance 问题
In [75]: x=4 In [76]: y=1 In [77]: str(bin(x ^ y))[2:].count('1') Out[77]: 2 In [78]: 来自:https://lee ...