[Codeforces 1037D] Valid BFS?
[题目链接]
http://codeforces.com/problemset/problem/1037/D
[算法]
首先求出每个点的父节点 , 每棵子树的大小
然后判断BFS序是否合法即可
时间复杂度 : O(N)
[代码]
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + ; struct edge
{
int to , nxt;
} e[MAXN << ]; int n , tot;
int a[MAXN],fa[MAXN],head[MAXN],size[MAXN]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u,int v)
{
tot++;
e[tot] = (edge){v,head[u]};
head[u] = tot;
} int main()
{ read(n);
if (n == ) { puts("YES"); return ; }
for (int i = ; i < n; i++)
{
int u , v;
read(u); read(v);
addedge(u,v);
addedge(v,u);
}
for (int i = ; i <= n; i++) read(a[i]);
if (a[] != ) { puts("NO"); return ; }
queue< int > q;
fa[] = -;
q.push();
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v != fa[u])
{
size[u]++;
fa[v] = u;
q.push(v);
}
}
}
int t = , cnt = ;
for (int i = ; i <= n; i++)
{
if (fa[a[i]] == a[t])
{
cnt++;
continue;
}
if (cnt == size[a[t]])
{
while (t < i && size[a[t + ]] == ) t++;
if (t == i)
{
printf("NO\n");
return ;
}
cnt = ;
t++;
continue;
}
printf("NO\n");
return ;
}
if (cnt == size[a[t]]) printf("YES\n");
else printf("NO\n"); return ; }
[Codeforces 1037D] Valid BFS?的更多相关文章
- [codeforces 1037D] Valid BFS? 解题报告(验证bfs序,思维题)
题目链接:http://codeforces.com/problemset/problem/1037/D 题目大意: 给出一棵树,询问一个序列是否可能为这棵树从节点1开始遍历的bfs序 题解: 对于每 ...
- Codeforces 1037D【BFS】
<题目链接> 题目大意: 给你一颗树的所有边,这些边是无向的,然后给你一段BFS序列,BFS都以1为根节点,判断这段BFS序列是否合法. 解题分析: 就是模拟BFS,某个父亲节点的所有子节 ...
- CF1037D Valid BFS?
Valid BFS? CodeForces - 1037D The BFS algorithm is defined as follows. Consider an undirected graph ...
- CF 1037 D. Valid BFS?
D. Valid BFS? http://codeforces.com/contest/1037/problem/D 题意: 给一个序列,一棵树,判断能否bfs这棵树,得到这个序列. 分析: 将每个点 ...
- Valid BFS? CodeForces - 1037D(思维 bfs)
我真是一只菜狗......emm... 题意: 判断一个从1开始的队列是否可以按照bfs的顺序 进行遍历..必须从1开始...然后后边依次是bfs顺序 解析: 看代码能看懂吧...emm...就是把每 ...
- 【Codeforces 1037D】Valid BFS?
[链接] 我是链接,点我呀:) [题意] 让你判断一个序列是否可能为一个bfs的序列 [题解] 先dfs出来每一层有多少个点,以及每个点是属于哪一层的. 每一层的bfs如果有先后顺序的话,下一层的节点 ...
- Codeforces | CF1037D 【Valid BFS?】
题目大意:给定一个\(n(1\leq n\leq 2\cdot10^5)\)个节点的树的\(n-1\)条边和这棵树的一个\(BFS\)序\(a_1,a_2,\dots,a_n\),判断这个\(BFS\ ...
- CodeForces CF877D题解(BFS+STL-set)
解法\(1:\) 正常的\(bfs\)剪枝是\(\Theta(nm4k)\),这个时间复杂度是只加一个\(vis\)记录的剪枝的,只能保证每个点只进队一次,并不能做到其他的减少时间,所以理论上是过不了 ...
- Amr and Chemistry CodeForces 558C(BFS)
http://codeforces.com/problemset/problem/558/C 分析:将每一个数在给定范围内(10^5)可变成的数(*2或者/2)都按照广搜的方式生成访问一遍,标记上访问 ...
随机推荐
- Linux中搭建FTP服务器
FTP工作原理 (1)FTP使用端口 [root@localhost ~]# cat /etc/services | grep ftp ftp-data 20/tcp #数据链路:端口20 ftp 2 ...
- mysql基准测试与sysbench工具
一.基准测试简介 1.什么是基准测试 数据库的基准测试是对数据库的性能指标进行定量的.可复现的.可对比的测试. 基准测试与压力测试 基准测试可以理解为针对系统的一种压力测试.但基准测试不关心业务逻辑 ...
- Python之爬虫-京东商品
Python之爬虫-京东商品 #!/usr/bin/env python # coding: utf-8 from selenium import webdriver from selenium.we ...
- python中正则表达式与模式匹配
一.前言 在之前找工作过程中,面试时经常被问到会不会python,懂不懂正则表达式.心里想:软件的东西和芯片设计有什么关系?咱也不知道因为啥用这个,咱也不敢问啊!在网上搜索到了一篇关于脚本在ASIC领 ...
- (贪心) Vacations 求休息的最少天数
Description Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasy ...
- The Text Splitting (将字符串分成若干份,每份长度为p或q)
Description You are given the string s of length n and the numbers p, q. Split the string s to piece ...
- 什么是CPU密集型、IO密集型?(转发)
CPU密集型(CPU-bound) CPU密集型也叫计算密集型,指的是系统的硬盘.内存性能相对CPU要好很多,此时,系统运作大部分的状况是CPU Loading 100%,CPU要读/写I/O(硬盘/ ...
- 实现下载pdf文件
//打开文件//$fileDir为文件路径 $fileName为文件名称$file = fopen($fileDir . DS . $fileName, "r");//输入文件标签 ...
- Online IDE & Public URLs & turbo
Online IDE powered by Visual Studio Code https://stackblitz.com/ https://www.polymer-project.org/3.0 ...
- codeforces 361B
#include<stdio.h> int a[100100]; int main() { int n,i,k; while(scanf("%d%d",&n,& ...