#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. springboot:非web启动

    需要运行一些调度任务,但是又不想放到web容器中运行. 见红色代码: import java.util.concurrent.ThreadPoolExecutor; import org.spring ...

  2. VS2017/VS 2019查看源代码

    通过VS2017/VS 2019使用F12 查看DLL源代码   今天在一本书中偶然看到原来VS2017中是可以查看dll中的源码,具体步骤是:工具>选项>文本编辑器>c#>高 ...

  3. 使用Qt画出直方图和分位数图

    https://blog.csdn.net/gudanai/article/details/72136420

  4. unity 在editor编辑器下创建多个按钮

    入坑最后发现就几句话,真是气死人了.发现自己掉坑里,脑子就变笨了,把代码贴给你们,保持大脑清醒. using System.Collections; using System.Collections. ...

  5. LeetCode 421. 数组中两个数的最大异或值(Maximum XOR of Two Numbers in an Array) 71

    421. 数组中两个数的最大异或值 421. Maximum XOR of Two Numbers in an Array 题目描述 给定一个非空数组,数组中元素为 a0, a1, a2, - , a ...

  6. SGU 126. Boxes --- 模拟

    <传送门> 126. Boxes time limit per test: 0.25 sec. memory limit per test: 4096 KB There are two b ...

  7. 同类控件的统一操作(以TCHECKBOX为例)

    https://www.cnblogs.com/gaodu2003/archive/2008/12/15/1355355.html Procedure UnCheck; var i: integer; ...

  8. js 数组传统方法

    js 数组传统方法 push() 功能:向数组的末尾添加一个或多个元素 var arr = [4]; arr.push(1,2,3); // [4,1,2,3] 返回: 会返回新数组的长度length ...

  9. linux上文件的上传和下载

    现整理一篇linux上文件的上传和下载 第一种方式就是在windos上安装工具 如: 工具如何使用我就不赘述了,easy 第二种方式就是使用liux的命令(首先是文件上传) 上传文件(首先创建文件夹如 ...

  10. 原!linux comm命令文件 比较 输出交集,差集。

    文件内容大致如下: 112805|300011222483|OL海12卓|47397c33e36cdbed26637c50dd305973|2019-08-06 10:50:13|登B录123|suc ...