图的遍历 | 1076 bfs
bfs踩了很多坑才写完。注意:出队时不做是否vis判断,但是要加上vis[出队顶点]=1 。入队时进行判断,并且也要 vis[入队顶点]=1
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <map> #define I scanf
#define OL puts
#define O printf
#define F(a,b,c) for(a=b;a<c;a++)
#define FF(a,b) for(a=0;a<b;a++)
#define FG(a,b) for(a=b-1;a>=0;a--)
#define LEN 1010
#define MAX 0x06FFFFFF
#define V vector<int> using namespace std; vector<int> g[LEN];
int vis[LEN]; int main(){
// freopen("D:\\CbWorkspace\\PAT\\图的遍历\\1076.txt","r",stdin);
int n,l,i,j,m,a,b,t,k;
I("%d%d",&n,&l) ;
F(i,,n+){
I("%d",&m);
FF(j,m){
I("%d",&t);
g[t].push_back(i);
}
}
I("%d",&k);
FF(i,k){
I("%d",&t);
memset(vis,,sizeof vis);
queue<int> q;
q.push(t);
int level=;
int cnt=;
while(!q.empty()){
int sz=q.size();
while(sz-->){ //每一层bfs
int tmp=q.front();
q.pop();
// if(vis[tmp]) continue; //出队防止遍历已知点
vis[tmp]=;
FF(j,g[tmp].size()){ //遍历后继
int o=g[tmp][j];
if(vis[o]==){ //入队防止遍历已知点
vis[o]=;
q.push(o);
cnt++;
}
}
}
level++;
if(level>=l) break;
}
printf("%d\n",cnt) ;
}
return ;
}
图的遍历 | 1076 bfs的更多相关文章
- PAT Advanced 1076 Forwards on Weibo (30) [图的遍历,BFS,DFS]
题目 Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and ...
- 图的遍历(bfs 和dfs)
BFS的思想: 从一个图的某一个顶点V0出发,首先访问和V0相邻的且未被访问过的顶点V1.V2.……Vn,然后依次访问与V1.V2……Vn相邻且未被访问的顶点.如此继续,找到所要找的顶点或者遍历完整个 ...
- PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]
题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a ...
- 第七十四课 图的遍历(BFS)
广度优先相当于对顶点进行分层,层次遍历. 在Graph.h中添加BFS函数: #ifndef GRAPH_H #define GRAPH_H #include "Object.h" ...
- 图的遍历(bfs+dfs)模板
bfs #include<iostream> #include<queue> #include<cstdio> using namespace std; queue ...
- 图的遍历[DFS][BFS]
#include<iostream> #include<iostream> #include<cstring> #include<queue> #inc ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- Kruskal和prime算法的类实现,图的遍历BFS算法。
一.图的遍历 #include<iostream> #include<queue> #include<vector> using namespace std; in ...
随机推荐
- Hbase Filter之PrefixFilter
PrefixFilter PrefixFilter是将rowkey前缀为指定字符串的数据全部过滤出来并返回给用户.例如: Scan scan = new Scan(); scan.setFilter( ...
- struts2的使用入门
虽然说Struts2现在已经被SpringMVC框架淘汰了,据说是有很多安全漏洞.但是Struts2作为一个成熟的MVC框架,还是有必要了解一下的,好歹是曾经风光一时的前辈,老祖宗的东西不能丢下,里面 ...
- scrapy 使用
启动方式: 写一个启动文件,与配置文件同级 from scrapy.cmdline import execute import sys,os sys.path.append(os.path.dir ...
- FormData使用详解
- 最简单的 kubernetes 高可用安装方式
sealos 项目地址:https://github.com/fanux/sealos 本文教你如何用一条命令构建 k8s 高可用集群且不依赖 haproxy 和 keepalived,也无需 ans ...
- JavaIO学习:字节流
JavaIO流之字节流 字节流 抽象基类:InputStream,OutputStream. 字节流可以操作任何数据. 注意: 字符流使用的数组是字符数组,char[] chs : 字节流使用的数组是 ...
- Redis(九)高可用专栏之Sentinel模式
本文讲述Redis高可用方案中的哨兵模式--Sentinel,RedisClient中的Jedis如何使用以及使用原理. Redis主从复制 Redis Sentinel模式 Jedis中的Senti ...
- golang学习笔记----源码文件
GO源码文件
- go ---MQTT client
Paho GO Client 语言 GO 协议 EPL AND EDL 官网地址 http://www.eclipse.org/paho/ API类型 Asynchronous 描述 Paho ...
- How to get the free disk space in PostgreSQL (PostgreSQL获取磁盘空间)
Get the current free disk space in PostgreSQL PostgreSQL获取磁盘空间 from eshizhan Here has a simple way t ...