【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 ...
随机推荐
- 清北考前刷题day4早安
LI /* 没有考虑次大值有大于一个的情况 */ #include<iostream> #include<cstdio> #include<cstring> # ...
- uva 11292 The Dragon of Loowater(贪心)
题目大意: 你的王国里有一条n个头的恶龙,你希望雇一些骑士把它杀死(即砍掉所有头).村里有m个骑士可以雇佣,一个能力值为x的骑士可以砍掉恶龙一个直径不超过x的头,且需要支付x个金币.如何雇佣骑士才 ...
- $CF55D [数位DP]$
题面 数位DP+状压. 首先,按照数位DP的基本套路,每个个位数的最小公倍数为2520,所以只用考虑模2520的情况.考虑一个DP.dp[i][j][k]表示当前是第i位,2~9的数的集合为j,模25 ...
- 洛谷 P2142 高精度减法(模板)
题目描述 高精度减法 输入输出格式 输入格式: 两个整数a,b(第二个可能比第一个大) 输出格式: 结果(是负数要输出负号) 输入输出样例 输入样例#1: 2 1 输出样例#1: 1 说明 20%数据 ...
- sql 数据库建表
1. 需求 打分和备注是两条记录显示,打分和备注应该放到一张里面吗?放到一张表里面,展示好一些,自己一张表搞定,如果再创建一张表存储备注,需要union all 来进行关联, 感觉性能上有些影响,但是 ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Train Seats Reservation
You are given a list of train stations, say from the station 11 to the station 100100. The passenger ...
- 程序 从存储卡 内存卡 迁移到 SD卡
程序 从存储卡 内存卡 迁移到 SD卡 如果你想移动其他软件,在应用市场界面,点击“管理 > 应用搬家”,点击需要转移的应用旁边的“移至SD卡”即可.
- 使用Oracle的DBMS_SQL包执行动态SQL语句
引用自:http://blog.csdn.net/ggjjzhzz/archive/2005/10/17/507880.aspx 在某些场合下,存储过程或触发器里的SQL语句需要动态生成.Oracle ...
- 网站开发综合技术 第一部分HTML 1.3.2表单
<form id="" name="" method="post/get" action="负责处理的服务端"&g ...
- 专题十二:实现一个简单的FTP服务器
引言: 在本专题中将和大家分享如何自己实现一个简单的FTP服务器.在我们平时的上网过程中,一般都是使用FTP的客户端来对商家提供的服务器进行访问(上传.下载文件),例如我们经常用到微软的SkyDriv ...