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. uva201 Squares

     Squares  A children's board game consists of a square array of dots that contains lines connecting ...

  2. mysql 5.7 内存使用监控

    5.7 中的performance_schema 已经有能力监控mysql 的内存使用情况了,对于这一点也是要通过instrument 来实现的,由于内存这一块没有对应的consumer 所以只要 配 ...

  3. 火狐的打开3D效果

    最近研究网页的时候,想看看一个页面中盒子的层次问题,点击右键查看元素的后,没有发现3D效果的按钮. 在网上百度后说要什么显卡支持,以为是公司的电脑用的是集显,就没有这个功能.回去用自己的笔记本后,发现 ...

  4. Effective Java实作equals() - 就是爱Java

    equals()这个方法,定义在Object class中,这个是所有class的base class,因此所有的class都继承这个方法,默认是比较内存地址,不过Mix需要的是商业规则上的比较,所以 ...

  5. NOR flash和NAND flash区别,RAM 和ROM区别d

    ROM和RAM指的都是半导体存储器,ROM是Read Only Memory的缩写,RAM是Random Access Memory的缩写.ROM在系统停止供电的时候仍然可以保持数据,而RAM通常都是 ...

  6. 一种基于C51单片机的非抢占式的操作系统架构

    摘 要:从Keil C51的内存空间管理方式入手,着重讨论实时操作系统在任务调度时的重入问题,分析一些解决重入的基本方式与方法:分析实时操作系统任务调度的占先性,提出非占先的任务调度是能更适合于Kei ...

  7. 《Programming WPF》翻译 第8章 6.我们进行到哪里了?

    原文:<Programming WPF>翻译 第8章 6.我们进行到哪里了? 动画可以增强应用程序的交互感.它有利于更平滑的转换--当条目出现或消失的时候.它应该,当然,被用于体验和重新着 ...

  8. 【转】 Git 常用命令详解(二)----不错

    原文网址:http://blog.csdn.net/ithomer/article/details/7529022 Git 是一个很强大的分布式版本管理工具,它不但适用于管理大型开源软件的源代码(如: ...

  9. Game of Life 解答

    Question According to the Wikipedia's article: "The Game of Life, also known simply as Life, is ...

  10. 【HDU1394】Minimum Inversion Number(线段树)

    大意:n次操作原串查询逆序数,求出所有串中最小的逆序数. 求逆序数属于线段树的统计问题,建立空树,每次进行插点时进行一次query操作即可.n次操作可以套用结论:如果是0到n的排列,那么如果把第一个数 ...