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. 并发调用get请求

    http://zeusami.iteye.com/blog/1172864 package com.alibaba.xteam.web.travel.module.rpc; import java.i ...

  2. jsp中${param.user}不解析,原样输出。

    没加<%@ page isELIgnored="false"%>

  3. mysql 查看是否存在某一张表

    判断表是否存在 SELECT table_name FROM information_schema.TABLES WHERE table_name ='yourname'; 或者 SHOW TABLE ...

  4. Leetcode: Rotate Function

    Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotati ...

  5. 20145207《Java程序设计》第三周学习总结

    前言 24号回来的,书看的差不多了,博客一直没写,求老师原谅呀!!!!!哈哈哈哈.博客我从今天开始补,对着书,一天最多能弄个两篇毕竟写这个东西挺费心思德,当然我做事慢也有关系.但是我会尽快的.老实讲, ...

  6. [原创]java WEB学习笔记90:Hibernate学习之路-- -HQL检索方式,分页查询,命名查询语句,投影查询,报表查询

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  7. Ruby操作Excel的方法与技巧大全

    测试工作中,批量的数据通常会放到excel表格中,测试输出的数据写回表格中,这样输入输出易于管理,同时清晰明了 使用ruby来操作excel文件首先需要在脚本里包含以下语句 require'win32 ...

  8. sql ROUND() 函数三个参数的含义

    ROUND的格式:ROUND(p1,p2,p3),其作用是取四舍四入值P1:要被四舍五入的数字P2:保留的小数位数P3:如果为0或不输入,则表示进P1进入四舍五入,如ROUND(123.86,1) = ...

  9. 2. 星际争霸之php面向对象(二)

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  10. 《聚焦3D地形编程》学习点

    痞子龙的译本虽然称不上好,但却保留了原汁原味,看这本书时最好结合原文与痞子龙的译文.另外,如果有过地形生成的经验再看这本书时有些帮助,这本书介绍的专业的室外地形开发,很全面的介绍. 仅是个人总结,可能 ...