题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707
题目大意:在一个无环的,从0开始发散状的地图里,找出各个距离0大于d的点的个数
Sample Input
1
10 2
0 1
0 2
0 3
1 4
1 5
2 6
3 7
4 8
6 9
Sample Output
2
分析:从0点开始BFS,给每个点一个距离0点标记,再算出大于距离d一共多少个
代码如下:
# include<iostream>
# include<cstdio>
# include<vector>
# include<queue>
# include<algorithm>
# define N
using namespace std; vector<int >e[N];
int dis[N];
bool vis[N];
int main()
{
int T;
int i,n,d,a,b;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&d);
for(i=; i<=n; i++)
{
e[i].clear();
dis[i] = N;
vis[i] = ;
}
dis[] = ;
for(i=; i<n; i++)
{
scanf("%d%d",&a,&b);
e[a].push_back(b);
e[b].push_back(a);
}
queue<int >q;
q.push();
vis[] = ;
while(!q.empty())
{
int x = q.front();
q.pop();
for(i=; i<e[x].size(); i++)
{
int y = e[x][i];
if(!vis[y])
{
vis[y] = ;
dis[y] = dis[x] + ;
q.push(y);
}
}
}
int ans = ;
for(i=; i<n; i++)
if(dis[i] > d)
ans ++;
printf("%d\n",ans);
}
return ;
}