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

大意:
  n个接点从0~n。n-1中关系,问到0节点距离大于k的节点数。
 #include<cstdio>
int n,b,a,k,fa[],i,ans;
int find(int a) //只查询不压缩
{
int r=a;
while(r != fa[r])
{
r=fa[r];
}
return r;
}
int f1(int a) //深度搜索
{
int s=;
while(a != fa[a])
{
a=fa[a];
s++; //s表示到0节点的距离
}
return s;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&k);
for(i = ; i < n ;i++)
{
fa[i]=i;
}
for(i = ; i < n ;i++)
{
scanf("%d %d",&a,&b);
if(a>b) //令大的数为小的数的子节点
{
fa[a]=b;
}
else
{
fa[b]=a;
}
}
ans=;
for(i = n- ; i >= ; i--) //从最大的数开始找(数大的节点在树的下面 )
{
if(find(i) == )
{
if(f1(i) > k)
{
ans++;
}
}
}
printf("%d\n",ans);
}
}

杭电 4707 pet(并查集求元素大于k的集合)的更多相关文章

  1. 杭电--1162--Eddy's picture--并查集

    Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. 杭电 1856 More is better (并查集求最大集合)

    Description Mr Wang wants some boys to help him with a project. Because the project is rather comple ...

  3. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环

    D. Dividing Kingdom II   Long time ago, there was a great kingdom and it was being ruled by The Grea ...

  4. C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块

    C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...

  6. More is better——并查集求最大集合(王道)

    Description Mr Wang wants some boys to help him with a project. Because the project is rather comple ...

  7. 杭电 1213 How Many Tables (并查集求团体数)

    Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...

  8. BC68(HD5606) 并查集+求集合元素

    tree  Accepts: 143  Submissions: 807  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65 ...

  9. 利用并查集求最大生成树和最小生成树(nlogn)

    hdu1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

随机推荐

  1. easyUI Uncaught TypeError: Cannot read property 'length' of undefined

    dataGrid json 封装数据格式为 List<Object> 格式

  2. :not 选择器

  3. 线段树(单点更新) HDOJ 2795 Billboard

    题目传送门 /* 主要利用线段树求区间最值,sum[]代表位置可用空间 每次找到最大值的位置 功能:查询最靠前能容纳广告的位置 */ #include <cstdio> #include ...

  4. LookupError: unknown encoding: idna 的处理方法

    写了一个脚本,想把它打包成exe文件,在python编译器中运行正常,但是打包成.exe文件运行报错 LookupError: unknown encoding: idna 找遍资料终于找到了解决方法 ...

  5. POJ 3522 Slim Span 暴力枚举 + 并查集

    http://poj.org/problem?id=3522 一开始做这个题的时候,以为复杂度最多是O(m)左右,然后一直不会.最后居然用了一个近似O(m^2)的62ms过了. 一开始想到排序,然后扫 ...

  6. zoj3772Calculate the Function(矩阵+线段树)

    链接 表达式类似于斐波那契 但是多了一个变量 不能用快速幂来解 不过可以用线段树进行维护 对于每一个点够一个2*2的矩阵 1 a[i] 1  0   这个矩阵应该不陌生 类似于构造斐波那契的那个数列 ...

  7. JavaScript星级评分,仿百度,增强版

    JavaScript星级评分,仿百度,增强版 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" & ...

  8. ES之基本数据类型之间的显示转换和隐式转换

    typeof(数据)/ typeof 数据 判断数据的数据类型,typeof返回的都是字符串 输出结果类型有:number.string.boolean. undefined.object.funct ...

  9. C++模板类头文件和实现文件分离

    http://www.cnblogs.com/lvdongjie/p/4288373.html 如何实现C++模板类头文件和实现文件分离,这个问题和编译器有关. 引用<<C++primer ...

  10. 51nod 1031 骨牌覆盖

    基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 在2*N的一个长方形方格中,用一个1*2的骨牌排满方格.   问有多少种不同的排列方法.   例如: ...