#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 100000;
int T, N, D;
int x, y;
int f[maxn];
void init()
{
for(int i = 0; i <= N - 1; i++)
f[i] = i;
}
int Find(int root)
{
while(root != f[root])
root = f[root];
return root;
}
int Solve(int x)
{
int d = 0;
while(x != f[x])
{
x = f[x];
d++;
}
return d;
}
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &N, &D);
init();
for(int i = 0; i < N - 1; i++)
{
scanf("%d %d", &x, &y);
f[y] = x;
}
int ans = 0;
for(int i = 0; i < N; i++)
{
if(Find(i) == 0)
{
if(Solve(i) > D)
ans++;
}
}
printf("%d\n",ans);
}
}

【Pet HDU - 4707 】【利用并查集找深度】的更多相关文章

  1. 分别利用并查集,DFS和BFS方法求联通块的数量

    联通块是指给定n个点,输入a,b(1<=a,b<=n),然后将a,b连接,凡是连接在一起的所有数就是一个联通块: 题意:第一行输入n,m,分别表示有n个数,有输入m对连接点,以下将要输入m ...

  2. hdu 3536【并查集】

    hdu 3536 题意: 有N个珠子,第i个珠子初始放在第i个城市.有两种操作: T A B:把A珠子所在城市的所有珠子放到B城市.  Q A:输出A珠子所在城市编号,该城市有多少个珠子,该珠子转移了 ...

  3. HDU 2818 (矢量并查集)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...

  4. 利用并查集+贪心解决 Hdu1232

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  5. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

  6. Bipartite Graph hdu 5313 bitset 并查集 二分图

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5313 题意: 给出n个顶点,m条边,问最多添加多少条边使之构成一个完全二分图 存储结构: bitset   ...

  7. hdu 3081(二分+并查集+最大流||二分图匹配)

    Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  8. Virtual Friends HDU - 3172 (并查集+秩+map)

    These days, you can do all sorts of things online. For example, you can use various websites to make ...

  9. HDU 6370 dfs+并查集

    Werewolf Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

随机推荐

  1. CKA认证简介

  2. qt model/view/delegate

    Qt Model/View理解(一)---构造model https://blog.csdn.net/weixin_42303052/article/details/89233887 Qt Model ...

  3. openssl 和libcurl的安装

    /usr/lib/aarch64-linux-gnu/qt5/bin/qmake CONFIG+=release 1 SET(CMAKE_PREFIX_PATH /home/qilin64/Qt5.6 ...

  4. 领域模型/DDD领域驱动设计

    http://www.fanyilun.me/2018/04/08/%E8%B0%88%E8%B0%88%E9%A2%86%E5%9F%9F%E5%BB%BA%E6%A8%A1/ http://www ...

  5. Android Studio 开发

    Android studio安装与配置 (收藏) https://www.cnblogs.com/gufengchen/p/10991886.html ------------------------ ...

  6. c# socket 心跳 重连

    /// <summary> /// 检查一个Socket是否可连接 /// </summary> /// <param name="socket"&g ...

  7. 处理登录时,AJAX的状态码无权限情况

    $.ajaxSetup({ complete: function(XMLHttpRequest, textStatus) { }, error:function(jqXHR,textStatus,er ...

  8. stvd使用中的一些问题

    1.stm8_interrupt_vector.c 会莫名其妙的自动出现,而且都是在项目目录下.进行如下操作 2.stvd编译时遇到no default placement for segment . ...

  9. CF1109F Sasha and Algorithm of Silence's Sounds LCT、线段树

    传送门 构成一棵树可以分成两个限制:图不成环.图的点数-边数=1. 我们考虑枚举右端点\(r\)计算所有可能的左端点\(l\)的答案.我们先考虑第一个限制:图不成环.注意到当\(r\)确定的时候,满足 ...

  10. c# 拼接字符串根据逗号切割 后转换成集合或数组

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qq_27559331/article/d ...