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. 知乎模拟登录 requests session

    Python 3.5 # -*- coding: utf-8 -*- """ Created on Wed May 3 16:26:55 2017 @author: x- ...

  2. Appium问题记录

    1.Appium 提示覆盖安装Appium Android Input Manager for Unicode 问题 安卓手机在新版本中Appium 总是提示覆盖安装Appium Android In ...

  3. 简单了解Linux中 du 和 df 以及它们的区别

    一 .du : 显示每个文件和目录的磁盘使用空间~~~文件的大小. 命令参数: -a   #显示目录中文件的大小  单位 KB . -b  #显示目录中文件的大小,以字节byte为单位. -c  #显 ...

  4. 498 Diagonal Traverse 对角线遍历

    详见:https://leetcode.com/problems/diagonal-traverse/description/ C++: class Solution { public: vector ...

  5. PHP pack和unpack函数详解

    pack 压缩资料到位字符串之中. 语法: string pack(string format, mixed [args]...); 返回值: 字符串 函数种类: 资料处理 内容说明 本函数用来将资料 ...

  6. [转]如何使用MFC和类型库创建自动化项目

    本文转自:http://www.cnblogs.com/zhoug2020/archive/2012/04/01/2429064.html 摘要 本文详细介绍了如何自动化像Microsoft Offi ...

  7. 通用maper无法获取实体类com.qmtt.model.PhWxUser对应的表名问题

    spring boot在采用了热加载后,可能会出现“无法获取实体类com.qmtt.model.PhWxUser对应的表名!”的异常, 解决办法 在resources新建一个文件夹META-INF,新 ...

  8. Git之删除分支

    目录 删除本地分支 删除远程分支 删除本地分支: git branch -d dev  [git branch -参数 本地分支名称] 删除远程分支: git push origin --delete ...

  9. Java GC机制简要总结(Java垃圾回收的基本工作原理)

    第一次编辑 2019-05-07 01:09:39 垃圾回收的对象 程序中的不可用对象(不存活的对象,没有任何引用),或者无用的变量信息等,在程序中长期存在会逐渐占用较多的内存空间,导致没有足够的空间 ...

  10. CF622C Not Equal on a Segment

    题目链接: http://codeforces.com/problemset/problem/622/C 题目大意: 给定一个长度为n(n不超过200000)的序列,有m(m不超过200000)次询问 ...