[题目链接]

http://codeforces.com/problemset/problem/1037/D

[算法]

首先求出每个点的父节点 , 每棵子树的大小

然后判断BFS序是否合法即可

时间复杂度 : O(N)

[代码]

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN = 2e5 + ;
  4.  
  5. struct edge
  6. {
  7. int to , nxt;
  8. } e[MAXN << ];
  9.  
  10. int n , tot;
  11. int a[MAXN],fa[MAXN],head[MAXN],size[MAXN];
  12.  
  13. template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
  14. template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
  15. template <typename T> inline void read(T &x)
  16. {
  17. T f = ; x = ;
  18. char c = getchar();
  19. for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
  20. for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
  21. x *= f;
  22. }
  23. inline void addedge(int u,int v)
  24. {
  25. tot++;
  26. e[tot] = (edge){v,head[u]};
  27. head[u] = tot;
  28. }
  29.  
  30. int main()
  31. {
  32.  
  33. read(n);
  34. if (n == ) { puts("YES"); return ; }
  35. for (int i = ; i < n; i++)
  36. {
  37. int u , v;
  38. read(u); read(v);
  39. addedge(u,v);
  40. addedge(v,u);
  41. }
  42. for (int i = ; i <= n; i++) read(a[i]);
  43. if (a[] != ) { puts("NO"); return ; }
  44. queue< int > q;
  45. fa[] = -;
  46. q.push();
  47. while (!q.empty())
  48. {
  49. int u = q.front();
  50. q.pop();
  51. for (int i = head[u]; i; i = e[i].nxt)
  52. {
  53. int v = e[i].to;
  54. if (v != fa[u])
  55. {
  56. size[u]++;
  57. fa[v] = u;
  58. q.push(v);
  59. }
  60. }
  61. }
  62. int t = , cnt = ;
  63. for (int i = ; i <= n; i++)
  64. {
  65. if (fa[a[i]] == a[t])
  66. {
  67. cnt++;
  68. continue;
  69. }
  70. if (cnt == size[a[t]])
  71. {
  72. while (t < i && size[a[t + ]] == ) t++;
  73. if (t == i)
  74. {
  75. printf("NO\n");
  76. return ;
  77. }
  78. cnt = ;
  79. t++;
  80. continue;
  81. }
  82. printf("NO\n");
  83. return ;
  84. }
  85. if (cnt == size[a[t]]) printf("YES\n");
  86. else printf("NO\n");
  87.  
  88. return ;
  89.  
  90. }

[Codeforces 1037D] Valid BFS?的更多相关文章

  1. [codeforces 1037D] Valid BFS? 解题报告(验证bfs序,思维题)

    题目链接:http://codeforces.com/problemset/problem/1037/D 题目大意: 给出一棵树,询问一个序列是否可能为这棵树从节点1开始遍历的bfs序 题解: 对于每 ...

  2. Codeforces 1037D【BFS】

    <题目链接> 题目大意: 给你一颗树的所有边,这些边是无向的,然后给你一段BFS序列,BFS都以1为根节点,判断这段BFS序列是否合法. 解题分析: 就是模拟BFS,某个父亲节点的所有子节 ...

  3. CF1037D Valid BFS?

    Valid BFS? CodeForces - 1037D The BFS algorithm is defined as follows. Consider an undirected graph ...

  4. CF 1037 D. Valid BFS?

    D. Valid BFS? http://codeforces.com/contest/1037/problem/D 题意: 给一个序列,一棵树,判断能否bfs这棵树,得到这个序列. 分析: 将每个点 ...

  5. Valid BFS? CodeForces - 1037D(思维 bfs)

    我真是一只菜狗......emm... 题意: 判断一个从1开始的队列是否可以按照bfs的顺序 进行遍历..必须从1开始...然后后边依次是bfs顺序 解析: 看代码能看懂吧...emm...就是把每 ...

  6. 【Codeforces 1037D】Valid BFS?

    [链接] 我是链接,点我呀:) [题意] 让你判断一个序列是否可能为一个bfs的序列 [题解] 先dfs出来每一层有多少个点,以及每个点是属于哪一层的. 每一层的bfs如果有先后顺序的话,下一层的节点 ...

  7. Codeforces | CF1037D 【Valid BFS?】

    题目大意:给定一个\(n(1\leq n\leq 2\cdot10^5)\)个节点的树的\(n-1\)条边和这棵树的一个\(BFS\)序\(a_1,a_2,\dots,a_n\),判断这个\(BFS\ ...

  8. CodeForces CF877D题解(BFS+STL-set)

    解法\(1:\) 正常的\(bfs\)剪枝是\(\Theta(nm4k)\),这个时间复杂度是只加一个\(vis\)记录的剪枝的,只能保证每个点只进队一次,并不能做到其他的减少时间,所以理论上是过不了 ...

  9. Amr and Chemistry CodeForces 558C(BFS)

    http://codeforces.com/problemset/problem/558/C 分析:将每一个数在给定范围内(10^5)可变成的数(*2或者/2)都按照广搜的方式生成访问一遍,标记上访问 ...

随机推荐

  1. python flask获取微信用户信息报404,nginx问题

    在学习flask与微信公众号时问题,发现测试自动回复/wechat8008时正常,而测试获取微信用户信息/wechat8008/index时出现404.查询资料后收发是nginx配置问题. 在loca ...

  2. Spring 和 Hibernate的整合

    问题 ,spring 和 hibernate 整合 如何整合 1. Spring 使用Hibernate的的SessionFactory 2. Hibernate使用Spring提供的声明式事务

  3. 【】node基础概念问题(转载)

    1.nodejs编写HelloWord,了解什么是nodejs,nodejs有什么特点   2.nodejs的模块怎么用,如何载入别的模块(require),如何给另一模块调用(module, mod ...

  4. java获取类的全类名----类名.class.getName()的作用是获取这个类的全类名

    类名.class.getName()的作用是获取这个类的全类名

  5. 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 ...

  6. STL map的用法介绍!

    map对象的定义 使用前添加map头文件,必须分别指明键和值的类型: map<string,int>word_count; map的构造函数: map<k,v>m;   创建一 ...

  7. multi cookie & read bug

    js cookie multi cookie & read bug document.cookie; // "access_token_test=eyJhbGciOiJIUzI1Ni ...

  8. Linux下汇编语言学习笔记25 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  9. Being a Good Boy in Spring Festival 博弈论 Nim博弈

    易游戏雷火盘古校园招聘开始! kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/10000 K (Ja ...

  10. org.springframework.amqp.AmqpIOException: java.net.UnknownHostException: guest解决

    org.springframework.amqp.AmqpIOException: java.net.UnknownHostException: guest 由于在yml文件中配置的时候误将passw ...