杭电 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\n",ans);
41     }
42 }
时间: 2024-11-08 05:19:36

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

杭电4496--D-City(***并查集***)

D-City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2649    Accepted Submission(s): 922 Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to D-c

Dragon Balls HDU杭电3635 【并查集,递归的方法找根节点】

Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to gather all of the dragon balls together. His country has N cities and there are exactly N dragon bal

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 Great Arya and Pari The Great. These two had some problems about the numbers they like, so they decided to divide the great kingdom between themselves. The

Codeforces 278C Learning Languages(并查集) 求连通块

Codeforces 278C Learning Languages(并查集) 求连通块 为什么最后还要getfather 一遍 比如 x 是 y 的父亲 然后你 Union(x,z) 然后 z 变成了 x 父亲 然后 y 的祖先就是错的了 题解 求一个无向图中有几个连通块 sum 特判 一下 如果 每一个人的语言都为 0 则答案为 sum 其他 答案则为 sum - 1 1 #include <bits/stdc++.h> 2 using namespace std ; 3 4 const

任意点~并查集求联通块

链接:https://www.nowcoder.com/acm/contest/84/C来源:牛客网 任意点 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可以改变方向. 请问至少需要加多少个点,使得点对之间互相可以到达. 输入描述: 第一行一个整数n表示点数( 1 <= n <= 100).第二行n行,

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 output standard output You are given a tree (a connected undirected graph without cycles) of nn vertices. Each of the n−1n−1 edges of the tree is col

POJ 2761-Feed the dogs(划分树)求区间内第k小的数

Feed the dogs Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 17679   Accepted: 5561 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the

算法题:求链表倒数第K个结点

说明:本文仅供学习交流,转载请标明出处,欢迎转载!  题目:给出一个单链表,返回倒数第K个结点,最后一个结点为倒数第1个. <剑指offer>上面给的解法是设置两个指针,这里记为p1.p2,先让p2走(k-1)步,然后p1.p2同时走,当p2走到最后一个结点时,p1所指向的结点就是倒数第k个结点. 我觉得按照这样的逻辑写代码反而更容易出错,因为我们需要把我两件重要的问题:(1).p2先走(k-1)步:(2)循环结束的条件是p2到达最后一个结点,即p2->next==NULL.显然这样不太

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

Description Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements. Mr Wang selected a room big enough to hold the boys. The boy w