题目链接 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)的更多相关文章

  1. 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 ...

  2. 2014牡丹江网络zoj3816Generalized Palindromic Number(dfs或者bfs)

    #include <iostream> #include <stdio.h> #include <cmath> #include <algorithm> ...

  3. 蓝桥杯 剪邮票(dfs枚举 + bfs)

    剪邮票 如图1, 有12张连在一起的12生肖的邮票.现在你要从中剪下5张来,要求必须是连着的.(仅仅连接一个角不算相连)比如,图2,图3中,粉红色所示部分就是合格的剪取. 请你计算,一共有多少种不同的 ...

  4. 数据结构(三十二)图的遍历(DFS、BFS)

    图的遍历和树的遍历类似.图的遍历是指从图中的某个顶点出发,对图中的所有顶点访问且仅访问一次的过程.通常有两种遍历次序方案:深度优先遍历和广度优先遍历. 一.深度优先遍历 深度优先遍历(Depth_Fi ...

  5. LeetCode Number of Islands 岛的数量(DFS,BFS)

    题意:0代表水,1代表陆地,那么被水围起来的就是岛了,给一个01矩阵,问有多少个岛? 思路:DFS还是比较短,实现了一下.如果一个点已经被遍历过了,那就将其置为0就行了,不要去搜0的. class S ...

  6. 算法数据结构——数的深搜和广搜(dfs和bfs)

    leetcode104 二叉树的最大深度 https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/ 深度搜索分两种:递归(使用栈) ...

  7. 【数据结构】图的基本操作——图的构造(邻接矩阵,邻接表),遍历(DFS,BFS)

    邻接矩阵实现如下: /* 主题:用邻接矩阵实现 DFS(递归) 与 BFS(非递归) 作者:Laugh 语言:C++ ***************************************** ...

  8. 搜索分析(DFS、BFS、递归、记忆化搜索)

    搜索分析(DFS.BFS.递归.记忆化搜索) 1.线性查找 在数组a[]={0,1,2,3,4,5,6,7,8,9,10}中查找1这个元素. (1)普通搜索方法,一个循环从0到10搜索,这里略. (2 ...

  9. 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. ...

随机推荐

  1. 基于SpringBoot从零构建博客网站 - 集成editor.md开发发布文章功能

    发布文章功能里面最重要的就是需要集成富文本编辑器,目前富文本编辑器有很多,例如ueditor,CKEditor.editor.md等.这里守望博客里面是集成的editor.md,因为editor.md ...

  2. Fragment 使用详解

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...

  3. spark shuffle写操作之SortShuffleWriter

    提出问题 1. spark shuffle的预聚合操作是如何做的,其中底层的数据结构是什么?在数据写入到内存中有预聚合,在读溢出文件合并到最终的文件时是否也有预聚合操作? 2. shuffle数据的排 ...

  4. 使用RedisMQ 做一次分布式改造

    引言 熟悉TPL Dataflow博文的朋友可能记得这是个单体程序,使用TPL Dataflow 处理工作流任务, 在使用Docker部署的过程中, 有一个问题一直无法回避: 在单体程序部署的瞬间会有 ...

  5. Flink+Druid构建实时OLAP的探索

    场景 k12在线教育公司的业务场景中,有一些业务场景需要实时统计和分析,如分析在线上课老师数量.学生数量,实时销售额,课堂崩溃率等,需要实时反应上课的质量问题,以便于对整个公司的业务情况有大致的了解. ...

  6. Guava cache使用总结

    缓存分为本地缓存和远端缓存.常见的远端缓存有Redis,MongoDB:本地缓存一般使用map的方式保存在本地内存中.一般我们在业务中操作缓存,都会操作缓存和数据源两部分.如:put数据时,先插入DB ...

  7. IPC机制1

    1.Android IPC简介 Inter-Process Communication的缩写就是IPC,含义是进程间通信或是跨进程间通信,是指两个进程进行交换数据的过程. 进程是什么? 进程在pc上就 ...

  8. 单机版ZooKeeper的安装教程

    之前一直没有时间去整理,现在抽出几分钟时间整理以下,有问题的在评论区留言即可. 前期准备JDK环境(ZK需要jdk进行编译,本文以jdk1.8.0_211为例).Linux系统(本文以Centos7为 ...

  9. Promise对象的resolve回调函数和reject回调函数使用

    Promise是ES6中用来结局回调地狱的问题的但是并不能帮我们减少代码量 Promise是一个构造函数 new Promise() 得到一个Promise一个实例 在Promise上有两个函数分别是 ...

  10. 解决微信二次分享失败--后面被加上from=singlemessage&isappinstalled=0的解决方案

    首次分享成功,点开后再次分享或第三次分享就失败了 1.检查你分享的链接,看是否多了两个参数,微信分享会根据分享的不同,为原始链接拼接: 朋友圈   from=timeline&isappins ...