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. ...
随机推荐
- druid0.15.0安装方式
Druid0.15.0安装文档 1 集群规划 Master包含Coordinator和Overlord,4核16G*2: data包含Historical和MiddleManager,16核64G*3 ...
- solr使用心得
/** * @author zhipeng * @date 创建时间:2015-10-10 下午12:15:35 * @parameter * @return */ publ ...
- Linux vim基本的使用方法
一.vim 的三种模式 (1) 插入模式 在插入模式中,才能输入文字:要进入插入模式,可以按键 “i”:如果要进入插入模式时,直接切换到下一行,可以输入“o”: (2) 命令模式 在命令模式中,主要进 ...
- java并发程序和共享对象实用策略
java并发程序和共享对象实用策略 在并发程序中使用和共享对象时,可以使用一些实用的策略,包括: 线程封闭 只读共享.共享的只读对象可以由多个线程并发访问,但任何线程都不能修改它.共享的只读对象包括不 ...
- F#周报2019年第31期
新闻 现在开始接受FSSF的第七次师友计划申请 Xamarin播客:XAML热重载 TorchSharp:将PyTorch引擎带入.NET 视频及幻灯片 F#中的异步编程2/3--实现异步工作流 ML ...
- Centos7 搭建owncloud云存储
Centos7 搭建owncloud云存储 首先准备必要的软件和资料. 这里我已经整理好了: 百度云共享 不过最好还是自己去官网上下.这里只不过是提供了快捷方式. owncloud官网:https:/ ...
- java课堂_动手动脑4
1.请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? 答:在Java中,内容相同的字串常量(“Hello”)只保存一份以节约内存,所以s0 ...
- C#的委托事件总结
什么是委托?1.委托是C#中由用户自定义的一个类型.2.类表示的是数据和方法的集合,而委托实际上是一个能持有对某个或某些方法的引用的类.3.与其他的类不同,委托类能拥有一个签名,并且他只能持有与他的签 ...
- 【杂项】关于NOIP2018复赛若干巧合的声明
导言 参加NOIP2018时本人学龄只有两个月,却斩获了省一等奖,保送了重点中学,这看上去是个我创造的神话,然而,在我自己心中,我认为这只是个巧合(其实我认为运气也是实力的一部分),接下来,我将说明一 ...
- android ——活动的生命周期
在其生命周期内,activity在运行.暂停和停止三种可能的状态间进行转换,不同状态之间互相转换的时候的调用不同的方法,重写这些方法就能在活动切换,被销毁时保存或传输数据,在被启动.被切换出来时接收数 ...