hdu 4707 Pet(DFS水过)

http://acm.hdu.edu.cn/showproblem.php?pid=4707

【题目大意】:

Lin Ji 的宠物鼠丢了,在校园里寻找,已知Lin Ji 在0的位置,输入N D,N表示校园中点的个数,D表示宠物鼠不可能在距离D之内,接下来N-1行,输入x,y,表示x与y相邻,(相邻两点之间的距离为1,不相邻为inf),不存在环结构。

【题解】:

用邻接表存储树形结构,然后用DFS遍历图

【code】:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <vector>
 5 
 6 using namespace std;
 7 #define NN 100010
 8 
 9 struct Nod
10 {
11     int b,dis;
12 }tnd;
13 
14 vector<Nod> G[NN];
15 int ans;
16 
17 void dfs(int s,int dis)
18 {
19     int i;
20     if(dis<0)   ans++;
21     for(i=0;i<G[s].size();i++)
22     {
23         int id = G[s][i].b;
24         dfs(id,dis-1);
25     }
26 }
27 
28 int main()
29 {
30     int t;
31     scanf("%d",&t);
32     while(t--)
33     {
34         int n,d;
35         scanf("%d%d",&n,&d);
36         int i;
37         ans = 0;
38         for(i=0;i<n;i++)
39         {
40             G[i].clear();
41         }
42         for(i=0;i<n-1;i++)
43         {
44             int a,b;
45             scanf("%d%d",&a,&b);
46             tnd.b = b;
47             tnd.dis = 0;
48             G[a].push_back(tnd);
49         }
50         dfs(0,d);
51         printf("%d\n",ans);
52     }
53     return 0;
54 }

 

转载于:https://www.cnblogs.com/crazyapple/p/3310908.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值