NC14521-珂朵莉的无向图(bfs)

珂朵莉的无向图

题号:NC14521
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述 

珂朵莉给了你一个无向图,每次查询给t个点以及一个常数s,求有多少个图中的点距离给出的那t个点中至少一个距离 <= s

输入描述:

第一行三个数表示n,m,q
之后m行每行两个数u,v表示有一条边位于u和v两个点之间
之后 2 x q 行表示询问
每次询问先输入两个数t,s
之后一行t个数,表示t个特殊点

输出描述:

q行,每行一个数表示答案

示例1

输入

复制

5 6 6
2 3
1 3
2 5
1 3
3 2
2 5
1 1
3
1 1
1
1 4
1
1 2
5
1 4
1
1 4
5

输出

复制

3
2
4
3
4
4

说明

n,m,q<= 5000 ,t的和<= 500000, s <= 109
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long  ll;
#define ls (p<<1)
#define rs (p<<1|1)
#define mid (l+r)/2
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
//    return a>b?a:b;
//}
//inline double min(double a,double b){
//    return a<b?a:b;
//}

int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};

//void Fire(){
//    queue<node> p;
//    p.push({fx,fy,0});
//    memset(fire, -1, sizeof(fire));
//    fire[fx][fy]=0;
//    while(!p.empty()){
//        node temp=p.front();
//        p.pop();
//        for(int i=0;i<8;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m||fire[x][y]!=-1){
//                continue;
//            }
//            fire[x][y]=temp.val+1;
//            p.push({x,y,temp.val+1});
//        }
//    }
//}
//int bfs(){
//    queue<node> p;
//    memset(vis, 0, sizeof(vis));
//    p.push({sx,sy,0});
//    while (!p.empty()) {
//        node temp=p.front();
//        vis[temp.x][temp.y]=1;
//        p.pop();
//        for(int i=0;i<4;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m)  continue;
//            if(x==ex&&y==ey&&temp.val+1<=fire[x][y]) return temp.val+1;
//            if(vis[x][y]||temp.val+1>=fire[x][y]||a[x][y]=='#') continue;
//            p.push({x,y,temp.val+1});
//        }
//    }
//    return -1;
//}
int n,m,q;
int z,x;
int t,s;
int num;
int vis[5050];
int f[5050];
vector<int> a[5050];
queue<int> b;
int bfs(){
    int cnt=0;
    while (!b.empty()) {
        cnt++;
        int x=b.front();
        b.pop();
        for(int i=0;i<a[x].size();i++){
            if(!vis[a[x][i]]){
                vis[a[x][i]]=1;
                f[a[x][i]]=f[x]+1;
                if(f[a[x][i]]<=s){
                    b.push(a[x][i]);
                }
            }
        }
    }
    return cnt;
}
int main()
{
    cin>>n>>m>>q;
    for(int i=0;i<m;i++){
        cin>>z>>x;
        a[z].push_back(x);
        a[x].push_back(z);
    }
    for(int i=0;i<q;i++){
        cin>>t>>s;
        memset(vis, 0, sizeof(vis));
        memset(f, 0, sizeof(f));
        for(int j=0;j<t;j++){
            cin>>num;
            if(!vis[num]){
                vis[num]=1;
                b.push(num);
            }
            
        }
        int ans=bfs();
        cout<<ans<<endl;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是一个无向图 BFS 算法的示例: 假设有如下无向图: ``` 1 -- 2 -- 3 | | | 4 -- 5 -- 6 ``` 我们可以通过 BFS 算法遍历这个无向图,得到遍历的次序。假设我们从节 1 开始遍历,那么遍历过程如下: 1. 将节 1 加入队列,并标记为已访问。 2. 从队列取出节 1,并遍历其所有相邻节,即节 2 和节 4,将这些节加入队列,并标记为已访问。 3. 从队列取出节 2,并遍历其所有相邻节,即节 1、3 和 5。由于节 1 已经被访问过,因此不将其加入队列。将节 3 和节 5 加入队列,并标记为已访问。 4. 从队列取出节 4,并遍历其所有相邻节,即节 1 和节 5。由于节 1 已经被访问过,因此不将其加入队列。将节 5 加入队列,并标记为已访问。 5. 从队列取出节 3,并遍历其所有相邻节,即节 2 和节 6。由于节 2 已经被访问过,因此不将其加入队列。将节 6 加入队列,并标记为已访问。 6. 从队列取出节 5,并遍历其所有相邻节,即节 2、4 和节 6。由于节 2 和节 4 已经被访问过,因此不将其加入队列。将节 6 加入队列,并标记为已访问。 7. 从队列取出节 6,并遍历其所有相邻节,即节 3 和节 5。由于这些节都已经被访问过,不需要将其加入队列。 8. 重复以上步骤,直到队列为空。 综上,通过 BFS 算法遍历无向图可以得到遍历的次序,但无法得到特殊的排序次序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭晋龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值