HDU 4707:Pet
Pet
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2248 Accepted Submission(s): 1098
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.
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.
1
10 2
0 1
0 2
0 3
1 4
1 5
2 6
3 7
4 8
6 9
2
迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……
#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的更多相关文章
- hdu 4707 Pet(DFS水过)
http://acm.hdu.edu.cn/showproblem.php?pid=4707 [题目大意]: Lin Ji 的宠物鼠丢了,在校园里寻找,已知Lin Ji 在0的位置,输入N D,N表示 ...
- HDU 4707 Pet(BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707 题目大意:在一个无环的,从0开始发散状的地图里,找出各个距离0大于d的点的个数 Sample I ...
- HDU 4707 Pet 邻接表实现
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707 解题报告:题目大意是在无向图G中有n个点,分别从0 到n-1编号,然后在这些点之间有n-1条边, ...
- hdu 4707 Pet【BFS求树的深度】
Pet Time Limit: 4000/2000 MS (Java/Others) ...
- HDU 4707 Pet(DFS(深度优先搜索)+BFS(广度优先搜索))
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...
- HDU 4707 Pet (水题)
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 4707 Pet(DFS && 邻接表)
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- hdu 4707 Pet hdu 2013 Asia Regional Online —— Warmup
一道简单的搜索题目,建一个树,根节点是 0 ,连接的两个节点的距离是 1 ,求 到 根节点长度是2的节点的个数. #include<stdio.h> #include<string. ...
- hdu 4707 Pet 2013年ICPC热身赛A题 dfs水题
题意:linji的仓鼠丢了,他要找回仓鼠,他在房间0放了一块奶酪,按照抓鼠手册所说,这块奶酪可以吸引距离它D的仓鼠,但是仓鼠还是没有出现,现在给出一张关系图,表示各个房间的关系,相邻房间距离为1,而且 ...
随机推荐
- C# 匿名方法
每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不 ...
- MongoDB分片集群还原
从mongodb 3.0开始,mongorestore还原的时候,需要一个运行着的实例.早期的版本没有这个要求. 1.为每个分片部署一个复制集 (1)复制集中的每个成员启动一个mongod mongo ...
- MVC 学习系列-Controller
MVC最核心的也就是Controller了,控制器(controller)在功能中起到了核心功能. 1,)在MVC类库中,根据URL,通过MVCHandler进入MVC处理系统中, 2,)解析初始化对 ...
- python入门到精通[一]:搭建开发环境
摘要:Python认识,及在windows和linux上安装环境,测试是否安装成功. 1.写在前面 参加工作也有5年多了,一直在做.net开发,近一年有做NodeJS开发.从一开始的不习惯,到逐步适应 ...
- Velocity(1)——注释
Velocity的单行注释,使用## 多行注释使用#* cooments *#
- CMOS and BIOS
1,cmos简介. cmos是主板上一块可读写的RAM芯片.用途:主要用来保存当前系统的硬件配置和操作人员对某些参数的设定.cmos芯片是由一块纽扣电池供电.因此在关机状态内部信息也不会丢失. 2 ...
- PostgreSQL Cascade Replication
PostgreSQL Cascade Replication node1:master:10.2.208.10:repclia(user) node2:upstreamnode:10.2.208.11 ...
- 。。。HibernateTemplate与Session。。。
今天在学习Spring框架的时候,突然发现了这个类----HibernateTemplate,这个类与Session一开始认为是差不多的,这个HibernateTemplate类对象拥有Session ...
- imread函数、namedWindow函数、imshow函数、imwrite函数
1.imread函数 首先,我们看imread函数,可以在OpenCV官方文档中查到其原型如下: Mat imread(const string& filename, int flags=1 ...
- OpenStack 界面开发中的排序问题
Contents [hide] 1 需求 2 调研 3 排序的办法 4 解决代码 需求 获取主机列表的时候,希望能够对主机列表能分组显示,比如网络,一组网络段希望在一起显示 调研 openstack的 ...