杭电 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
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
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的结点数)的更多相关文章
- 杭电--1162--Eddy's picture--并查集
Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 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 ...
- 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 ...
- HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...
- 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% ...
- 求数列中第K大的数
原创 利用到快速排序的思想,快速排序思想:https://www.cnblogs.com/chiweiming/p/9188984.html array代表存放数列的数组,K代表第K大的数,mid代表 ...
- 杭电 1213 How Many Tables (并查集求团体数)
Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...
- 杭电 1856 More is better (并查集求最大集合)
Description Mr Wang wants some boys to help him with a project. Because the project is rather comple ...
- BC68(HD5606) 并查集+求集合元素
tree Accepts: 143 Submissions: 807 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...
随机推荐
- 决斗(Headshot )
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; char ...
- c 浮点科学计数法
浮点数 比喻1e1 e后面跟的是10的指数(也就是1的10次方,e表示10次方),f表示浮点数 1e1表示1×10¹,其实就是10 再例如5e2f,表示5×10²,也就是500 =========== ...
- windows密码长度最小值改不了
控制台输入gpedit.msc或者在“开始→控制面板→管理工具→本地安全策略→账户策略→密码策略→密码长度最小值”中修改不了,是灰色的,不让修改 用命令行可以修改开始-->运行-->输入& ...
- HtmlUnit爬取Ajax动态生成的页面内容
HtmlUnit说白了就是一个浏览器,这个浏览器是用Java写的无界面的浏览器,正因为其没有界面,因此执行的速度还是可以滴. HtmlUnit提供了一系列的API,这些API可以干的功能比较多,如表单 ...
- 移动端UI自动化Appium测试——Android系统下使用uiautomator viewer查找元素
在利用Appium做自动化测试时,最重要的一步就是获取对应的元素值,根据元素来对对象进行对应的操作,如何获得对象元素呢?Appium Server Console其实提供了一个界面对话框&qu ...
- asp.net 线程批量导入数据,ajax获取执行状态
最近做了一个批量导入功能,长时间运行,没个反馈状态,很容易让人看了心急,产生各种臆想!为了解决心里障碍,写了这么个功能. 通过线程执行导入,并把正在执行的状态存入session,既共享执行状态,通过a ...
- python工具之myql数据库操作
import pymysql import config ''' 1.0 简单封装 1.1 添加了insert_id属性,返回insert时返回的主键 1.2 添加了column属性,返回查询的col ...
- (4)《Head First HTML与CSS》学习笔记---文本的CSS规则和盒模型;div与span;<a>元素的链接色;伪类
1.每个font-family包含一组共同特征的字体.共五个字体系列: sans-serif----这个系列包括了没有衬线的字体,与serif相比,通常认为这个系列更容易在计算机上识读. serif- ...
- ceph脚本-自动部署计算机节点
依然还在加班中,最近确实忙的脚打后脑勺! 又花了些时间丰富ceph脚本,可以连带着自动部署计算机节点了. 这一部分内容是后加的.可以关注我的公众号获取更多的项目代码和讲解!波神与你同行哦,加油!!!
- NSString 与NSMutableString的区别
NSString 与NSMutableString的区别 Suppose You have a code like this NSString *s = [[NSString alloc] ...