[Codeforces 1037D] Valid BFS?
[题目链接]
http://codeforces.com/problemset/problem/1037/D
[算法]
首先求出每个点的父节点 , 每棵子树的大小
然后判断BFS序是否合法即可
时间复杂度 : O(N)
[代码]
- #include<bits/stdc++.h>
- using namespace std;
- const int MAXN = 2e5 + ;
- struct edge
- {
- int to , nxt;
- } e[MAXN << ];
- int n , tot;
- int a[MAXN],fa[MAXN],head[MAXN],size[MAXN];
- template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
- template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
- template <typename T> inline void read(T &x)
- {
- T f = ; x = ;
- char c = getchar();
- for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
- for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
- x *= f;
- }
- inline void addedge(int u,int v)
- {
- tot++;
- e[tot] = (edge){v,head[u]};
- head[u] = tot;
- }
- int main()
- {
- read(n);
- if (n == ) { puts("YES"); return ; }
- for (int i = ; i < n; i++)
- {
- int u , v;
- read(u); read(v);
- addedge(u,v);
- addedge(v,u);
- }
- for (int i = ; i <= n; i++) read(a[i]);
- if (a[] != ) { puts("NO"); return ; }
- queue< int > q;
- fa[] = -;
- q.push();
- while (!q.empty())
- {
- int u = q.front();
- q.pop();
- for (int i = head[u]; i; i = e[i].nxt)
- {
- int v = e[i].to;
- if (v != fa[u])
- {
- size[u]++;
- fa[v] = u;
- q.push(v);
- }
- }
- }
- int t = , cnt = ;
- for (int i = ; i <= n; i++)
- {
- if (fa[a[i]] == a[t])
- {
- cnt++;
- continue;
- }
- if (cnt == size[a[t]])
- {
- while (t < i && size[a[t + ]] == ) t++;
- if (t == i)
- {
- printf("NO\n");
- return ;
- }
- cnt = ;
- t++;
- continue;
- }
- printf("NO\n");
- return ;
- }
- if (cnt == size[a[t]]) printf("YES\n");
- else printf("NO\n");
- return ;
- }
[Codeforces 1037D] Valid BFS?的更多相关文章
- [codeforces 1037D] Valid BFS? 解题报告(验证bfs序,思维题)
题目链接:http://codeforces.com/problemset/problem/1037/D 题目大意: 给出一棵树,询问一个序列是否可能为这棵树从节点1开始遍历的bfs序 题解: 对于每 ...
- Codeforces 1037D【BFS】
<题目链接> 题目大意: 给你一颗树的所有边,这些边是无向的,然后给你一段BFS序列,BFS都以1为根节点,判断这段BFS序列是否合法. 解题分析: 就是模拟BFS,某个父亲节点的所有子节 ...
- CF1037D Valid BFS?
Valid BFS? CodeForces - 1037D The BFS algorithm is defined as follows. Consider an undirected graph ...
- CF 1037 D. Valid BFS?
D. Valid BFS? http://codeforces.com/contest/1037/problem/D 题意: 给一个序列,一棵树,判断能否bfs这棵树,得到这个序列. 分析: 将每个点 ...
- Valid BFS? CodeForces - 1037D(思维 bfs)
我真是一只菜狗......emm... 题意: 判断一个从1开始的队列是否可以按照bfs的顺序 进行遍历..必须从1开始...然后后边依次是bfs顺序 解析: 看代码能看懂吧...emm...就是把每 ...
- 【Codeforces 1037D】Valid BFS?
[链接] 我是链接,点我呀:) [题意] 让你判断一个序列是否可能为一个bfs的序列 [题解] 先dfs出来每一层有多少个点,以及每个点是属于哪一层的. 每一层的bfs如果有先后顺序的话,下一层的节点 ...
- Codeforces | CF1037D 【Valid BFS?】
题目大意:给定一个\(n(1\leq n\leq 2\cdot10^5)\)个节点的树的\(n-1\)条边和这棵树的一个\(BFS\)序\(a_1,a_2,\dots,a_n\),判断这个\(BFS\ ...
- CodeForces CF877D题解(BFS+STL-set)
解法\(1:\) 正常的\(bfs\)剪枝是\(\Theta(nm4k)\),这个时间复杂度是只加一个\(vis\)记录的剪枝的,只能保证每个点只进队一次,并不能做到其他的减少时间,所以理论上是过不了 ...
- Amr and Chemistry CodeForces 558C(BFS)
http://codeforces.com/problemset/problem/558/C 分析:将每一个数在给定范围内(10^5)可变成的数(*2或者/2)都按照广搜的方式生成访问一遍,标记上访问 ...
随机推荐
- python flask获取微信用户信息报404,nginx问题
在学习flask与微信公众号时问题,发现测试自动回复/wechat8008时正常,而测试获取微信用户信息/wechat8008/index时出现404.查询资料后收发是nginx配置问题. 在loca ...
- Spring 和 Hibernate的整合
问题 ,spring 和 hibernate 整合 如何整合 1. Spring 使用Hibernate的的SessionFactory 2. Hibernate使用Spring提供的声明式事务
- 【】node基础概念问题(转载)
1.nodejs编写HelloWord,了解什么是nodejs,nodejs有什么特点 2.nodejs的模块怎么用,如何载入别的模块(require),如何给另一模块调用(module, mod ...
- java获取类的全类名----类名.class.getName()的作用是获取这个类的全类名
类名.class.getName()的作用是获取这个类的全类名
- 2017icpc 西安 XOR
XOR Consider an array AAA with n elements . Each of its element is A[i]A[i]A[i] (1≤i≤n)(1 \le i \le ...
- STL map的用法介绍!
map对象的定义 使用前添加map头文件,必须分别指明键和值的类型: map<string,int>word_count; map的构造函数: map<k,v>m; 创建一 ...
- multi cookie & read bug
js cookie multi cookie & read bug document.cookie; // "access_token_test=eyJhbGciOiJIUzI1Ni ...
- Linux下汇编语言学习笔记25 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- Being a Good Boy in Spring Festival 博弈论 Nim博弈
易游戏雷火盘古校园招聘开始! kiki's game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 40000/10000 K (Ja ...
- org.springframework.amqp.AmqpIOException: java.net.UnknownHostException: guest解决
org.springframework.amqp.AmqpIOException: java.net.UnknownHostException: guest 由于在yml文件中配置的时候误将passw ...