HUD 4707
2015-07-18 21:27
351 查看
Pet
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1726 Accepted Submission(s): 829
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
#include<stdio.h> #include<string.h> #include<algorithm> #include<vector> #include<queue> using namespace std; vector<int> node_tree[100010]; int step[100010]; void bfs() { queue<int> q; q.push(0); step[0] = 0; while(!q.empty()) { int node = q.front(); q.pop(); int Len = node_tree[node].size(); for(int i = 0; i < Len; i++) { int newNode = node_tree[node][i]; if(-1 == step[newNode]) { q.push(newNode); step[newNode] = step[node] + 1; } } } } int main() { int T; scanf("%d", &T); while(T--) { int N,D,x,y,ans=0; scanf("%d%d", &N,&D); memset(step,-1,sizeof(step)); for(int i = 0; i <= N; i++) node_tree[i].clear(); for(int i = 1; i < N; i++) { scanf("%d%d", &x,&y); node_tree[x].push_back(y); node_tree[y].push_back(x); } bfs(); for(int i = 0; i < N; i++) { if(step[i] > D) ans++; } printf("%d\n",ans); } return 0; }
相关文章推荐
- codeforces Gym 100187J J. Deck Shuffling dfs
- jquery动画总结
- ASP.net后台弹出消息对话框的方法!【转】
- 生产者消费者模式(1)
- 【Spark Core】TaskScheduler源码与任务提交原理浅析2
- 10行Java代码实现最近被使用(LRU)缓存
- OpenGL_ES 创建一个平面 绘制纹理 响应输入事件
- POJ1088滑雪(入门级DP)
- bzoj1257[CQOI]余数之和
- ZOJ 2104 Let the Balloon Rise
- poj3984-迷宫问题(广搜)
- Android学习笔记之dispatchTouchEvent和OnInterceptTouchEvent和OnTouchEvent三个方法之间的联系...
- Eddy's research I
- 【Spark Core】TaskScheduler源码与任务提交原理浅析1
- 每个程序员都需要了解的一个SQL技巧
- SWIFT中获取配置文件路径的方法
- 迭代器模式
- git使用的基础及常用命令
- ACdream 1727 Sequence
- 添加view类图中的二级菜单