Valid BFS? CodeForces - 1037D(思维 bfs)
我真是一只菜狗。。。。。。emm。。。
题意:
判断一个从1开始的队列是否可以按照bfs的顺序 进行遍历。。必须从1开始。。。然后后边依次是bfs顺序
解析:
看代码能看懂吧。。。emm。。。就是把每个有关系的用map标记一下
然后bfs的时候 加一个循环进队列就好了 emm。。/。
#include <bits/stdc++.h>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define LL long long
#define ULL unsigned long long
using namespace std;
const int maxn = , INF = 0x7fffffff;
int n;
map<int, bool> w[maxn];
int a[maxn];
bool bfs()
{
queue<int> Q;
int nex = ;
if(a[nex] != ) return false;
Q.push(a[nex]);
nex++;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
int v = a[nex];
while(w[u][v])
{
Q.push(v);
nex++;
v = a[nex];
}
}
if(nex == n) return true;
return false;
} int main()
{
rd(n);
int u, v;
rep(i, , n-)
{
rd(u), rd(v);
w[u][v] = w[v][u] = ;
}
rep(i, , n)
rd(a[i]);
if(n == ) cout<< "Yes" <<endl;
else if(bfs()) cout<< "Yes" <<endl;
else cout<< "No" <<endl; return ;
}
Valid BFS? CodeForces - 1037D(思维 bfs)的更多相关文章
- Codeforces 1037D【BFS】
<题目链接> 题目大意: 给你一颗树的所有边,这些边是无向的,然后给你一段BFS序列,BFS都以1为根节点,判断这段BFS序列是否合法. 解题分析: 就是模拟BFS,某个父亲节点的所有子节 ...
- CodeForces 586D【BFS】
题意: s是这个人开始位置:连续相同大写字母是 Each of the k trains,相应的火车具有相应的字母: '.' 代表空: 有个人在最左列,上面有连续字母代表的火车,火车从左边出去的话,会 ...
- POJ 1915-Knight Moves (单向BFS && 双向BFS 比)
主题链接:Knight Moves 题意:8个方向的 马跳式走法 ,已知起点 和终点,求最短路 研究了一下双向BFS,不是非常难,和普通的BFS一样.双向BFS只是是从 起点和终点同一时候開始搜索,可 ...
- [codeforces 1037D] Valid BFS? 解题报告(验证bfs序,思维题)
题目链接:http://codeforces.com/problemset/problem/1037/D 题目大意: 给出一棵树,询问一个序列是否可能为这棵树从节点1开始遍历的bfs序 题解: 对于每 ...
- [Codeforces 1037D] Valid BFS?
[题目链接] http://codeforces.com/problemset/problem/1037/D [算法] 首先求出每个点的父节点 , 每棵子树的大小 然后判断BFS序是否合法即可 时间复 ...
- 【Codeforces 1037D】Valid BFS?
[链接] 我是链接,点我呀:) [题意] 让你判断一个序列是否可能为一个bfs的序列 [题解] 先dfs出来每一层有多少个点,以及每个点是属于哪一层的. 每一层的bfs如果有先后顺序的话,下一层的节点 ...
- Codeforces Round #297 (Div. 2) D. Arthur and Walls [ 思维 + bfs ]
传送门 D. Arthur and Walls time limit per test 2 seconds memory limit per test 512 megabytes input stan ...
- Vladik and Favorite Game CodeForces - 811D (思维+BFS+模拟+交互题)
D. Vladik and Favorite Game time limit per test 2 seconds memory limit per test 256 megabytes input ...
- CodeForces - 377A Maze BFS逆思维
Maze Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, ...
随机推荐
- POJ 1144 Network(tarjan 求割点个数)
Network Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17016 Accepted: 7635 Descript ...
- redis的使用,相比memcached
redis支持数据持久化,不像memcached断电或者重启就丢失数据了. 支持持久化主要有两种方式,在redis.conf配置文件里配置. 1. 使用.rdb格式存储,配置save参数(save N ...
- mysql的常用优化知识
索引类型:主键索引,唯一索引,联合索引,普通索引,全文索引 建立索引: create index index_name on table(field_name); 删除索引: drop index i ...
- 03-Maven坐标管理
1.什么是坐标? 2.坐标的详细概念 3.Maven包引用
- 实现MD5的加密和解密
加密代码 public static string Encrypt(string Text, string sKey) { DESCryptoServiceProvider des = new DES ...
- WPF 嵌入winform 控件
引入 WindowsFormsIntegration.dll 和 System.Windows.Forms.dll <Window x:Class="wgscd.Window1 ...
- 对*P++的理解,再联想~~~
前言: 最近在看一位叫朱有鹏大神的视频,讲的甚好.应此,我的感悟也因此被激发,准备针对朱老师将的内容,结合自己的理解,写一个系列的笔记博客--大家可以去www.zhulaoshi.org观看视频-- ...
- 德哥的PostgreSQL私房菜 - 史上最屌PG资料合集
德哥的PostgreSQL私房菜 - 史上最屌PG资料合集
- libgdx学习记录10——Particle粒子
粒子对制作画面特效很有用,可以使用Particle Editor进行自行编辑粒子效果,跟图片一起生成.p粒子文件,然后导入到程序中使用. 本文所用的粒子效果是基于其自带的demo的. 实例: pack ...
- 移动端页面滑动时候警告:Unable to preventDefault inside passive event listener due to target being treated as passive.
移动端项目中,在滚动的时候,会报出以下提示: [Intervention] Unable to preventDefault inside passive event listener due to ...