【链接】 我是链接,点我呀:)

【题意】

让你判断一个序列是否可能为一个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?的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 796D】Police Stations

    [题目链接]:http://codeforces.com/contest/796/problem/D [题意] 在一棵树上,保证每个点在距离d之内都有一个警察局; 让你删掉最多的边,使得剩下的森林仍然 ...

  3. 【39.29%】【codeforces 552E】Vanya and Brackets

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. 【codeforces 757C】Felicity is Coming!

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【codeforces 755D】PolandBall and Polygon

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【codeforces 755C】PolandBall and Forest

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【codeforces 546D】Soldier and Number Game

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【codeforces 750F】New Year and Finding Roots

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【codeforces 750B】New Year and North Pole

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. P2700逐个击破(并查集/树形dp)

    P2700 逐个击破 题目背景 三大战役的平津战场上,傅作义集团在以北平.天津为中心,东起唐山西至张家口的铁路线上摆起子一字长蛇阵,并企图在溃败时从海上南逃或向西逃窜.为了就地歼敌不让其逃走,老毛同志 ...

  2. python实现汉诺塔程序

    # 汉诺塔思想笔记# 认识汉诺塔的目标:把A柱子上的N个盘子移动到C柱子# 递归的思想就是把这个目标分解成三个子目标# 子目标1:将前n-1个盘子从a移动到b上# 子目标2:将最底下的最后一个盘子从a ...

  3. [转]C++常用字符串分割方法实例汇总

    本文实例汇总了C++常用字符串分割方法,分享给大家供大家参考.具体分析如下: 我们在编程的时候经常会碰到字符串分割的问题,这里总结下,也方便我们以后查询使用. 一.用strtok函数进行字符串分割 原 ...

  4. 启动tomcat报错:ImageFormatException

    启动某工程报错: java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/ImageFormatException 查找此类存在于jdk的rt ...

  5. 1393 0和1相等串 鸽笼原理 || 化简dp公式

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1393 正解一眼看出来的应该是鸽笼原理.记录每个位置的前缀和,就是dp[i ...

  6. Python的I/O操作

    1.读取键盘输入 msg = raw_input("Please enter :") print "you input ",msg #可接受Python表达式作 ...

  7. Python之双色球选购和三级菜单问题

    1:双色球选购# 1 双色球(假设一共八个球,6个红球,球号1-32.2个蓝球,球号1-16)# 2 确保用户不能重复选择,不能超出范围# 3 用户输入有误时有相应的错误提示# 4 最后展示用户选择的 ...

  8. poj1787 Charlie's Change

    思路: 完全背包,记录路径. 实现: #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; ] ...

  9. 08使用NanoPiM1Plus在Android4.4.2下接TF卡

    08使用NanoPiM1Plus在Android4.4.2下接TF卡 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 21504965 AT qq.com 完成时间:2017/12/5 17:51 版本: ...

  10. android调用webservice接口获取信息

    我的有一篇博客上讲了如何基于CXF搭建webservice,service层的接口会被部署到tomcat上,这一篇我就讲一下如何在安卓中调用这些接口传递参数. 1.在lib中放入ksoap2的jar包 ...