Pet

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

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

给定一个树,根节点为0,找到距离根节点大于d的节点的个数。bfs搜索

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
int n,d;
vector<int > map[+];
int vis[+];
struct node
{
int x;
int t;
}tem,top;
vector<int>::iterator p;
void bfs()
{
int i,j;
queue <node> s;
top.x=,top.t=;
s.push(top);
while(!s.empty())
{
top=s.front();
s.pop();
if(top.t==d)
continue;
vis[top.x]=;
for(p=map[top.x].begin();p!=map[top.x].end();p++)
{
if(!vis[*p])
{
tem.x=*p;
tem.t=top.t+;
vis[*p]=;
s.push(tem);
}
}
}
}
int main()
{
int T;
int s,t;
int ans;
freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&d);
memset(vis,,sizeof(vis));
for(int i=;i<n;i++) map[i].clear();
for(int i=;i<n-;i++)
{
scanf("%d%d",&s,&t);
map[s].push_back(t);
map[t].push_back(s);
}
ans=;
bfs();
for(int i=;i<n;i++)
{
if(!vis[i])
{
ans++;
//cout<<i<<" ";
}
}
printf("%d\n",ans);
}
}

Pet(hdu 4707 BFS)的更多相关文章

  1. 逃离迷宫(HDU 1728 BFS)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. HDU 5094 题解(状压BFS)

    题面: Maze 题目中文大意: 这个故事发生在“星际迷航”的背景下. “星际争霸”的副队长史波克落入克林贡的诡计中,被关押在他们的母亲星球Qo’noS上. 企业的上尉詹姆斯·T·柯克(James T ...

  3. 搜索(另类状态BFS):NOIP 华容道

    描述 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面,华容道是否根本就无法完成,如果能完成,最少需要多少时间. 小 B 玩的华容道与经典的 ...

  4. Codeforces1076D. Edge Deletion(最短路树+bfs)

    题目链接:http://codeforces.com/contest/1076/problem/D 题目大意: 一个图N个点M条双向边.设各点到点1的距离为di,保证满足条件删除M-K条边之后使得到点 ...

  5. PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)

    1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...

  6. UVA Planning mobile robot on Tree树上的机器人(状态压缩+bfs)

    用(x,s)表示一个状态,x表示机器人的位置,s表示其他位置有没有物体.用个fa数组和act数组记录和打印路径,转移的时候判断一下是不是机器人在动. #include<bits/stdc++.h ...

  7. CF 986A Fair(多源BFS)

    题目描述 一些公司将在Byteland举办商品交易会(or博览会?).在Byteland有 nnn 个城市,城市间有 mmm 条双向道路.当然,城镇之间两两连通. Byteland生产的货物有 kkk ...

  8. Codeforces H. Kilani and the Game(多源BFS)

    题目描述: Kilani and the Game time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  9. [BZOJ1195]:[HNOI2006]最短母串(AC自动机+BFS)

    题目传送门 题目描述 给定n个字符串(S1,S2,…,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,…,Sn)都是T的子串. 输入格式 第一行是一个正整数n,表示给定的字符串的个数 ...

随机推荐

  1. Bootstrap 模态对话框 remote指定内容加载

    第一个页面: .....其他内容..... <div class="modal" id="ID_ReformDetail"> <div cla ...

  2. 递归实现全排列序列C语言实现

    大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang 以下鄙人用递归回溯的办法,采用C语言实现了全排列序列,用以某些优化方案的原始方案的给定 #incl ...

  3. memcached在Windows下的安装

    memcached简介详情请谷歌.这里介绍如何在windows下安装. 1.下载     下载地址:http://download.csdn.net/detail/u010562988/9456109 ...

  4. Delphi的核心优势:三快一多

    快是指:开发快(RAD开发),编译快(好像是没有用到LL(1),从而避免了潜在的歧义),运行快(原生代码). 多是指:开发符合PME标准的可视化控件,易开发,易使用,所以第三方控件特别多,这个不用解释 ...

  5. Spring AOP 本质(4)

    这一主要看看Spring AOP是如何实现通知包围的.   Spring AOP包围通知在功能上和前置通知加后置通知类似,但还是有区别的:包围通知可以修改返回值,还可以阻止.替换目标方法的执行.   ...

  6. android网址

    1. 优麒麟手机助手 https://github.com/ubuntu-kylin/phone_manager 2. http://forum.xda-developers.com/index.ph ...

  7. wikioi1191 数轴染色

    题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色.接着 我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色.请输出每个操作执行后 剩余黑色点的个 ...

  8. Hdu3072-Intelligence System(强连通求最小值)

    After a day, ALPCs finally complete their ultimate intelligence system, the purpose of it is of cour ...

  9. Unity 开发游戏Game分辨率设置

    最近自己开发小游戏,突然又被Game视图中设置分辨率被诱惑了, 我到底该怎么设置分辨率设置的图片才能让电脑和手机尺寸显示的大小一模一样呢? 然后又被手机尺寸和分辨率迷惑了! =.= 越搞越混   分辨 ...

  10. python高级编程之选择好名称:pepe8和命名最佳实践

    # # -*- coding: utf-8 -*- # # python:2.x # __author__ = 'Administrator' # my_list=['a','b','c','d'] ...