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. (转)在Xcode 7上直接使用Clang Address Sanitizer

    原文地址: http://www.cocoachina.com/ios/20150730/12830.html WWDC 2015上,除了Swift 2.0外,还有一个令人激动的消息:可以直接在Xco ...

  2. VIJOS1476 旅行规划(树形Dp + DFS暴力乱搞)

    题意: 给出一个树,树上每一条边的边权为 1,求树上所有最长链的点集并. 细节: 可能存在多条最长链!最长链!最长链!重要的事情说三遍 分析: 方法round 1:暴力乱搞Q A Q,边权为正-> ...

  3. 【Oracle】使用dblink+minus方式迁移数据

    一.需求说明 1.数据库a104的表syssde.a4_syssde_department中有1000条数据: 2.数据库a230的表syssde.a4_syssde_department中有800条 ...

  4. 百度蜘蛛IP段分析

    大家进行网站日志分析的时候,常见到很多不同IP段的百度蜘蛛,为了方便大家更好的进行日志分析,下面列举了百度不同IP段常见蜘蛛的一些详情情况,及所谓的降权蜘蛛,沙盒蜘蛛,高权重蜘蛛等等 下面的百度蜘蛛I ...

  5. 【LeetCode】Maximize Sum Of Array After K Negations(K 次取反后最大化的数组和)

    这道题是LeetCode里的第1005道题. 题目描述: 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次. ...

  6. A. Light Bulb

    A. Light Bulb Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 32768KB   64-bit integer IO f ...

  7. 奇奇怪怪的冒泡排序 TOJ 2014: Scramble Sort

    粘贴两个特别简单的冒泡排序 2014: Scramble Sort Description In this problem you will be given a series of lists co ...

  8. 九度oj 题目1250:矩阵变换

    题目描述: 对于一个整数矩阵,存在一种运算,对矩阵中任意元素加一时,需要其相邻(上下左右)某一个元素也加一, 现给出一正数矩阵,判断其是否能够由一个全零矩阵经过上述运算得到. 输入: 输出: 如果可以 ...

  9. IntelliJ IDEA 代码提示快捷键

    1.写代码时用Alt-Insert(Code|Generate…)可以创建类里面任何字段的getter与setter方法. mac版 是ctrl+enter 2.CodeCompletion(代码完成 ...

  10. 暑假训练Round1——G: Hkhv的水题之二(字符串的最小表示)

    Problem 1057: Hkhv的水题之二 Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format: ...