hdu5326 Work

题目链接:点击打开链接

题目大意:有n个人。各自管理不同的人,问有多少人管理k个人。

思路:先记录每一个人的直接上级。然后模拟路径压缩的过程进行计数求和。

#include<stdio.h>
#include<string.h>
int p[105],sum[105];
void init()
{
    int i;
    for(i=0;i<105;i++)
    {
        p[i]=i;
        sum[i]=1;
    }
}
void findroot(int x)
{
    int r=x;
    while(r!=p[r])
    r=p[r];
    int i,j;
    i=x;
    while(i!=r)
    {
        j=p[i];
        sum[p[i]]+=1;
    //    p[i]=r;
        i=j;    //模拟路径压缩过程,注意不要真的进行路径压缩。
    }
}
int main()
{
    int n,k,i,j,a,b;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        init();
        for(i=1;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            if(a!=b)
            p[b]=a;
        }
        for(i=1;i<=n;i++)
        {
            findroot(i);
        }
        int ans=0;
        for(i=1;i<=n;i++)
        {
            sum[i]--;
        //printf("%d\n",sum[i]);
            if(sum[i]==k)ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-11 06:35:40

hdu5326 Work的相关文章

解题报告 之 HDU5326 Work

解题报告 之 HDU5326 Work Description It's an interesting experience to move from ICPC to work, end my college life and start a brand new journey in company. As is known to all, every stuff in a company has a title, everyone except the boss has a direct le

hdu5326 Work(树形dp)

题意:n个人,有n-1个关系,表示A管理B,求管理人数为K的人数.(间接上司也算上司) #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #inc

hdu5326(多校)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5326 Work Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1147 Accepted Submission(s): 695 Problem Description It's an interesting experience to move f

多校联赛第三场(部分题解)

1001. Magician (hdu5316) 这个题目用到线段树来合并区间,题目有句话的意思A beautiful subsequence is a subsequence that all the adjacent pairs of elves in the sequence have a different parity of position说的是相邻的两个元素必须有不同的奇偶位置,所有一个序列有四种可能的形式,奇...奇, 奇...偶,偶...奇, 偶...偶,这四种形式只是端点的奇