Pet

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1809    Accepted Submission(s): 874

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
 
 
 
这是个搜索题,我用并查集也过了,明天会附上搜索代码!
 
 
 
 
 #include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 100100
using namespace std;
int p[maxn],a,b;
void init()
{
int i;
for(i=;i<a;i++)
p[i]=i;
}
int find(int x)
{
int t=x;
while(t!=p[t])//不用压缩路径
t=p[t];
return t;
}
int fun(int x)
{
int i=x,num=;
while(i!=p[i])
{
//p[i]=find(i);//查找x的深度
i=p[i];
num++;
}
return num;
} int main()
{
int N,n,i,x,y,cot;
scanf("%d",&N);
while(N--)
{ cot=;
scanf("%d%d",&a,&b);
init();
for(i=;i<a-;i++)
{
scanf("%d%d",&x,&y);
p[y]=x;
}
for(i=a-;i>=;i--)
{
if(find(i)==)
if(fun(i)>b)
cot++;
}
printf("%d\n",cot);
}
return ;
}

Pet--hdu4707的更多相关文章

  1. HDU4707:Pet(DFS)

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

  2. hdu4707 Pet

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

  3. UAT SIT QAS DEV PET

    UAT: User Acceptance Testing 用户验收测试SIT: System Integration Testing 系统集成测试PET: Performance Evaluation ...

  4. get a new level 25 battle pet in about an hour

    If you have 2 level 25 pets and any level 1 pet, obviously start with him in your lineup. Defeat all ...

  5. 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 ...

  6. HDU 4707:Pet

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

  7. Microsoft .NET Pet Shop 4

    Microsoft .NET Pet Shop 4:将 ASP.NET 1.1 应用程序迁移到 2.0 299(共 313)对本文的评价是有帮助 - 评价此主题 发布日期 : 2006-5-9 | 更 ...

  8. Pet

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

  9. asp.net的3个经典范例(ASP.NET Starter Kit ,Duwamish,NET Pet Shop)学习资料

    asp.net的3个经典范例(ASP.NET Starter Kit ,Duwamish,NET Pet Shop)学习资料 NET Pet Shop .NET Pet Shop是一个电子商务的实例, ...

  10. Pet(hdu 4707 BFS)

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

随机推荐

  1. Android Studio Linking an external C++ project 时候 报Invalid file name. Expected: CMakeLists.txt

    Android Studio 右键Linking an external C++ project 时候 报Invalid file name. Expected: CMakeLists.txt错误 查 ...

  2. 如何在jQuery中使用 setInterval,setTimeout

    当遇到setInterval,setTimeout与jquery混用的问题 时,直接按JavaScript中的语法写并不起作用,有以下两种解决方法. 方法1. 直接在ready中调用其他方法,会提示缺 ...

  3. [转]MySQL 5.6 全局事务 ID(GTID)实现原理(一)

    原文作者:淘长源 原文连接:http://qing.blog.sina.com.cn/1757661907/68c3cad333002qhe.html 转载注明以上信息   MySQL 5.6 的新特 ...

  4. grep;egrep;fgrep

    -1 使用场景:搜索定位内容并输出(所在行) -2 三者区别: --1 grep  默认支持普通正则 --2 egrep 默认支持扩展正则 等同于 grep -E --3 fgrep 速度最快,不支持 ...

  5. 蜘蛛牌(hdu 1584 DFS)

    蜘蛛牌 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. MVC4 + EF + System.Threading.Thread 出现的问题记录

    项目要求是页面监测到后台数据库用户数据(Users)变化,前台做出相应的响应和操作. 一.参考很多资料,大概有几种方式: 参考资料地址:http://www.cnblogs.com/hoojo/p/l ...

  7. ASP.NET MVC 4.0 学习4-Code First

    之前我們需要用到的數據,通過添加Entity實體數據模型把數據庫中需要的Database拉到項目中如下圖, 而就是Code First就是相對於這種處理數據的方法而言的 Code First更加準確的 ...

  8. QObject的event函数就可以改写对消息的处理

    一个既自己处理Tab事件又自己处理某些按键事件,然后将其它不需自己处理的事件转发给基类处理: bool MyWidget::event(QEvent *event){ if (event->ty ...

  9. spring-boot+nginx+tomcat+ssl配置笔记

    如果你的tomcat应用需要采用ssl来加强安全性,一种做法是把tomcat配置为支持ssl,另一种做法是用nginx反向代理tomcat,然后把nginx配置为https访问,并且nginx与tom ...

  10. express设置ejs并将后缀改为html

    http://www.cnblogs.com/-nothing-/p/4943354.html http://blog.csdn.net/macyang/article/details/8841966 ...