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. ...
随机推荐
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode(136) Single Number
题目 Given an array of integers, every element appears twice except for one. Find that single one. Not ...
- 【HIHOCODER 1529】 不上升序列
描述 给定一个长度为 n 的非负整数序列 a[1..n]. 你每次可以花费 1 的代价给某个 a[i] 加1或者减1. 求最少需要多少代价能将这个序列变成一个不上升序列. 输入 第一行一个正整数 n. ...
- Caffe 不同版本之间layer移植方法
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/52185521 (前两天这篇博客不小心被 ...
- vamare下centos7.0 动态获取ip报错问题
CentOS7 Failed to start LSB: Bring up/down解决方法 centos7.0中service network restart重启报错的问题 报错信息: /etc/i ...
- python 列表 字符串 转换
列表转字符串python中的列表l = ['1','2','3','4']转成str型'1,2,3,4'','.join(l)这个方法,列表里都是字符串的话可以这样用.列表里是整数的情况可以用: &g ...
- 洛谷P3758 - [TJOI2017]可乐
Portal Description 给出一张\(n(n\leq30)\)个点\(m(m\leq100)\)条边的无向图.初始时有一个可乐机器人在点\(1\),这个机器人每秒会做出以下三种行为之一:原 ...
- 【leetcode dp】Dungeon Game
https://leetcode.com/problems/dungeon-game/description/ [题意] 给定m*n的地牢,王子初始位置在左上角,公主在右下角不动,王子要去救公主,每步 ...
- polya burnside 专题
polya题目:uva 11077 Find the Permutationsuva 10294 Arif in DhakaLA 3641 Leonardo's Notebookuva 11077 F ...
- POJ1861 Network
Time Limit: 1000MS Memory Limit: 30000KB 64bit IO Format: %lld & %llu Description Andrew is ...