lightoj 1111 - Best Picnic Ever(dfs or bfs)
题目链接 http://www.lightoj.com/volume_showproblem.php?problem=1111
题意:给你一个有向图再给你几个人的位置,问所有人可以在哪些点相聚。
简单的搜索题,可以用bfs也可以用dfs,要注意的是存边的时候最好用vector,因为边比较少。
用struct会超时。下面附上dfs和bfs代码。
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <cstdio>
using namespace std;
int a[1100] , map[1100] , vis[1100];
vector<int> s[1100];
void bfs(int st) {
queue<int>q;
q.push(st);
vis[st] = 1;
map[st]++;
while(!q.empty()) {
int gg = q.front();
int len = s[gg].size();
for(int i = 0 ; i < len ; i++) {
if(vis[s[gg][i]] != 1) {
map[s[gg][i]]++;
vis[s[gg][i]] = 1;
q.push(s[gg][i]);
}
}
q.pop();
}
}
int main()
{
int t;
scanf("%d" , &t);
int ans = 0;
while(t--) {
ans++;
int k , n , m;
for(int i = 0 ; i <= 1100 ; i++) {
s[i].clear();
}
memset(map , 0 , sizeof(map));
scanf("%d%d%d" , &k , &n , &m);
for(int i = 0 ; i < k ; i++)
scanf("%d" , &a[i]);
for(int i = 0 ; i < m ; i++) {
int x , y;
scanf("%d%d" , &x , &y);
s[x].push_back(y);
}
for(int i = 0 ; i < k ; i++) {
memset(vis , 0 , sizeof(vis));
bfs(a[i]);
}
int cnt = 0;
for(int i = 1 ; i <= n ; i++) {
//cout << map[i] << endl;
if(map[i] == k)
cnt++;
}
printf("Case %d: %d\n" , ans , cnt);
}
return 0;
}
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <cstdio>
using namespace std;
int a[1100] , map[1100] , vis[1100];
vector<int> s[1100];
void dfs(int st) {
vis[st] = 1;
int len = s[st].size();
for(int i = 0 ; i < len ; i++) {
if(vis[s[st][i]] == 0) {
map[s[st][i]]++;
dfs(s[st][i]);
}
}
return;
}
int main()
{
int t;
scanf("%d" , &t);
int ans = 0;
while(t--) {
ans++;
int k , n , m;
for(int i = 0 ; i <= 1100 ; i++) {
s[i].clear();
}
memset(map , 0 , sizeof(map));
scanf("%d%d%d" , &k , &n , &m);
for(int i = 0 ; i < k ; i++)
scanf("%d" , &a[i]);
for(int i = 0 ; i < m ; i++) {
int x , y;
scanf("%d%d" , &x , &y);
s[x].push_back(y);
}
for(int i = 0 ; i < k ; i++) {
memset(vis , 0 , sizeof(vis));
map[a[i]]++;
dfs(a[i]);
}
int cnt = 0;
for(int i = 1 ; i <= n ; i++) {
//cout << map[i] << endl;
if(map[i] == k)
cnt++;
}
printf("Case %d: %d\n" , ans , cnt);
}
return 0;
}
lightoj 1111 - Best Picnic Ever(dfs or bfs)的更多相关文章
- PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)
1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...
- 2014牡丹江网络zoj3816Generalized Palindromic Number(dfs或者bfs)
#include <iostream> #include <stdio.h> #include <cmath> #include <algorithm> ...
- 蓝桥杯 剪邮票(dfs枚举 + bfs)
剪邮票 如图1, 有12张连在一起的12生肖的邮票.现在你要从中剪下5张来,要求必须是连着的.(仅仅连接一个角不算相连)比如,图2,图3中,粉红色所示部分就是合格的剪取. 请你计算,一共有多少种不同的 ...
- 数据结构(三十二)图的遍历(DFS、BFS)
图的遍历和树的遍历类似.图的遍历是指从图中的某个顶点出发,对图中的所有顶点访问且仅访问一次的过程.通常有两种遍历次序方案:深度优先遍历和广度优先遍历. 一.深度优先遍历 深度优先遍历(Depth_Fi ...
- LeetCode Number of Islands 岛的数量(DFS,BFS)
题意:0代表水,1代表陆地,那么被水围起来的就是岛了,给一个01矩阵,问有多少个岛? 思路:DFS还是比较短,实现了一下.如果一个点已经被遍历过了,那就将其置为0就行了,不要去搜0的. class S ...
- 算法数据结构——数的深搜和广搜(dfs和bfs)
leetcode104 二叉树的最大深度 https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/ 深度搜索分两种:递归(使用栈) ...
- 【数据结构】图的基本操作——图的构造(邻接矩阵,邻接表),遍历(DFS,BFS)
邻接矩阵实现如下: /* 主题:用邻接矩阵实现 DFS(递归) 与 BFS(非递归) 作者:Laugh 语言:C++ ***************************************** ...
- 搜索分析(DFS、BFS、递归、记忆化搜索)
搜索分析(DFS.BFS.递归.记忆化搜索) 1.线性查找 在数组a[]={0,1,2,3,4,5,6,7,8,9,10}中查找1这个元素. (1)普通搜索方法,一个循环从0到10搜索,这里略. (2 ...
- Clone Graph leetcode java(DFS and BFS 基础)
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
随机推荐
- SpringMvc新建实例配置
一.创建项目: 1.建立新的动态web项目: 2.为项目命名为:SpringMVC_01 3.添加tomcat运行时环境\依赖库 如果是MyEclipse的话创建web项目时就不需要此步骤 右键项目 ...
- Kubernetes容器集群管理环境 - Prometheus监控篇
一.Prometheus介绍之前已经详细介绍了Kubernetes集群部署篇,今天这里重点说下Kubernetes监控方案-Prometheus+Grafana.Prometheus(普罗米修斯)是一 ...
- DesignPattern系列__05开闭原则
介绍 开闭原则是编程设计中最基本.最重要的原则. 定义:一个软件实体如类.方法和模块等,应该对扩展(提供方)开放,对修改(使用方)关闭.用抽象构建框架,用实现扩展细节. 也就是说,在需求发生新的变化时 ...
- JavaScript数据结构——字典和散列表的实现
在前一篇文章中,我们介绍了如何在JavaScript中实现集合.字典和集合的主要区别就在于,集合中数据是以[值,值]的形式保存的,我们只关心值本身:而在字典和散列表中数据是以[键,值]的形式保存的,键 ...
- 解决:Navicat连接不上MySQL 8.0
转载自 https://www.cnblogs.com/shiysin/p/shiysin.html Navicat连接不上,总是报错1251: 原因是MySQL8.0版本的加密方式和MySQL5.0 ...
- L1063 能量项链
1 #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i <= b ...
- .Net Core in Docker - 使用阿里云Codepipeline及阿里云容器镜像服务实现持续集成(CI)
前面已经介绍过了 .Net Core 程序发布到 Docker 容器的内容.但是每次通过 SSH 链接到服务器敲命令,运行脚本也是挺麻烦的一件事.程序员是最懒的,能让电脑解决的问题绝不手动解决,如果当 ...
- Spring Security (CORS)跨域资源访问配置
1.CORS介绍 CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing).它允许浏览器向跨源(协议 + 域名 + 端口)服务 ...
- IBM实习工作(一)
2019.1.21 今天的任务是完成会计是否在岗配置表格增加操作记录,任务描述:1. [会计是否在岗配置] 查询结果界面: 修改人编码/修改人/修改时间 字段:2. 字段取值为[会计是否在 ...
- linux安装启动mongodb
1:下载 http://www.mongodb.org/downloads 在85机器上上传压缩包后解压缩. 首先在linux中解压缩安装程序 通过命令操作: 解压 tar -zxvf mongodb ...