Nzhtl1477-Zookeeper zookeeper Transaction Processing (BFS)

Source: Internet
Author: User

Nzhtl1477-Zookeeper zookeeper

Description

Question:

You are William! You want to cook a buttered cake for codoori ~!

There are n shops in island 68, and some shops are directly connected by paths. The length of paths is 1.

Glick tells you where there may be ingredients for making butter cakes.

But that person is a poor seller, so

He will tell you some stores, and then tell you the distance between these stores <= K is likely to have raw materials

Then you need to repeat all these stores.

How many shops are you going

Because you are brave, you have m inquiries.

Simple question:

Give you a graph, and each time you query a bunch of special points and a number k, calculate the number of points in the graph and the distance between at least one special point cannot exceed K

The edge is undirected.

Input/Output Format

Input Format:

The three numbers in the first row represent n, m, Q

After m rows, there are two numbers x in each row, and y indicates that there is an edge between the two points ~

Next, I will ask you a question. Each question will first give you a number a and a number K.

The number of A in the next row, indicating a Special Vertex

Output Format:

Row Q. Each row has one number to indicate the answer.

Input and Output sample

Input example #1:Copy

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

Output sample #1:Copy

3

2

4

3

4

4

Description

For 30% of data, n, m, q <= 100, each query only returns one vertex.

For another 30% of data, k = 1

For 100% of data, n, m, q <= 5000, sum of a <= 500000

 

 

Analysis

Online operations: add each vertex to the queue, similar to the modified spfa algorithm;

At the same time, you can add several points to calculate the remaining points based on the distance from their closest center point;

 

#include<bits/stdc++.h>using namespace std;#define ll long long #define rint register intinline int read(){    int x=0,f=0;char ch=getchar();    while(!isdigit(ch)) f=(ch==45),ch=getchar();    while( isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();    return f?(~x+1):x;}#define man 5050struct edge{    int next,to;}e[man<<1];int head[man<<1],num=0;inline void add(int from,int to){    e[++num]=(edge){head[from],to};    head[from]=num;}int n,m,q;int dis[man],vis[man],tot,k;int main(){    memset(dis,63,sizeof(dis));    n=read();m=read();q=read();    for(rint i=1,x,y;i<=m;i++){        x=read();y=read();        add(x,y);add(y,x);    }    for(rint i=1,cnt;i<=q;i++){        queue<int>q;tot=0;        memset(vis,0,sizeof(vis));        memset(dis,63,sizeof(dis));        cnt=read();k=read();        for(rint x,i=1;i<=cnt;i++){            x=read();            q.push(x);dis[x]=0;vis[x]=1;        }        do{            int u=q.front();q.pop();            for(rint i=head[u];i;i=e[i].next){                int to=e[i].to;                dis[to]=min(dis[to],dis[u]+1);                if(!vis[to]) vis[to]=1,q.push(to);            }        }while(q.size());        for(rint i=1;i<=n;i++)            if(dis[i]<=k) tot++;                printf("%d\n",tot);    }    return 0;}

 

Nzhtl1477-Zookeeper zookeeper Transaction Processing (BFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.