前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Pet HDU - 4707 】【利用并查集找深度】

【Pet HDU - 4707 】【利用并查集找深度】

作者头像
_DIY
发布2019-09-29 15:30:36
3470
发布2019-09-29 15:30:36
举报
代码语言:javascript
复制
#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);
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档