Problem Description
One day, Lin Ji wake up in the morning and found that his pethamster escaped. He searched in the room but didn’t find the hamster. He tried to use some cheese to trap the hamster. He put the cheese trap in his room and waited for three days. Nothing but cockroaches was caught. He got the map of the school and foundthat there is no cyclic path and every location in the school can be reached from his room. The trap’s manual mention that the pet will always come back if it still in somewhere nearer than distance D. Your task is to help Lin Ji to find out how many possible locations the hamster may found given the map of the school. Assume that the hamster is still hiding in somewhere in the school and distance between each adjacent locations is always one distance unit.
 
Input
The input contains multiple test cases. Thefirst line is a positive integer T (0<T<=10), the number of test cases. For each test cases, the first line has two positive integer N (0<N<=100000) and D(0<D<N), separated by a single space. N is the number of locations in the school and D is the affective distance of the trap. The following N-1lines descripts the map, each has two integer x and y(0<=x,y<N), separated by a single space, meaning that x and y is adjacent in the map. Lin Ji’s room is always at location 0.

 
Output
For each test case, outputin a single line the number of possible locations in the school the hamster may be found.
 
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的距离大于d的节点数目

思路:建树深搜

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; struct node
{
int next,to;
int step;
} a[100005]; int head[100005];
int n,d,len,ans; void add(int x,int y)
{
a[len].to = y;
a[len].next = head[x];
head[x] = len++;
} void dfs(int x,int step)
{
int i,j,k;
if(-1 == head[x])
return ;
for(i = head[x]; i!=-1; i = a[i].next)
{
k = a[i].to;
a[i].step = step+1;
if(a[i].step>d)
ans++;
dfs(k,a[i].step);
}
} int main()
{
int T,i,j,x,y;
scanf("%d",&T);
while(T--)
{
memset(head,-1,sizeof(head));
memset(a,0,sizeof(a));
scanf("%d%d",&n,&d);
len = 0;
for(i = 1; i<n; i++)
{
scanf("%d%d",&x,&y);
add(x,y);
}
ans = 0;
dfs(0,0);
printf("%d\n",ans);
} return 0;
}

HDU4707:Pet(DFS)的更多相关文章

  1. hdu4707 Pet

    Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...

  2. Pet(dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=4707 题意:判断距离大于D的点有多少个. 思路: 邻接表建图,dfs每一个点,记录步数. #include &l ...

  3. Pet(dfs+vector)

    Pet Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. HDU 4707 Pet(DFS(深度优先搜索)+BFS(广度优先搜索))

    Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...

  5. hdu 4707 Pet(DFS &amp;&amp; 邻接表)

    Pet Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  6. hdu 4707 Pet(DFS水过)

    http://acm.hdu.edu.cn/showproblem.php?pid=4707 [题目大意]: Lin Ji 的宠物鼠丢了,在校园里寻找,已知Lin Ji 在0的位置,输入N D,N表示 ...

  7. hdu 4707 Pet 2013年ICPC热身赛A题 dfs水题

    题意:linji的仓鼠丢了,他要找回仓鼠,他在房间0放了一块奶酪,按照抓鼠手册所说,这块奶酪可以吸引距离它D的仓鼠,但是仓鼠还是没有出现,现在给出一张关系图,表示各个房间的关系,相邻房间距离为1,而且 ...

  8. HDU 4707 DFS

    Problem Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He ...

  9. hduoj 4707 Pet 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4707 Pet Time Limit: 4000/2000 MS (Java/Others)    Memory ...

随机推荐

  1. 修改项目工程名 iOS

    结合这两篇原文: http://www.cocoachina.com/ios/20150104/10824.html http://jingyan.baidu.com/article/0964eca2 ...

  2. Longest Substring Without Repeating Characters - 哈希与双指针

    题意很简单,就是寻找一个字符串中连续的最长包含不同字母的子串. 其实用最朴素的方法,从当前字符开始寻找,找到以当前字符开头的最长子串.这个方法猛一看是个n方的算法,但是要注意到由于字符数目的限制,其实 ...

  3. Joel在耶鲁大学的演讲

    Joel Spolsky是一个美国的软件工程师,他的网络日志"Joel谈软件"(Joel on Software)非常有名,读者人数可以排进全世界前100名. 上个月28号,他回到 ...

  4. QQ对话代码

    <li> <strong class="QQ">客服QQ</strong> <span> <a href="mqqw ...

  5. 使用JS进行pc端、手机端判断

     <script type="text/javascript">            (function(){                var ua = nav ...

  6. Go与Docker的几本书的作者

    http://thenewstack.io/ebookseries/http://thenewstack.io/building-a-web-server-in-go/

  7. 「操作系统」: Conditional Move Instructions(trap)

    Not all conditional expressions can be compiled using conditional moves. Most significantly, the abs ...

  8. cocos2dx进阶学习之CCLayer

    继承关系 CCLayer -> CCNode,CCTouchDelegate,CCAccelerometerDelegate,CCKeypadDelegate CCLayer在cocos2dx中 ...

  9. 如何修改Sublime 侧边栏Sidebar的颜色

    参考自:http://blog.csdn.net/a497393102/article/details/10563791 首先要找到 Default.sublime-theme 文件, 点击 subl ...

  10. wiki oi 1044 拦截导弹

    题目描述 Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某 ...