题目链接: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一共多少个
代码如下:
1 # include<iostream> 2 # include<cstdio> 3 # include<vector> 4 # include<queue> 5 # include<algorithm> 6 # define N 100010 7 using namespace std; 8 9 vector<int >e[N]; 10 int dis[N]; 11 bool vis[N]; 12 int main() 13 { 14 int T; 15 int i,n,d,a,b; 16 scanf("%d",&T); 17 while(T--) 18 { 19 scanf("%d%d",&n,&d); 20 for(i=0; i<=n; i++) 21 { 22 e[i].clear(); 23 dis[i] = N; 24 vis[i] = 0; 25 } 26 dis[0] = 0; 27 for(i=1; i<n; i++) 28 { 29 scanf("%d%d",&a,&b); 30 e[a].push_back(b); 31 e[b].push_back(a); 32 } 33 queue<int >q; 34 q.push(0); 35 vis[0] = 1; 36 while(!q.empty()) 37 { 38 int x = q.front(); 39 q.pop(); 40 for(i=0; i<e[x].size(); i++) 41 { 42 int y = e[x][i]; 43 if(!vis[y]) 44 { 45 vis[y] = 1; 46 dis[y] = dis[x] + 1; 47 q.push(y); 48 } 49 } 50 } 51 int ans = 0; 52 for(i=0; i<n; i++) 53 if(dis[i] > d) 54 ans ++; 55 printf("%d ",ans); 56 } 57 return 0; 58 }