HDU4707:Pet(DFS求树深)
来源:互联网 发布:c语言如何输出汉字 编辑:程序博客网 时间:2024/05/17 01:04
Pet
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2762 Accepted Submission(s): 1362
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
110 20 10 20 31 41 52 63 74 86 9
Sample Output
2
Source
2013 ACM/ICPC Asia Regional Online —— Warmup
题意:一个无回路图,n个点,n-1条路,求距离0号点大于D的节点数。
# include <iostream># include <cstdio># include <vector># define MAXN 100000using namespace std;vector<int>a[MAXN+1];int n, d;int dfs(int pre, int now, int dep){ int ans = 0; for(int i=0; i<a[now].size(); ++i) if(a[now][i] != pre) ans += dfs(now, a[now][i], dep+1); return ans + (dep > d);}int main(){ int t, x, y; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&d); for(int i=0; i<n; ++i) a[i].clear(); while(--n) { scanf("%d%d",&x,&y); a[x].push_back(y); a[y].push_back(x); } printf("%d\n",dfs(-1, 0, 0)); } return 0;}
0 0
- HDU4707:Pet(DFS求树深)
- hdu4707 Pet(bfs & dfs,vector)
- HDU4707:Pet(DFS)
- HDU4707:Pet(DFS)
- HDU4707 Pet(dfs)
- hdu4707 Pet【DFS&邻接表】
- hdu4707 Pet
- Pet--hdu4707
- hdu4707 Pet
- HDU4707 pet
- hdu4707 Pet (BFS)
- hdu4707——Pet
- 【HDU4707】Pet(暴搜)
- hdu4707 Pet 图
- VJ pet(DFS)
- hdu4707 hdu1241-dfs&bfs
- HDOJ 题目4707 Pet(DFS)
- hdu 4707 Pet(DFS && 邻接表)
- 蚂蚁笔记的快捷键
- MySQL视图
- python中时间日期格式化符号
- css设定文本超出一行或多行就隐藏并显示省略号
- Jeewx-api 1.1 版本发布,微信极速 SDK
- HDU4707:Pet(DFS求树深)
- Linux下用GDB调试程序崩溃错误
- boolan——c++学习笔记之STL简介
- 分布式服务框架学习笔记2 常用的分布式服务框架 与 通信框架选择
- BZOJ 4320(ShangHai2006 Homework-询问分段+并查集)
- 深入理解Java Servlet与Web容器之间的关系
- 读取某个文件夹下指定前缀和后缀的文件,并且返回指定文件的功能性Demo
- 数据预处理
- 直销系统框架(ASP.NET)之二