Pet

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1809    Accepted Submission(s): 874

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
 
Source

field=problem&key=2013+ACM%2FICPC+Asia+Regional+Online+%A1%AA%A1%AA+Warmup&source=1&searchmode=source">2013 ACM/ICPC Asia Regional Online —— Warmup

求全部房子建一颗树,到根节点距离大于 D的节点的个数

DFS。邻接表存储,,。第一次用邻接表。对于菜鸟的我,

第一次接触还真不好理解。我把我理解半天的邻接表存储的都凝视了

希望对小伙伴们理解有帮助,,

#include<stdio.h>
#include<string.h>
#define M 100100
struct node{
int to,next,w;//w为边权值
}edge[M];
int head[M];
int cnt,n,m,ans;
void add(int x,int y){//邻接表是对每一条边进行编号,从0開始。
edge[cnt].to=y;//edg[cnt].to表示编号为cnt的那条边的终点
edge[cnt].next=head[x];//edge[cnt].next表示和cnt这条边同起点的下一条边的编号
head[x]=cnt++;//head[i]的意思是以i为起点的第一条边的编号,( 事实上就是以i为起点最后输入的那条边的编号,就是逆序第一条边的编号)
}
void dfs(int x,int w){
if(head[x]==-1)
return;
for(int i=head[x]; i!=-1 ;i=edge[i].next){//从以x为起点的最后一个输入的边開始遍历
//i=edge[i].next表示和上一条边同起点的下一条边的编号,依次遍历
int to=edge[i].to;
edge[i].w=w+1;
if(edge[i].w > m)
ans++;
dfs(to,edge[i].w);
}
}
int main(){
int t,i,a,b;
scanf("%d",&t);
while(t--){
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
cnt=0;
for(i=0;i<n-1;i++){
scanf("%d%d",&a,&b);
add(a,b);
}
ans=0;
dfs(0,0);
printf("%d\n",ans);
}
return 0;
}

hdu 4707 Pet(DFS &amp;&amp; 邻接表)的更多相关文章

  1. HDU 4707 Pet 邻接表实现

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707 解题报告:题目大意是在无向图G中有n个点,分别从0 到n-1编号,然后在这些点之间有n-1条边, ...

  2. hdu 4707 Pet(DFS水过)

    http://acm.hdu.edu.cn/showproblem.php?pid=4707 [题目大意]: Lin Ji 的宠物鼠丢了,在校园里寻找,已知Lin Ji 在0的位置,输入N D,N表示 ...

  3. HDU 2586 How far away(LCA+邻接表)

    How far away &题解: 和上篇是一样的题,这用的是lca方法做的, 不知道为什么,把数组开到80000 就a了 >_< 哈 我现在知道为什么了,因为我的rmq数组没有乘 ...

  4. HDU 4707 Pet(DFS(深度优先搜索)+BFS(广度优先搜索))

    Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...

  5. hdu 4707 Pet 2013年ICPC热身赛A题 dfs水题

    题意:linji的仓鼠丢了,他要找回仓鼠,他在房间0放了一块奶酪,按照抓鼠手册所说,这块奶酪可以吸引距离它D的仓鼠,但是仓鼠还是没有出现,现在给出一张关系图,表示各个房间的关系,相邻房间距离为1,而且 ...

  6. HDU 4707 Pet(BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707 题目大意:在一个无环的,从0开始发散状的地图里,找出各个距离0大于d的点的个数 Sample I ...

  7. hdu 4707 Pet【BFS求树的深度】

    Pet                                                          Time Limit: 4000/2000 MS (Java/Others)  ...

  8. HDU 4707:Pet

    Pet Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  9. hdu 4707 Pet hdu 2013 Asia Regional Online —— Warmup

    一道简单的搜索题目,建一个树,根节点是 0 ,连接的两个节点的距离是 1 ,求 到 根节点长度是2的节点的个数. #include<stdio.h> #include<string. ...

随机推荐

  1. 关于php使用xpath解析html中文乱码问题

    $str2 = '<div id="content">我很好 </div>'; $dom = new DOMDocument(); //load之前强转字符 ...

  2. centos7 安装nodejs 最新版

    笔者在安装时,node为11.0.0版本.这里以11版本为例,以后更新,安装步骤时一致的. 下载node安装包到指定目录 wget https://npm.taobao.org/mirrors/nod ...

  3. jmeter压力测试入门

    http://www.51testing.com/html/80/n-853680.html http://blog.csdn.net/vincy_zhao/article/details/70238 ...

  4. Java-替换字符串中的子字符串

    自顶一个repace方法 package com.tj; public class MyClass implements Cloneable { public static void main(Str ...

  5. 大数据学习——azkaban工作流调度系统

    azkaban的安装部署 在/root/apps 1目录下新建azkaban文件夹 上传安装包到azkaban 2解压 .tar.gz 3删掉安装包 [root@mini1 azkaban]# .ta ...

  6. TOJ 2017: N-Credible Mazes

    2017: N-Credible Mazes  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByteTotal Subm ...

  7. POJ 1543 Perfect Cubes

    Perfect Cubes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12595   Accepted: 6707 De ...

  8. POJ 2187 Beauty Contest ——计算几何

    貌似直接求出凸包之后$O(n^2)$暴力就可以了? #include <cstdio> #include <cstring> #include <string> # ...

  9. [BZOJ2287]【POJ Challenge】消失之物(DP)

    传送门 f[i][j]表示前i个物品,容量为j的方案数c[i][j]表示不选第i个物品,容量为j的方案数两个数组都可以压缩到一维 那么f[i][j] = f[i - 1][j] + f[i - 1][ ...

  10. 【kmp+求所有公共前后缀长度】poj 2752 Seek the Name, Seek the Fame

    http://poj.org/problem?id=2752 [题意] 给定一个字符串,求这个字符串的所有公共前后缀的长度,按从小到达输出 [思路] 利用kmp的next数组,最后加上这个字符串本身 ...