$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. Rediskey生命周期管理-key聚合

    | 导语 Redis中的数据以k-v的方式组织:为了方便管理key一般具有某些特定的模式:有些key直接是由mysql中的表行数据转化而来:如果要对Redis的key进行生命周期管理,由于Redis的 ...

  2. vue 项目之后生成的 dist 文件该怎么在本地启动运行

    简单高效 npm i -g servecd distserve

  3. Centos7 在线安装开发环境 jdk1.8+mysql+tomcat

    写在最前 刚刚开始接触Linux,并折腾着在服务器上部署自己的项目,当然作为一个后端开发人员,必不可少的东西肯定是 JDK Mysql Tomcat容器 每天记录一天,每天进步一点点~~ 1.更新系统 ...

  4. springboot打成jar包后无法解压

    springboot打成jar包后无法解压 Springboot打出来的jar,用压缩工具解压报错.Why? 先说解决办法. 1.解决办法 executable属性导致的,属性改成false后重新打包 ...

  5. ICO学习说明

    IOC叫做控制反转,可以理解为我要做一件事,分为1,2,3,4这4部,我们可以在一个函数实现这四步,控制反转就是将这个流程体现在框架中.将原来实现在应用程序流程控制转移到框架中,框架利用一个引擎驱动整 ...

  6. IdentityServer4使用OpenIdConnect实现单点登录

    接上一篇:IdentityServer4实现OAuth2.0四种模式之授权码模式 前面写的四种OAuth2.0实现模式只涉及到IdentityServer4的OAuth2.0特性,并没有涉及到OenI ...

  7. WebClient 请求 https 页面出错:未能创建 SSL/TLS 安全通道

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | Securi ...

  8. 父元素设置min-height子元素设置100%问题

    问题:父元素设置min-height子元素高度设置100%取不到值,这是因为子元素 div设置 height:100%: 只有当父级元素满足min-height:1000px;设置的条件才触发: 浏览 ...

  9. Java System Reports

    You use Java System Reports as a problem detection and analysis tool to: ●      Monitor the AS Java ...

  10. org.springframework.dao.DuplicateKeyException: 问题

    转自:https://blog.51cto.com/chengxuyuan/1786938 org.springframework.dao.DuplicateKeyException: a diffe ...