$des$
一个 n 个点 m 条边的无向连通图从 1 号点开始 bfs,可能得到的 bfs 序有很多,
取决于出边的访问顺序。现在给出一个 1 到 n 的排列,判断是否可能是一个 bfs 序。

$sol$
对于每个节点,令其权值为在给定序列中的位置。
然后从 1 号点开始正常的 bfs,出边的访问顺序按照权值从小到大访问。
最后将得到的 bfs 序与给定序列比较,若完全一致则是合法的。

$code$

#include <bits/stdc++.h>

using namespace std;

#define gc getchar()
inline int read() {
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x; } #define LL long long
#define Rep(i, a, b) for(int i = a; i <= b; i ++) const int N = 2e5 + ; int A[N];
int n;
vector <int> V[N];
int W[N];
int B[N], js;
bool use[N];
int ls[N], len; bool Cmp(int a, int b) {
return W[a] < W[b];
} void Bfs(int s) {
queue <int> Q;
use[s] = ;
Q.push(s);
while(!Q.empty()) {
int tp = Q.front();
B[++ js] = tp;
Q.pop();
len = ;
int S = V[tp].size();
Rep(i, , S - ) {
int v = V[tp][i];
if(use[v]) continue;
use[v] = ;
ls[++ len] = v;
}
sort(ls + , ls + len + , Cmp);
Rep(i, , len) Q.push(ls[i]);
}
} int main() {
n = read();
Rep(i, , n - ) {
int u = read(), v = read();
V[u].push_back(v), V[v].push_back(u);
}
Rep(i, , n) A[i] = read();
Rep(i, , n) W[A[i]] = i;
Bfs(); Rep(i, , n) {
if(A[i] != B[i]) {
cout << "No";
return ;
}
}
cout << "Yes"; return ;
}

cf 1037D BFS的更多相关文章

  1. CF #375 (Div. 2) D. bfs

    1.CF #375 (Div. 2)  D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...

  2. CF 520 B. Two Buttons(bfs)

    /*题意:一个数,就是输入的第一个数,让它变成第二个数最少用几步.可以点红色按钮,蓝色按钮来改变数字,红色:*2,蓝色:-1,如果变成负数,就变成原来的数.CF 520 B. Two Buttons思 ...

  3. cf.295.B Two Buttons (bfs)

     Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. CF Two Buttons (BFS)

    Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  5. CF div2 D BFS

    http://codeforces.com/contest/676/problem/D 题目大意: 勇者去迷宫杀恶龙.迷宫是有n*m的方格子组成的.迷宫上有各种记号,这些记号表达着能走的方向.当且仅当 ...

  6. (中等) CF 585B Phillip and Trains,BFS。

    The mobile application store has a new game called "Subway Roller". The protagonist of the ...

  7. Codeforces 1037D【BFS】

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

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

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

  9. cf 295 div 2 B (bfs)

    题意:给出 n.m 两数,可以对 n 进行两种操作 减一或者乘二,操作过程中 n 必须保证非负,问使 n 变为 m 至少需要几步操作. 这是我练水题的时候做到的,题目不难,只是我 bfs 一直没怎么用 ...

随机推荐

  1. 日志之slf4j和logback日志系统(二)

    这篇文章我们讲一下,如何使用slf4j和logback组合来搭建一套日志系统. 介绍 如果我们的系统是新系统,也就是之前没有引入其他的日志工具,那么只需要引入,如果之前已经用了common-loggi ...

  2. spark源码阅读--SparkContext启动过程

    ##SparkContext启动过程 基于spark 2.1.0  scala 2.11.8 spark源码的体系结构实在是很庞大,从使用spark-submit脚本提交任务,到向yarn申请容器,启 ...

  3. 从MVC -> MVVM ? 开发模式

    MVVM 到底是什么? view :由 MVC 中的 view 和 controller 组成,负责 UI 的展示,绑定 viewModel 中的属性,触发 viewModel 中的命令: viewM ...

  4. NameError: name “ ” is not defined

    NameError: name “ ” is not defined 问题一:name ‘name’ is not defined "name"两端是双下划线"_&quo ...

  5. mount.cifs Windows共享目录权限755问题

    umount -l /usr/local/tomcat7/webapps/dsideal_yy/html/down mount -t cifs -o rw,dir_mode=,file_mode=,s ...

  6. jquery复杂节点获取

    jquery.find方法 1 $("div").find(".1").css({"color":"red"," ...

  7. 前端知识总结--2 js部分

    1. JavaScript的『预解释』与『变量提升』 先看以下代码输出啥? var a= 1; function f() { console.log(a); var a = 2; } f(); 首先答 ...

  8. python_并发编程——管道

    1.管道 from multiprocessing import Pipe conn1,conn2 = Pipe() #返回两个值 conn1.send('wdc') #发送 print(conn2. ...

  9. .net序列化 - Newtonsoft(Json.Net)简单应用

    Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库. Json.Net是一个读写Json效率比较高的.Net框架.Json.Net 使得在.Net环境下使用Json更加简 ...

  10. MySQL 索引原理及慢查询优化

    MySQL凭借着出色的性能.低廉的成本.丰富的资源,已经成为绝大多数互联网公司的首选关系型数据库.虽然性能出色,但所谓“好马配好鞍”,如何能够更好的使用它,已经成为开发工程师的必修课,我们经常会从职位 ...