杭电 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 ...
随机推荐
- hdu3926 Hand in Hand 同构图
#include<cstring> #include<cstdio> #include<algorithm> using namespace std; ]; str ...
- spring+redis 报错 org.springframework.core.serializer.support.DeserializingConverter.<init>(Ljava/lang/ClassLoader;)V
这个问题的原因大概就是spring-data-redis.jar包版本不对 ,下面版本可以正常启动 <dependency> <groupId>org.springframew ...
- 洛谷p2922[USACO08DEC]秘密消息Secret Message
题目: 题目链接:[USACO08DEC]秘密消息Secret Message 题意: 给定n条01信息和m条01密码,对于每一条密码A,求所有信息中包含它的信息条数和被它包含的信息条数的和. 分析: ...
- LIS 2015百度之星初赛2 HDOJ 5256 序列变换
题目传送门 题意:中文题面 分析:LIS(非严格):首先我想到了LIS,然而总觉得有点不对:每个数先减去它的下标,防止下面的情况发生:(转载)加入序列是1,2,2,2,3,这样求上升子序列是3,也就是 ...
- 转-eclipse管理多个workspace
Eclipse作为Java开发中最常用的开发工具,大家都很熟悉了,但是,当你做过很多项目后你会发现你的eclipse的package explorer视图下显示的project超级多,这时你可能会关闭 ...
- 144 Binary Tree Preorder Traversal 二叉树的前序遍历
给定一棵二叉树,返回其节点值的前序遍历.例如:给定二叉树[1,null,2,3], 1 \ 2 / 3返回 [1,2,3].注意: 递归方法很简单,你可以使用迭代方法来解决 ...
- 前端编辑神器---sublime text2
个人印象 之前一直在用dreamweaver,但是由于软件太大,加载速度慢,所以sublime text2作为一款跨平台的编辑器,它的优势就展现出来了,它本身小巧,支持代码高亮,语法提示,自动完成,自 ...
- 【转】Android官方架构项目之MVP + Clean
首先,不了解 Clean 架构的可以看看这个,不过也没关系,阅读本文后你也许会对Clean架构思想有一个认识. 对比MVP项目的结构图,我们发现不同之处是新增的这个Domain Layer这层,来隔离 ...
- REST风格笔记
这一篇主要是看了FB的覃超大大的文章,做了一些笔记和自己的思考. 定义: 用URL来定义资源,用HTTP(GET/POST/DELETE/DETC)来描述操作. 1. REST描述的是网络 ...
- logging模块基础
很多程序都有记录日志的需求,日志不仅可以保存访问记录,也可以有错误,警告等信息输出. python的logging模块提供了标准的日志接口,可以通过logging存储各种格式的日志.logging模块 ...