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. mysql设置连接等待时间(wait_timeout)

    Linux下mysql修改连接超时   1,首先进入mysql,查看 wait_timeout.interactive_timeout这个值是否为默认的8小时(即 28800)  [root@serv ...

  2. Django 反向生成 从数据库生成Model

    Django 反向生成 从数据库生成Model 使用Django生成Model python manage.py inspectdb或python manage.py inspectdb > m ...

  3. SGU 194 Reactor Cooling

    http://acm.sgu.ru/problem.php?contest=0&problem=194 题意:m条有向边,有上下界,求最大流. 思路:原图中有u-v low[i],high[i ...

  4. SQL省市区三级表结构

    -- 表的结构 areaDROP TABLE  area;CREATE TABLE  area (   id int NOT NULL ,  areaID int NOT NULL,  area va ...

  5. UESTC_Dividing Numbers CDOJ 1156

    Dividing Numbers Time Limit: 9000/3000MS (Java/Others)     Memory Limit: 262144/262144KB (Java/Other ...

  6. C#中,表达式的计算遵循一个规律:从左到右依次计算。

    int i = 0; int j = (i++)+(i++)=(i++)+i=i+++i=i+++i++=1;

  7. MVC Razor视图引擎

    Razor 不是编程语言.它是服务器端标记语言. Razor 是一种允许您向网页中嵌入基于服务器的代码(Visual Basic 和 C#)的标记语法 当网页被写入浏览器时,基于服务器的代码能够创建动 ...

  8. iOS FMDB中的使用

    n使用事务 [queue inTransaction:^(FMDatabase *db, BOOL *rollback) { [db executeUpdate:@"INSERT INTO ...

  9. pyqt字符串分离开,放入列表中

    string1 = ''''' the stirng Has many line In THE fIle ''' list_of_string = string1.split() print list ...

  10. mybatis于Date和DateTime现场插入

    最近,该公司使用MyBatis3做数据持久层,有在该领域Date和DateTime种类,只有在插入数据时属性设置为一个实体Timestamp将相应mysql的DateTime类型.Date会相应mys ...