题目链接 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. Java 设置PDF文档浏览偏好

    在查看PDF文档时,可进行一些浏览偏好设置,例如是否全屏浏览.隐藏或显示菜单栏/工具栏.设置页面布局模式等,下面将通过Java编程的方式来演示如何设置. 使用工具: Free Spire.PDF fo ...

  2. python基础之循环与迭代器

    循环 python 循环语句有for循环和while循环. while循环while循环语法 while 判断条件: 语句 #while循环示例 i = 0 while i < 10: i += ...

  3. c++/c关于函数指针

    顺便提一句:指针也是一种变量类型 和 int double 这些类型是一个级别 不同的是它的值是地址 #include "stdafx.h"#include<stdlib.h ...

  4. Python连载30-多线程之进程&线程&线程使用举例

    一.多线程 1.我们的环境 (1)xubuntu 16.04(2)anaconda(3)pycharm(4)python 3.6 2.程序:一堆代码以文本的形式存入一个文档 3.进程:程序运行的一个状 ...

  5. php 中session_set_cookie_params 和 setcookie 函数的区别与用法

    session_set_cookie_params() 函数不管刷不刷新页面,都不会改变cookie的过期时间, 但setcookie() 函数页面每刷新一次,cookie 的过期时间就会刷新一次. ...

  6. Flink 源码解析 —— 深度解析 Flink 序列化机制

    Flink 序列化机制 https://t.zsxq.com/JaQfeMf 博客 1.Flink 从0到1学习 -- Apache Flink 介绍 2.Flink 从0到1学习 -- Mac 上搭 ...

  7. 100天搞定机器学习|Day22 机器为什么能学习?

    前情回顾 机器学习100天|Day1数据预处理 100天搞定机器学习|Day2简单线性回归分析 100天搞定机器学习|Day3多元线性回归 100天搞定机器学习|Day4-6 逻辑回归 100天搞定机 ...

  8. 7.4 元组tuple类型内置方法

    元组tuple 元组相当于不可变的列表,在定义完成后后面就不可以进行更改,因此元组只可取不可存 因为不可变,所以相对列表来说,他的功能也少了很多,所以,不难理解,他有个优点就是占用内存小. 作用:能够 ...

  9. SQL 分组后,获取每组中的最大值对应的数据

    select gr,num,dt,(select bys from test where gr=b.gr and dt=b.dt) bys from ( select gr,count(0) num, ...

  10. 5G标准公布,你很快又要换手机了

    通常,在4G网络环境下,下载一部1G的电影只需要30秒时间,对于经历过2G和3G网络的我们来说已经非常快了. 但是听说,5G环境中下载一部同样的电影,根本不是用秒来计算的,甚至有外媒说,5G的速率会是 ...