珂朵莉的无向图
题号: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;
}
}