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搜索

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <queue>
  5. #include <vector>
  6. using namespace std;
  7. int n,d;
  8. vector<int > map[+];
  9. int vis[+];
  10. struct node
  11. {
  12. int x;
  13. int t;
  14. }tem,top;
  15. vector<int>::iterator p;
  16. void bfs()
  17. {
  18. int i,j;
  19. queue <node> s;
  20. top.x=,top.t=;
  21. s.push(top);
  22. while(!s.empty())
  23. {
  24. top=s.front();
  25. s.pop();
  26. if(top.t==d)
  27. continue;
  28. vis[top.x]=;
  29. for(p=map[top.x].begin();p!=map[top.x].end();p++)
  30. {
  31. if(!vis[*p])
  32. {
  33. tem.x=*p;
  34. tem.t=top.t+;
  35. vis[*p]=;
  36. s.push(tem);
  37. }
  38. }
  39. }
  40. }
  41. int main()
  42. {
  43. int T;
  44. int s,t;
  45. int ans;
  46. freopen("in.txt","r",stdin);
  47. scanf("%d",&T);
  48. while(T--)
  49. {
  50. scanf("%d%d",&n,&d);
  51. memset(vis,,sizeof(vis));
  52. for(int i=;i<n;i++) map[i].clear();
  53. for(int i=;i<n-;i++)
  54. {
  55. scanf("%d%d",&s,&t);
  56. map[s].push_back(t);
  57. map[t].push_back(s);
  58. }
  59. ans=;
  60. bfs();
  61. for(int i=;i<n;i++)
  62. {
  63. if(!vis[i])
  64. {
  65. ans++;
  66. //cout<<i<<" ";
  67. }
  68. }
  69. printf("%d\n",ans);
  70. }
  71. }

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. Jasper_mainReport_excel html pdf 主报表中常用属性

    jasper中,excel , html, pdf 一般可以使用相同的主报表和子报表.需要在主报表中添加不同格式对应的属性.导出不同格式的报表,编译器会将相应的属性应用到对应的报表格式中. 常用属性如 ...

  2. c# 弹出框-后台调前台函数

    前台代码: <script src="../../../Common/Scripts/Order/popup.js" type="text/javascript&q ...

  3. web后台获取不到session中的值(loading sessions from persistent storage),后改用JS传值

    线上的程序似乎从session中取不到domain数据,重启了一下tomcat查看log日志发现,居然有报错.错误信息如下 22-Sep-2016 00:52:16.562 SEVERE [local ...

  4. 5W1H分析法

    "5W1H分析法"也叫"六何分析法",它是一种分析方法也可以说是一种创造技法.是对选定的项目.工序和操作,都要从原因(Why).对象(What).地点(Wher ...

  5. Windows 8.1 正式版 MSDN第二版 官方简体中文/英文版 (专业版/企业版)

    说明:文件名cn开头的是简中版文件名en开头的是英文版文件名含x64的为64位版本文件名含x86的为32位版本文件名含enterprise的为企业版文件名含pro_vl的为专业批量授权版文件名不含en ...

  6. KEIL的宏汇编器A51介绍

    A51是一种具有通用特性和用法的重定位宏汇编器.它与Intel公司的MASM51宏汇编器具有很好兼容性,支持模块化编程,可以方便地与高级语言接口.A51宏汇编器支持汇编伪指令.宏处理指令以及汇编控制命 ...

  7. 关于BitmapFactory.decodeStream(is)方法无法正常解码为Bitmap对象的解决方法

    在android sdk 1.6版本API帮助文档中,其中关于BitmapFactory.decodeFactory.decodeStream(InputStream is)的帮助文档是这么说明的: ...

  8. 程序减肥,strip,eu-strip 及其符号表

    程序减肥,strip,eu-strip 及其符号表 我们要给我们生成的可执行文件和DSO瘦身,因为这样可以节省更多的磁盘空间,所以我们移除了debug信息,移除了符号表信息,同时我们还希望万一出事了, ...

  9. STL中istream_iterator和ostream_iterator的基本用法

    标准程序库定义有供输入及输出用的iostream iterator类,称为istream_iterator和ostream_iterator,分别支持单一型别的元素读取和写入.使用这两个iterato ...

  10. windows media player 中播放pls的方法

    windows media player目前只能播放 wpl 和 asm格式的列表文件.而linux下mplayer和vlc支持的pls,很遗憾没法支持. 不过,老外写了个“open pls in w ...