hdu4707 Pet (BFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707


#include <cstdio>
#include <cstring>
#include <vector>
#define MAXN 100001
using namespace std;

vector<int> mp[MAXN];
int deep[MAXN*10],Que[MAXN];

int Scan()                      
{                      
	char ch;                      
	int ret=0;                      
	while((ch=getchar())<'0'||ch>'9');                      
	while(ch>='0'&&ch<='9')                      
	{                      
		ret=ret*10+(ch-'0');                      
		ch=getchar();                      
	}                      
	return ret;                      
} 

void BFS(int s)
{
	int v,i,temp,size,front=0,rear=1;
	memset(deep,-1,sizeof(deep));
	deep[s]=0;
	Que[front]=s;
	while(front<rear)
	{
		v=Que[front++];
		size=mp[v].size();
		for(i=0;i<size;++i)
		{
			temp=mp[v][i];
			if(deep[temp]!=-1)
				continue;
			deep[temp]=deep[v]+1;
			Que[rear++]=temp;
		}
	}
}

int main()
{
	int x,y,n,D,test,i,ans;
	scanf("%d",&test);
	while(test--)
	{
		n=Scan();
		D=Scan();
		for(i=0;i<MAXN;++i)
			mp[i].clear();
		for(i=1;i<n;++i)
		{
			x=Scan();
			y=Scan();
			mp[x].push_back(y);
			mp[y].push_back(x);
		}
		BFS(0);
		ans=0;
		for(i=0;i<n;++i)
		{
			if(deep[i]>D)
				ans++;
		}
		printf("%d\n",ans);
	}
	return 0;
}


你可能感兴趣的:(hdu4707 Pet (BFS))