题目链接 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. jumpserver1.4.1 安装过程

    # 修改字符集 localedef -c -f UTF-8 -i zh_CN zh_CN.UTF-8 export LC_ALL=zh_CN.UTF-8 echo 'LANG="zh_CN. ...

  2. html的一些基本语法学习与实战

    其实在学校前端开始之前,问过自己为什么要学,因为自己学的比较杂,直到现在刚刚毕业出来工作了,才明确了方向了,要往嵌入式方向走,但是随着时代的发展,在编程和智能硬件结合的越来越紧密,特别是物联网这一块, ...

  3. Docker:跨主机通信

    修改主机docker默认的虚拟网段,然后在各自主机上分别把对方的docker网段加入到路由表中,配合iptables即可实现docker容器夸主机通信.配置方法如下: 设有三台虚拟机 v1: 10.1 ...

  4. codeforces1088D_Ehab and another another xor problem交互题

    传送门 一道考验思维的交互题 大致思路就是从最高的二进制位向下询问 代入例子比如: 5 6 6 5 7 4 6 4 讨论一下 交互题的重点学会推理和归纳 #include <bits/stdc+ ...

  5. 同时启动多个tomcat,端口修改

    所用Tomcat服务器都为zip 版,非安装版.以 tomcat8 为例: 安装第二个Tomcat完成后,打开 tomcat/conf/server.xml 文件,查找以下三处: 1. 修改http访 ...

  6. 深扒JVM,对它进行“开膛破肚”式解析!

    1. 打怪升级,你绕不开JVM JVM,对Java程序员进阶而言,是一个绝对绕不开,也不能绕开的话题. 在你打怪升级.进阶蜕变的路上,势必会遇到项目上线中各种OOM.GC等问题,此时JVM的功底就至关 ...

  7. 以kaldi中的yesno为例谈谈transition

    在基于GMM-HMM的传统语音识别里,比音素(phone)更小的单位是状态(state).一般每个音素由三个状态组成,特殊的是静音(SIL)由五个状态组成.这里所说的状态就是指HMM里的隐藏的状态,而 ...

  8. Spark1——介绍

    1.Spark是什么 Spark是一个用来实现快速而通用的集群计算的平台. 2.Spark是一个大一统的软件栈 Spark项目包含多个紧密集成的组件.首先Spark的核心是一个对由很多计算任务组成的. ...

  9. Android实现多语言so easy

    微信公众号:CodingAndroid CSDN:http://blog.csdn.net/xinpengfei521声明:本文由CodingAndroid原创,未经授权,不可随意转载! 最近,我们公 ...

  10. ubuntu 输出 log 基础

    自定义日志文件 nohup your_command > my_nohup.log 2>&1 & #(将日志输出在my_nohup.log文件中,并将stderr重定向至s ...