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的。
 #include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
int n,k,i,ans,fa[],a,b,num[];
void f1(int a) //若a不是父结点,让a上面所有结点都加1;表示领导的人数
{
while(a != fa[a])
{
num[fa[a]]++;
a=fa[a];
}
}
int main()
{
while(scanf("%d %d",&n,&k)!=EOF)
{
memset(num,,sizeof(num));
ans=;
for(i = ; i <= n ; i++)
{
fa[i]=i;
}
for(i = ; i < n ; i++)
{
scanf("%d %d",&a,&b);
fa[b]=a;
}
for(i = ; i <= n ; i++)
{
f1(i);
}
for(i = ; i<=n;i++)
{
if(num[i] == k) //找出领导的人数为k的结点
{
ans++;
}
}
printf("%d\n",ans);
}
}

杭电 5326 Work (并查集求子结点为k的结点数)的更多相关文章

  1. 杭电--1162--Eddy's picture--并查集

    Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环

    D. Dividing Kingdom II   Long time ago, there was a great kingdom and it was being ruled by The Grea ...

  3. C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块

    C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...

  5. ACM1229_还是A+B(求A的第K位的数公式:A%((int)(pow(10,K)))

    #include<stdio.h> #include<math.h> int main() { int A,k,B,sum,c,d; while(scanf("%d% ...

  6. 求数列中第K大的数

    原创 利用到快速排序的思想,快速排序思想:https://www.cnblogs.com/chiweiming/p/9188984.html array代表存放数列的数组,K代表第K大的数,mid代表 ...

  7. 杭电 1213 How Many Tables (并查集求团体数)

    Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...

  8. 杭电 1856 More is better (并查集求最大集合)

    Description Mr Wang wants some boys to help him with a project. Because the project is rather comple ...

  9. BC68(HD5606) 并查集+求集合元素

    tree  Accepts: 143  Submissions: 807  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65 ...

随机推荐

  1. 2018 年度码云热门项目排行榜 TOP 10

    2016 年度码云热门项目排行榜 TOP 10 是通过开源项目2016年在码云上的 Watch.Star.Fork 数量来评定的榜单.码云平台发展至今,涌现了越来越多优秀的开源项目,越来越多的开源作者 ...

  2. Spring + MyBaits java.lang.reflect.InvocationTargetException 启动日志报错

    调试发现 实例化 class org.apache.ibatis.logging.slf4j.Slf4jImpl时发生异常,所以 slf4j jar 问题解决: http://www.cnblogs. ...

  3. Common Divisors CodeForces - 182D || kmp最小循环节

    Common Divisors CodeForces - 182D 思路:用kmp求next数组的方法求出两个字符串的最小循环节长度(http://blog.csdn.net/acraz/articl ...

  4. DNS递归查询、主从、加密认证、负载均衡

    环境同DNS练习之正向解析. 在sishen64主机上安装必要软件 [root@sishen64 ~]# yum install -y bind bind-chroot bind-libs bind- ...

  5. 【学习笔记】Base64编码解码原理及手动实现(C#)

    1.[Base64编码原理]@叶落为重生 -base64的编码都是按字符串长度,以每3个8bit的字符为一组,-然后针对每组,首先获取每个字符的ASCII编码,-然后将ASCII编码转换成8bit的二 ...

  6. BOM学习-javascript计时器小结

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  7. Android Studio3.0 Error:Execution failed for task ':app:javaPreCompileDebug' 错误

    Error:Execution failed for task ':app:javaPreCompileDebug'. > Annotation processors must be expli ...

  8. 全文索引Elasticsearch,Solr,Lucene

    最近项目组安排了一个任务,项目中用到了全文搜索,基于全文搜索 Solr,但是该 Solr 搜索云项目不稳定,经常查询不出来数据,需要手动全量同步,而且是其他团队在维护,依赖性太强,导致 Solr 服务 ...

  9. Java String startsWith()方法

    描述: 这个方法有两个变体并测试如果一个字符串开头的指定索引指定的前缀或在默认情况下从字符串开始位置. 语法 此方法定义的语法如下: public boolean startsWith(String ...

  10. sqlserver:查询锁住sql以及解锁

    --查看被锁表:SELECT request_session_id spid, OBJECT_NAME( resource_associated_entity_id ) tableNameFROM s ...