HDU4707:Pet(DFS)
10 2
0 1
0 2
0 3
1 4
1 5
2 6
3 7
4 8
6 9
题意:所有房子组成一颗树,求出离根节点0的距离大于d的节点数目
思路:建树深搜
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; struct node
{
int next,to;
int step;
} a[100005]; int head[100005];
int n,d,len,ans; void add(int x,int y)
{
a[len].to = y;
a[len].next = head[x];
head[x] = len++;
} void dfs(int x,int step)
{
int i,j,k;
if(-1 == head[x])
return ;
for(i = head[x]; i!=-1; i = a[i].next)
{
k = a[i].to;
a[i].step = step+1;
if(a[i].step>d)
ans++;
dfs(k,a[i].step);
}
} int main()
{
int T,i,j,x,y;
scanf("%d",&T);
while(T--)
{
memset(head,-1,sizeof(head));
memset(a,0,sizeof(a));
scanf("%d%d",&n,&d);
len = 0;
for(i = 1; i<n; i++)
{
scanf("%d%d",&x,&y);
add(x,y);
}
ans = 0;
dfs(0,0);
printf("%d\n",ans);
} return 0;
}
HDU4707:Pet(DFS)的更多相关文章
- hdu4707 Pet
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...
- Pet(dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=4707 题意:判断距离大于D的点有多少个. 思路: 邻接表建图,dfs每一个点,记录步数. #include &l ...
- Pet(dfs+vector)
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 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(DFS && 邻接表)
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- hdu 4707 Pet(DFS水过)
http://acm.hdu.edu.cn/showproblem.php?pid=4707 [题目大意]: Lin Ji 的宠物鼠丢了,在校园里寻找,已知Lin Ji 在0的位置,输入N D,N表示 ...
- hdu 4707 Pet 2013年ICPC热身赛A题 dfs水题
题意:linji的仓鼠丢了,他要找回仓鼠,他在房间0放了一块奶酪,按照抓鼠手册所说,这块奶酪可以吸引距离它D的仓鼠,但是仓鼠还是没有出现,现在给出一张关系图,表示各个房间的关系,相邻房间距离为1,而且 ...
- HDU 4707 DFS
Problem Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He ...
- hduoj 4707 Pet 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4707 Pet Time Limit: 4000/2000 MS (Java/Others) Memory ...
随机推荐
- C# Best Practices - Handling Strings
Features Strings Are Immutable. A String Is a Reference Type Value Type Store their data directly Ex ...
- AIX LVM学习笔记
LVM: LOGIC VOLUMN MANAGEMENT (逻辑卷管理器) 通过将数据在存储空间的 逻辑视图 与 实际的物理磁盘 之间进行映射,来控制磁盘资源.实现方式是在传统的物理设备驱动层之上加载 ...
- tasklet和工作队列
tasklet机制和工作队列 http://blog.chinaunix.net/uid-28236237-id-3450753.html tasklet原理 http://www.kuqin.com ...
- JavaScript基础知识----document对象
对象属性document.title //设置文档标题等价于HTML的<title>标签document.bgColor //设 ...
- YII2 实现登录时候修改最新登录时间
YII2 实现登录时候修改最新登录时间 YII2保存最新登录时间主要技巧:为 EVENT_AFTER_LOGIN 事件绑定一个方法,在方法中保存最新时间 public function login() ...
- leetcode 15. 3Sum 双指针
题目链接 给n个数, 找出三个数相加结果为0的所有的组, 不可重复. 用双指针的思想,O(n^2)暴力的找, 注意判重复. class Solution { public: vector<vec ...
- 我的Python成长之路---第六天---Python基础(20)---2016年2月20日(晴)
一.面向对象基础 面向对象名词解释: 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公 ...
- poj 2661 Factstone Benchmark
/** 大意: 求m!用2进制表示有多少位 m! = 2^n 两边同时取对数 log2(m!) = n 即 log2(1) + log2(2)+log2(3)+log2(4)...+log2(m) = ...
- Chromium如何显示Web页面
Displaying A Web Page In Chrome 概念化的应用分层 参见原文档:http://goo.gl/MsEJX 每一个box代表一个抽象层.下层不依赖于上层. WebKit:渲染 ...
- android:background="@drawable/home_tab_bg"
android:background="@drawable/home_tab_bg" home_tab_bg/xml: <bitmap xmlns:android=" ...