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. 【Python网络编程】多线程聊天软件程序

    课程设计的时候制作的多线程聊天软件程序 基于python3.4.3 import socket import pickle import threading import tkinter import ...

  2. Red and Black(poj 1979 bfs)

    Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27891   Accepted: 15142 D ...

  3. load average[zhuan]

    load average值的含义 单核处理器 假设我们的系统是单CPU单内核的,把它比喻成是一条单向马路,把CPU任务比作汽车.当车不多的时候,load <1:当车占满整个马路的时候 load= ...

  4. abiword Related Pages

    Application Framework The 'af' directory contains all source code for the cross-platform application ...

  5. C# is 与 as 运算符

    as运算符有一定的适用范围,它只适用于引用类型或可以为null的类型,而无法执行其他的转换,如值类型的转换以及用户自定义的类型转换,这类转换应该适用强制转换表达式来执行.as当转换不了的时候返回nul ...

  6. C# 字符串驻留池

    在.Net中,对于相同的字符串,.Net会将它们指向同一个地址,它们是相同的实例..Net中的字符串并不会更新,当更改一个字符串变量时,由于字符串的不可变性,.Net实际上是新创建一个字符串,而将变量 ...

  7. 安装 Kali Linux 后需要做的 20 件事

    安装 Kali Linux 后需要做的 20 件事 本文含有我觉得有用的每一件事情.本文分为三大部分: 专门针对Kali用户 Kali Linux是来自Debian的一个特殊版本,Kali Linux ...

  8. 携程SQL面试题忘大牛解答解决思路

    讨论地址:http://bbs.csdn.net/topics/380208742

  9. UESTC_秋实大哥与妹纸 2015 UESTC Training for Data Structures<Problem F>

    F - 秋实大哥与妹纸 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 1500/1500KB (Java/Others) Submit ...

  10. Pascal's Triangle II 解答

    Question Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...