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
 
水题,DFS练手
#include<set>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std; set<int>donser;
vector<int>d[];
int r=,num=,lable=,deep=;
void dfs(int i)
{
if(deep>=r||d[i].empty()){donser.insert(i);return;}
while(d[i].size()>)
{
deep++;
donser.insert(i);
lable=d[i].back();
d[i].pop_back();
dfs(lable);
deep--;
}
}
int main()
{
int a,n,b,T,x,y;
scanf("%d",&T);
for(a=;a<=T;a++)
{
scanf("%d%d",&n,&r);
for(b=;b<n;b++)
{
scanf("%d%d",&x,&y);
d[x].push_back(y);
}
dfs();
cout<<n-donser.size()<<endl;
r=num=lable=deep=;
donser.clear();
memset(d,,sizeof(d));
}
return ;
}

HDU 4707 DFS的更多相关文章

  1. HDU 5143 DFS

    分别给出1,2,3,4   a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...

  2. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

  3. hdu 4707 Pet(DFS水过)

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

  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(DFS &amp;&amp; 邻接表)

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

  7. HDU 4707 Pet 邻接表实现

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

  8. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  9. hdu 4751(dfs染色)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...

随机推荐

  1. VIM for Python and Django Development

    VIM for Python and Django Development VIM-PyDjango created by Programmer for Programmer who work on ...

  2. C#读取Excel,DataTable取值为空的解决办法

    连接字符串这么些就行了 string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + opnFileName ...

  3. (转)Java字符串

    转自:http://blog.sina.com.cn/s/blog_899678b90101brz0.html 创建字符串有两种方式:两种内存区域(字符串池,堆)1," " 引号创 ...

  4. 解决问题--VS2012中一个Panel覆盖另一个Panel时拖动时容易造成两个控件成父子关系的避免

    在*.Designer.cs中,假如想把panel1覆盖到panel2上,但是VS自动让panel1成为panel2的子控件了,在文件中会有this.panel2.Controls.Add(this. ...

  5. 使用docker安装lamp

    docker search -s 10 lamp #搜索被收藏或使用较多的LAMP镜像,小伙伴们都推荐使用tutum/lamp docker pull tutum/lamp #下载镜像 #使用默认方式 ...

  6. SICP— 第一章 构造过程抽象

    SICP  Structure And Interpretation Of Computer Programs 中文第2版 分两部分  S 和 I 第一章 构造过程抽象 1,程序设计的基本元素 2,过 ...

  7. asp.net mvc @Html.Partial @Html.Action @Html.RenderPartial @Html.RenderAction区别

    转载自 :  <asp.net mvc @Html.Partial @Html.Action @Html.RenderPartial @Html.RenderAction区别> 先复制过来 ...

  8. 使用idea开发Springmvc

    使用IntelliJ IDEA开发SpringMVC网站(一)开发环境 http://my.oschina.net/gaussik/blog/385697?fromerr=A4EKE0Ix idea1 ...

  9. C#实现Excel模板导出和从Excel导入数据

    午休时间写了一个Demo关于Excel导入导出的简单练习 1.窗体 2.引用office命名空间 添加引用-程序集-扩展-Microsoft.Office.Interop.Excel 3.封装的Exc ...

  10. hdu4782 Beautiful Soup (模拟)

    http://acm.hdu.edu.cn/showproblem.php?pid=4782 2013成都区域赛全题PDF:http://acm.hdu.edu.cn/downloads/2013Ch ...