Pet

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2248    Accepted Submission(s): 1098

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
 
Source
 

迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……

#include <iostream>
#include <cmath>
#include <stdio.h>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <iomanip>
#include <algorithm>
#define maxn 100010
using namespace std;
struct P
{
int num;
int dis;
};
int vis[maxn];
vector<int> v[maxn];
int N,D;
void init()
{
for(int i=0; i<=N; i++)
v[i].clear();
}
int bfs()
{
int ans=0;
P tmp,n;
queue<P> q;
while(!q.empty())
q.pop();
tmp.dis=0;
tmp.num=0;
q.push(tmp);
memset(vis,0,sizeof(vis));
while(!q.empty())
{
n=q.front();
q.pop();
if(n.dis>D)ans++;
for(int i=0; i<(int)v[n.num].size(); i++)
{
if(vis[v[n.num][i]]==0)
{
vis[v[n.num][i]]=1;
tmp.dis=n.dis+1;
tmp.num=v[n.num][i];
q.push(tmp);
}
}
}
return ans;
}
int main()
{
int T;
int x,y;
cin>>T;
while(T--)
{
scanf("%d%d",&N,&D);
init();
for(int i=0; i<N-1; i++)
{
scanf("%d%d",&x,&y);
v[x].push_back(y);
}
cout<<bfs()<<endl;
}
return 0;
}

HDU 4707:Pet的更多相关文章

  1. hdu 4707 Pet(DFS水过)

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

  2. HDU 4707 Pet(BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707 题目大意:在一个无环的,从0开始发散状的地图里,找出各个距离0大于d的点的个数 Sample I ...

  3. HDU 4707 Pet 邻接表实现

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707 解题报告:题目大意是在无向图G中有n个点,分别从0 到n-1编号,然后在这些点之间有n-1条边, ...

  4. hdu 4707 Pet【BFS求树的深度】

    Pet                                                          Time Limit: 4000/2000 MS (Java/Others)  ...

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

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

  6. HDU 4707 Pet (水题)

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

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

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

  8. hdu 4707 Pet hdu 2013 Asia Regional Online —— Warmup

    一道简单的搜索题目,建一个树,根节点是 0 ,连接的两个节点的距离是 1 ,求 到 根节点长度是2的节点的个数. #include<stdio.h> #include<string. ...

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

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

随机推荐

  1. MVC ViewModel

    ViewModel这个概念不只是在在MVC模式中有,你会在很多关于MVC.MVP.MVVM的文章中见到这个说法,并且这个概念在任何技术中都有可能提到,比如ASP.NET, Silverlight, W ...

  2. resin 64 & Java install

    [root@vohst voh]# uname -rnvohst 3.10.0-123.el7.x86_64 resin-pro-4.0.41.tar.gz Unrecognized option: ...

  3. devexpress13学习系列(三)PDFViewer(3)

    PdfDocumentProperties Class 该类,用来显示载入的pdf文件的属性,包括:   Name Description Application Indicates the appl ...

  4. CSS 中文字体的英文名称

    宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft JhengHei 新宋体 NSimSun 新细明体 PMingLiU 细明体 Ming ...

  5. 使用visual studio 2012 编译opencv2.4.9

    最近,由于需要从opencv源码部分对opencv中的某个函数进行修改,以提升算法的速度,因此一直在尝试使用vs2012来编译opencv.期间不乏多次的失败.今天通过实验发现了自己编译的opencv ...

  6. Java基础(39):数据的四舍五入、去整、产生随机数---Math类的应用

    使用 Math 类操作数据 Math 类位于 java.lang 包中,包含用于执行基本数学运算的方法, Math 类的所有方法都是静态方法,所以使用该类中的方法时,可以直接使用类名.方法名,如: M ...

  7. SpringMvc项目分析

    首先在配置文件中配置一个视图解析器,视图解析器规定了视图解析的规则,即controller处理请求之后,返回给DispatcheServlet一个字符串(也可能是ModelAndView之类的),而D ...

  8. bzoj1758 [Wc2010]重建计划 & bzoj2599 [IOI2011]Race

    两题都是树分治. 1758这题可以二分答案avgvalue,因为avgvalue=Σv(e)/s,因此二分后只需要判断Σv(e)-s*avgvalue是否大于等于0,若大于等于0则调整二分下界,否则调 ...

  9. Android中使用ShareSDK实现分享

    1,在http://www.mob.com官网上去创建一个后台应用(如果没有账号的同学要去先注册一下),主要后去的是你新创建的应用的appKey值 2,这里我们分享新浪微博为例,在新浪微博的sdk中创 ...

  10. SQL关于apply的两种形式cross apply和outer apply(转载)

    SQL 关于apply的两种形式cross apply 和 outer apply   apply有两种形式: cross apply 和 outer apply   先看看语法:   <lef ...