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. spring @InitBinder

    /** * 将字符串日期转化为Date类型 * @param binder */ @InitBinder protected void initBinder(WebDataBinder binder) ...

  2. _bzoj1257 [CQOI2007]余数之和sum【小技巧】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1257 最近刚做了一道莫比乌斯的题,需要用到这种方法. 应该让k / i相等的一连串k % i ...

  3. 题解报告:poj 1738 An old Stone Game(区间dp)

    Description There is an old stone game.At the beginning of the game the player picks n(1<=n<=5 ...

  4. eclipse 当安装jad仍然不能反编译,提示attach source的时候

    当安装jad仍然不能反编译,提示attach source的时候,其实是当前workspace有问题了: 所使用的workspace目录下.metadata\.mylyn会出现一个.tasks.xml ...

  5. AJPFX关于面向对象之封装,继承,多态 (下)

    (3)private: 对于对于成员来说:只能在该成员隶属于的类中访问. 对于类来说:类不可以声明为private. 4)protected: 对于对于成员来说:相同包中的类可以访问(包访问权限):基 ...

  6. ceph脚本-自动部署计算机节点

    依然还在加班中,最近确实忙的脚打后脑勺! 又花了些时间丰富ceph脚本,可以连带着自动部署计算机节点了. 这一部分内容是后加的.可以关注我的公众号获取更多的项目代码和讲解!波神与你同行哦,加油!!!

  7. Android一键锁屏APP

    题记: 这个app完全是拾人牙慧,作为练手用的,其实没有什么原创的东西.当然,博客还是我自己写的,记录下来,对自己也算是一种成长吧. 转载请注明原文地址: http://www.cnblogs.com ...

  8. 初试springWebMVC

    最近在尝试配置SpringMVC,发现各种坑. 首先遇到了这个问题. 'component-scan' and its parser class [org.springframework.contex ...

  9. vue-devtools在google浏览器下安装扩展

    下载vue-devtools,地址: https://github.com/vuejs/vue-devtools 解压到对应目录,eg: D:\ProgramFiles\vue-devtools-de ...

  10. js中json处理总结之JSON.parse

    踩过的坑都将成为路上的风景.队友在cookie中已存以下值: address_info {"address_name":"人民大会堂","...lng ...