【Codeforces 1037D】Valid BFS?
【链接】 我是链接,点我呀:)
【题意】
让你判断一个序列是否可能为一个bfs的序列
【题解】
先dfs出来每一层有多少个点,以及每个点是属于哪一层的。
每一层的bfs如果有先后顺序的话,下一层的节点的出队也是有先后顺序的
因此x是当前层只是一个简单的判断条件,还需要更深入的判断
也就是说它是不是应该在这个时候从队列中出来,也就是说它前面是不是应该有节点比它先出现.
我们需要记录每个节点(dep层)的priority值。
这个值表示了这个节点x它的直系儿子们(dep+1层)在所有dep+1层出现的顺序(如果为1最先出现,其次的话依次往后推
细节比较多。
要注意某个节点可能没有后代节点了。
【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = (int)2e5;;
static class Task{
int n;
ArrayList g[] = new ArrayList[N+10];
int fa[] = new int[N+10];
int dep[]= new int[N+10],dep_size[] = new int[N+10];
int priority[] = new int[N+10];
void dfs1(int x,int f) {
dep_size[dep[x]]++;
fa[x] = f;
int len = g[x].size();
for (int i = 0;i < len;i++) {
int y = (int)g[x].get(i);
if (y==f) continue;
dep[y] = dep[x] + 1;
dfs1(y,x);
}
}
int get_priority_up(int x) {
if (x==1)
return g[x].size();
else
return g[x].size()-1;
}
public void solve(InputReader in,PrintWriter out) {
for (int i = 1;i <= N;i++) g[i] = new ArrayList<>();
n = in.nextInt();
for (int i = 1;i <= n-1;i++) {
int x,y;
x = in.nextInt();y = in.nextInt();
g[x].add(y);g[y].add(x);
}
dep[1] = 1;
dfs1(1,-1);
int x;
x = in.nextInt();
if (x!=1) {
out.println("No");
return;
}
priority[1] = 1;
int cur_dep = 2,cur_priority = 1,cur_priority_count = 0;
int record_priority = 1;
ArrayList cur_priority_size[] = new ArrayList[N+10];
for (int i = 1;i <= n;i++) {
cur_priority_size[i] = new ArrayList<>();
cur_priority_size[i].add(-1);
}
cur_priority_size[1].add(g[1].size());
for (int i = 2;i <= n;i++) {
while((int)cur_priority_size[cur_dep-1].get(cur_priority)==0) {
cur_priority++;
if (cur_priority>dep_size[cur_dep-1]) {
record_priority = 1;
cur_dep++;
cur_priority = 1;
}
}
x = in.nextInt();
if (dep[x]!=cur_dep) {
out.println("No");
return;
}
int _father = fa[x];
int f_priority = priority[_father];
if (f_priority!=cur_priority) {
out.println("No");
return;
}
priority[x] = record_priority;
cur_priority_size[cur_dep].add(g[x].size()-1);
//out.println("priority["+x+"]="+priority[x]);
record_priority++;
cur_priority_count++;
if (cur_priority_count==get_priority_up(_father)) {
cur_priority++;
cur_priority_count = 0;
if (cur_priority>dep_size[cur_dep-1]) {
record_priority = 1;
cur_dep++;
cur_priority = 1;
}
}
}
out.println("Yes");
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
【Codeforces 1037D】Valid BFS?的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 796D】Police Stations
[题目链接]:http://codeforces.com/contest/796/problem/D [题意] 在一棵树上,保证每个点在距离d之内都有一个警察局; 让你删掉最多的边,使得剩下的森林仍然 ...
- 【39.29%】【codeforces 552E】Vanya and Brackets
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 757C】Felicity is Coming!
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 755D】PolandBall and Polygon
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 755C】PolandBall and Forest
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 546D】Soldier and Number Game
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 750F】New Year and Finding Roots
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 750B】New Year and North Pole
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- 【171】IDL读取HDF文件
;+ ;:Description: ; Describe the procedure. ; ; Author: DYQ 2009-7-19; ; ;- PRO TEST_READHDF COMPILE ...
- springmvc 用fasterxml.jackson返回son数据
一,引入fasterxm.jackson包 <dependency> <groupId>com.fasterxml.jackson.core</groupId> & ...
- HTML 5.1 -- 14项新增功能及如何使用
最近太忙了 过完年来 连续的加班让我筋疲力尽,今天终于把东西交了,抽空来点干货吧! 1. 响应式图像 W3C 引入了一些功能特性,无需使用 CSS 就可以实现响应式图像.它们是 … srcset 图像 ...
- robotframework - create dictionary 操作
1.创建字典 2.从字典中获取的项 -- 打印出 item 3.获取字典的key -- 打印出 key 4.获取字典的value -- 打印出 value 5.获取字典key,value 6.打印出字 ...
- 基于itchat实现微信群消息同步机器人
原始网址:http://www.jianshu.com/p/7aeadca0c9bd# 最近 全栈数据工程师养成攻略 的微信群已经将近500人,开了二群之后为了打通不同微信群之间的消息,花了点时间做了 ...
- 转 mysql 5.7版本修改编码为utf-8
刚开始学习MySQL,下载的是官网最新版本 5..7.14,使用cmd输入中文时报错,于是开始修改mysql默认编码(windows下) 首先通过 show variables like 'chara ...
- 红黑树与AVL(平衡二叉树)的区别
关于红黑树和AVL树,来自网络: 1 好处 及 用途 红黑树 并不追求“完全平衡 ”——它只要求部分地达到平衡要求,降低了对旋转的要求,从而提高了性能. 红黑树能够以 O(log2 n) 的时间复 ...
- 卸载掉原有mysql
[root@xiaoluo ~]# rpm -qa | grep mysql // 这个命令就会查看该操作系统上是否已经安装了mysql数据库 有的话,我们就通过 rpm -e 命令 或者 rpm - ...
- Framework7首页隐藏navbar
f7首页隐藏navbar其他页面显示navbar 帮别人解决问题,自己也记录一下, 首页.navbar加.navbar-hidden, 首页.page加.no-navbar, 如果首页有.navbar ...
- JavaScript设计模式 (1) 原型模式
原型模式(Prototype):用原型实例指向创建类对象,使用于创建新对象的类共享原型对象的属性以及方法. //图片轮播类 var LoopImages = function (imgArr, con ...