首页 技术 正文
技术 2022年11月11日
0 收藏 596 点赞 4,980 浏览 2659 个字

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

【题意】

让你判断一个序列是否可能为一个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());
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,498
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,911
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,745
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,502
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,141
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,304