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

【题意】

让你判断一个序列是否可能为一个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. $割点割顶tarjan$

    原题 #include <bits/stdc++.h> using namespace std; typedef long long LL; inline LL read () { LL ...

  2. java 读取word

    读取word文件 import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import org ...

  3. ACM_错排(递推dp)

    RPG的错排 Time Limit: 2000/1000ms (Java/Others) Problem Description: 今年暑假GOJ集训队第一次组成女生队,其中有一队叫RPG,但做为集训 ...

  4. uiautomatorviewer详解

    一,uiautomatorviewer是什么? Android 4.1发布的,uiautomator是用来做UI测试的.也就是普通的手工测试,点击每个控件元素 看看输出的结果是否符合预期.比如 登陆界 ...

  5. C# 访问mongodb数据库

    1.引用四个mongodb动态库MongoDB.Bson.dll,MongoDB.Driver.Core.dll,MongoDB.Driver.dll,MongoDB.Driver.Legacy.dl ...

  6. hibernate一对多查询

    一对多查询 1,同时添加老师和学生案例 在进行具有关联关系的对象同时添加时 首先绑定对像间的关系 ---将多方关联一方 ---将一方关联多方 然后全部添加 备注: 1,保存老师对象时, 由于设置了学生 ...

  7. es6之数据结构 set,WeakSet,mapWeakMap

    { let list = new Set(); list.add(1); list.add(2); list.add(1); console.log(list); //Set(2) {1, 2} le ...

  8. C# 调用Mysql 带参数存储过程

    使用C#调用Mysql 带参数的存储过程: 1.创建带参数的存储过程:USP_Temp_Test 2.两个参数:IN 参数为 P_XML , OUT 参数为 P_ErrorOut 3.C#代码调用该存 ...

  9. Android(java)学习笔记204:JNI之native方法头文件的生成

    1. JDK1.6 ,进入到工程的bin目录下classes目录下: 使用命令: javah  packageName.ClassName 会在当前目录下生成头文件,从头文件找到jni协议方法 下面举 ...

  10. VR虚拟红包的技术实现

    2017年1月20日,腾讯发布消息称将推出Q-Glass,除了一般VR眼镜的标配功能外,Q-Glass还能实现眨眼抢红包.听起来是不是很酷炫?上市时间可要在10年后.不过,2016年底支付宝首推AR实 ...