zoukankan      html  css  js  c++  java
  • 杭电 5326 Work (并查集求子结点为k的结点数)

    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 leader, and all the relationship forms a tree. If A’s title is higher than B(A is the direct or indirect leader of B), we call it A manages B. 
    Now, give you the relation of a company, can you calculate how many people manage k people. 

    Input

    There are multiple test cases. 
    Each test case begins with two integers n and k, n indicates the number of stuff of the company. 
    Each of the following n-1 lines has two integers A and B, means A is the direct leader of B. 

    1 <= n <= 100 , 0 <= k < n 
    1 <= A, B <= n 

    Output

    For each test case, output the answer as described above.

    Sample Input

    7 2
    1 2
    1 3
    2 4
    2 5
    3 6
    3 7

    Sample Output

    2

    大意:
      给你n-1个关系,a直接领导b。问这些人中有多少人可以领导K个人。领导包括直接领导和间接领导。
    思路:
      用数组num[]记录每个结点领导的人数,令i从1~n表示结点,若i不是父结点,让num[fa[i]]+1,表示i上面的结点领导的人数加1,一直加到父结点,到i=n时sun[1~n]表示1~n个结点领导的人数,再找出人数等于k的。
     1 #include<cstdio>
     2 #include<string.h>
     3 #include<algorithm>
     4 using namespace std;
     5 int n,k,i,ans,fa[110],a,b,num[110];
     6 void f1(int a)                        //若a不是父结点,让a上面所有结点都加1;表示领导的人数 
     7 {
     8     while(a != fa[a])
     9     {
    10         num[fa[a]]++;
    11         a=fa[a];
    12     }
    13 }
    14 int main()
    15 {
    16     while(scanf("%d %d",&n,&k)!=EOF)
    17     {
    18         memset(num,0,sizeof(num));
    19         ans=0;
    20         for(i = 1 ; i <= n ; i++)
    21         {
    22             fa[i]=i;
    23         }
    24         for(i = 1 ; i < n ; i++)
    25         {
    26             scanf("%d %d",&a,&b);
    27             fa[b]=a;
    28         }
    29         for(i = 1 ; i <= n ; i++)
    30         {
    31             f1(i);
    32         }
    33         for(i = 1; i<=n;i++)
    34         {
    35             if(num[i] == k)                //找出领导的人数为k的结点 
    36             {
    37                 ans++;
    38             }
    39         }
    40         printf("%d
    ",ans);
    41     }
    42 }
    ——将来的你会感谢现在努力的自己。
  • 相关阅读:
    spring 自定义事件发布及监听(简单实例)
    解析spring中的BeanFactory(看完会有收获)
    如何提高锁的性能
    spring MVC模式拦截所有入口方法的入参出参打印
    java基于feemarker 生成word文档(超级简单)
    数据库事务特性汇总
    如何让window.open()以post请求方式调用(巧妙解法)
    a标签添加背景图片的解决办法
    深入理解Django Admin的list_display, list_filter和raw_id_fields,filter_horizontal选项
    django配置log日志
  • 原文地址:https://www.cnblogs.com/yexiaozi/p/5727272.html
Copyright © 2011-2022 走看看