题意:中文题;

思路:就是找某个点距离其他点的距离,他给你很多点也无所谓。用一个dist【】数组,这个数组保存的是他给你的点到其他点的最短距离且标记的作用,然后bfs搜索就行了。

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<set>
#include<map>
#include<queue>
#include<vector>
#define ll long long int
#define mod 1000000007
#define me(a,b) memset(a,b,sizeof(a))
const int inf=0x7fffffff;
const int maxn=10010;
using namespace std;
int head[maxn];
int cnt;
int dist[maxn];
struct node
{
int to;
int next;
}edge[maxn];
void add(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int main()
{
int s,t,n,m,q,x;
int u,v;
int ans;
me(head,-1);
cnt=0;
queue<int>qu;
scanf("%d%d%d",&n,&m,&q);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
while(q--)
{
scanf("%d%d",&t,&s);
me(dist,-1);ans=0;
while(t--)
scanf("%d",&x),dist[x]=0,qu.push(x);//将所有给你的点都存进一个数组;
while(!qu.empty())
{
int z=qu.front();
qu.pop();
for(int i=head[z];i!=-1;i=edge[i].next)//假设某个点距离第一个点距离为2,那么第一次for到不了这个点,如果第二个点距离这个点为1,第二次for确定距离;
{
if(dist[edge[i].to]==-1)
dist[edge[i].to]=dist[z]+1,qu.push(edge[i].to);
}
}
for(int i=1;i<=n;i++)
{
if(dist[i]!=-1&&dist[i]<=s)
ans++;
}
printf("%d\n",ans);
}
return 0;
}

牛客网练习赛7-D-无向图(bfs,链式前向星)的更多相关文章

  1. zzuli 2130: hipercijevi 链式前向星+BFS+输入输出外挂

    2130: hipercijevi Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 595  Solved: 112 SubmitStatusWeb B ...

  2. poj-1459-最大流dinic+链式前向星-isap+bfs+stack

    title: poj-1459-最大流dinic+链式前向星-isap+bfs+stack date: 2018-11-22 20:57:54 tags: acm 刷题 categories: ACM ...

  3. 链式前向星写法下的DFS和BFS

    Input 5 7 1 2 2 3 3 4 1 3 4 1 1 5 4 5 output 1 5 3 4 2 #include<bits/stdc++.h> using namespace ...

  4. 链式前向星BFS

    本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5399068 采用链式前向星的BFS: #include <iostream> #incl ...

  5. 链式前向星存树图和遍历它的两种方法【dfs、bfs】

    目录 一.链式前向星存图 二.两种遍历方法 一.链式前向星存图:(n个点,n-1条边) 链式前向星把上面的树图存下来,输入: 9 ///代表要存进去n个点 1 2 ///下面是n-1条边,每条边连接两 ...

  6. 牛客网挑战赛24 青蛙(BFS)

    链接:https://www.nowcoder.com/acm/contest/157/E来源:牛客网 有一只可爱的老青蛙,在路的另一端发现了一个黑的东西,想过去一探究竟.于是便开始踏上了旅途 一直这 ...

  7. 牛客网练习赛23 F 托米的游戏

    链接:https://www.nowcoder.com/acm/contest/156/F 来源:牛客网 题目描述 题目背景编不下去了 托米有一棵有根树 T, 树根为1,每轮他会在剩下的子树中等概率一 ...

  8. 牛客网练习赛28A

    题目链接:https://www.nowcoder.com/acm/contest/200/A 链接:https://www.nowcoder.com/acm/contest/200/A来源:牛客网 ...

  9. 牛客网练习赛18 A 【数论/整数划分得到乘积最大/快速乘】

    链接:https://www.nowcoder.com/acm/contest/110/A 来源:牛客网 题目描述 这题要你回答T个询问,给你一个正整数S,若有若干个正整数的和为S,则这若干的数的乘积 ...

  10. 牛客网练习赛26B(简单的dp)

    题目链接:https://www.nowcoder.com/acm/contest/180/B 链接:https://www.nowcoder.com/acm/contest/180/B来源:牛客网 ...

随机推荐

  1. PAT A1017 Queueing at Bank (25 分)——队列

    Suppose a bank has K windows open for service. There is a yellow line in front of the windows which ...

  2. JAVA 第七周学习总结

    20175308 2018-2019-2 <Java程序设计>第七周学习总结 教材学习内容总结 本周学习第八章:常用实用类 String类: String类位于java.lang包中,被定 ...

  3. Ubuntu下 AndroidStudio 无法识别设备的问题

    最近想搞一下 Android的软件开发 于是下决心,开始研究ubuntu 环境下android studio 的配置.对于我这个对APP一无所知的技术小白,还是遇到很多的问题. 1.先拔掉数据线,按下 ...

  4. Android2.3系统 自定义的PopupWindow在实例化时报空指针异常

    情况:是这样的,前段时间做了一个自定义的PopupWindow,就是写一个类,然后继承PopupWindow,别的什么操作都没有,但是在实例化的时候,在2.3系统中直接就报空指针异常(4.0及以上系统 ...

  5. oa tomcat 代码处理跨域问题

    meta标签处理http.https跨域 <!-- 将http请求转成https请求 --> <meta http-equiv="Content-Security-Poli ...

  6. java算法----排序----(1)插入排序

    package log; public class Test4 { /** * java算法---插入排序 * * @param args */ public static void main(Str ...

  7. maven 单独构建多模块项目中的单个模块

    maven 单独构建多模块项目中的单个模块, maven选项说明 -pl, --projects Build specified reactor projects instead of all pro ...

  8. HNOI2019 白兔之舞 dance

    HNOI2019 白兔之舞 dance 显然\(n=3\)就是\(n=1\)的扩展版本,先来看看\(n=1\)怎么做. 令\(W=w[1][1]\),显然答案是:\(ans_t=\sum_{i\mod ...

  9. Jmeter(三十一)_数据驱动,业务关联

    这种数据驱动的本质是:将测试的case,参数,url,预期结果,存储于本地excel中.运行脚本时,从文件中获取预期结果,将实际结果与预期结果比较,将实际结果,比较结果,响应状态码回写excel. 一 ...

  10. 001_IntelliJ IDEA详细安装步骤

    安装IntelliJ IDEA 一.安装JDK 1 下载最新的jdk,这里下的是jdk-8u66 2 将jdk安装到默认的路径C:\Program Files\Java目录下 二.安装IntelliJ ...