hdu 4707 Pet(DFS && 邻接表)
Pet
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1809 Accepted Submission(s): 874
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.
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.
1
10 2
0 1
0 2
0 3
1 4
1 5
2 6
3 7
4 8
6 9
2
求全部房子建一颗树,到根节点距离大于 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 && 邻接表)的更多相关文章
- HDU 4707 Pet 邻接表实现
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707 解题报告:题目大意是在无向图G中有n个点,分别从0 到n-1编号,然后在这些点之间有n-1条边, ...
- hdu 4707 Pet(DFS水过)
http://acm.hdu.edu.cn/showproblem.php?pid=4707 [题目大意]: Lin Ji 的宠物鼠丢了,在校园里寻找,已知Lin Ji 在0的位置,输入N D,N表示 ...
- HDU 2586 How far away(LCA+邻接表)
How far away &题解: 和上篇是一样的题,这用的是lca方法做的, 不知道为什么,把数组开到80000 就a了 >_< 哈 我现在知道为什么了,因为我的rmq数组没有乘 ...
- HDU 4707 Pet(DFS(深度优先搜索)+BFS(广度优先搜索))
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...
- hdu 4707 Pet 2013年ICPC热身赛A题 dfs水题
题意:linji的仓鼠丢了,他要找回仓鼠,他在房间0放了一块奶酪,按照抓鼠手册所说,这块奶酪可以吸引距离它D的仓鼠,但是仓鼠还是没有出现,现在给出一张关系图,表示各个房间的关系,相邻房间距离为1,而且 ...
- HDU 4707 Pet(BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707 题目大意:在一个无环的,从0开始发散状的地图里,找出各个距离0大于d的点的个数 Sample I ...
- hdu 4707 Pet【BFS求树的深度】
Pet Time Limit: 4000/2000 MS (Java/Others) ...
- HDU 4707:Pet
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- hdu 4707 Pet hdu 2013 Asia Regional Online —— Warmup
一道简单的搜索题目,建一个树,根节点是 0 ,连接的两个节点的距离是 1 ,求 到 根节点长度是2的节点的个数. #include<stdio.h> #include<string. ...
随机推荐
- ARM-Linux基本开发步骤
拿到一块YC2440(s3c2440)的开发板,经过几天的学习,我对arm-linux系统开发步骤有了一些认识.就以开发这个开发板为例,arm-linux开发工作大概分4个部分 1. 硬件 ...
- Developing
To work with the Android code, you will need to use both Git and Repo. In most situations, you can u ...
- 自己项目使用了ARC,项目中使用第三方库继续引用ARC
Objective-C引入ARC(Automatic Reference Counting)后,我们经常会面对这样一种困境:自己的项目使用了ARC,却发现要使用的第三方类库是non-ARC的:又或者自 ...
- Palindromic Paths(DP)
描述 Given an N×N grid of fields (1≤N≤500), each labeled with a letter in the alphabet. For example: A ...
- 2017 ACM/ICPC Asia Regional Shenyang Online
cable cable cable Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Android ScrollView嵌套GridView导致GridView只显示一行item
Android ScrollView嵌套GridView导致GridView只显示一行item Android ScrollView在嵌套GridView时候,会导致一个问题发生:GridView只显 ...
- Java 线程池的原理与实现学习(二)
java类库中提供的线程池简介: java提供的线程池更加强大,相信理解线程池的工作原理,看类库中的线程池就不会感到陌生了. execute(Runnable command):履行Ruannable ...
- [TJOI2009]开关 (线段树)
题目描述 现有N(2 ≤ N ≤ 100000)盏灯排成一排,从左到右依次编号为:1,2,......,N.然后依次执行M(1 ≤ M ≤ 100000)项操作,操作分为两种:第一种操作指定一个区间[ ...
- java面试题之什么是ThreadLocal?底层如何实现的?
ThreadLocal是一个解决线程并发问题的一个类,用于创建线程的本地变量,我们知道一个对象的所有线程会共享它的全局变量,所以这些变量不是线程安全的,我们可以使用同步技术.但是当我们不想使用同步的时 ...
- scrapy介绍及源码分析
一 简介 Scrapy基于事件驱动网络框架 Twisted 编写.因此,Scrapy基于并发性考虑由非阻塞(即异步)的实现. 官方文档 :https://docs.scrapy.org/en/late ...