杭电 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 ...
随机推荐
- C#程序结构与基本语法
C# 程序结构 Hello World 实例 一个 C# 程序主要包括以下部分: 命名空间声明(Namespace declaration) 一个 class Class 方法 Class 属性 一个 ...
- Hdu 5459 Jesus Is Here (2015 ACM/ICPC Asia Regional Shenyang Online) 递推
题目链接: Hdu 5459 Jesus Is Here 题目描述: s1 = 'c', s2 = 'ff', s3 = s1 + s2; 问sn里面所有的字符c的距离是多少? 解题思路: 直觉告诉我 ...
- 洛谷1736(二维dp+预处理)
洛谷1387的进阶版,但很像. 1387要求是“全为1的正方形”,取dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1]))吧?这个有“只有对 ...
- 优先队列 POJ 3253 Fence Repair
题目传送门 题意:一块木板按照某个顺序切成a[1], a[2]...a[n]的长度,每次切都会加上该两段木板的长度,问选择什么顺序切能使得累加和最小 分析:网上说这是哈夫曼树.很容易想到先切掉最长的, ...
- 看Facebook是如何优化React Native性能
原文出处: facebook 译文出处:@Siva海浪高 该文章翻译自Facebook官方博客,传送门 React Native 允许我们运用 React 和 Relay 提供的声明式的编程模型, ...
- Suricata的初始化脚本
见官网 https://suricata.readthedocs.io/en/latest/initscripts.html
- PDO相关函数
(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0) PDO::__construct — 创建一个表示数据库连接的 PDO 实例 说明 PDO::__co ...
- Git之提交项目到远程github
1.在分支dev下,默认本地工作区有项目project 2. git add project [添加项目到暂存区] 3. git commit project -m "提交项目" ...
- 《精通css》笔记
第2章 选择器,注释 1.要知道常用选择器(id选择器,类选择器,类型选择器,后代选择器,伪类选择器(文档结构之外)) 通用选择器(*{ }) 高级选择器(子选择器,相邻同胞选择器,属性选 ...
- android开发工具eclipse的安装与配置
l开发主要应用Eclipse 3.7版本. l辅助工具为jdk.Androidsdk Android环境搭建 –1.1.JDK安装 –1.2.Eclipse安装 –1.3.Android SDK安 ...