Problem 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

这个题目是个递推,不过由于是树形的,需要dfs来完成递推的过程。

关键在于p[now] += p[to]+1;如果now能manage to的话。

此处采用链式前向星来保存关系图。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#define LL long long using namespace std; const int maxN = ; struct Edge
{
int to, next;
}edge[maxN]; int head[maxN], cnt; void addEdge(int u, int v)
{
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt;
cnt++;
} void initEdge()
{
memset(head, -, sizeof(head));
cnt = ;
} int n, k;
int fa[maxN], p[maxN]; void input()
{
initEdge();
memset(p, -, sizeof(p));
int u, v;
for (int i = ; i < n; ++i)
{
scanf("%d%d", &u, &v);
addEdge(u, v);
}
} void dfs(int now)
{
p[now] = ;
int to;
for (int i = head[now]; i != -; i = edge[i].next)
{
to = edge[i].to;
if (p[to] == -)
dfs(to);
p[now] += p[to]+;
}
} void work()
{
int ans = ;
for (int i = ; i <= n; ++i)
{
if (p[i] != -)
{
if (p[i] == k)
ans++;
}
else
{
dfs(i);
if (p[i] == k)
ans++;
}
}
printf("%d\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%d%d", &n, &k) != EOF)
{
input();
work();
}
return ;
}

ACM学习历程—HDU 5326 Work(树形递推)的更多相关文章

  1. ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)

    Description A sequence consisting of one digit, the number 1 is initially written into a computer. A ...

  2. ACM学习历程—ZOJ3777 Problem Arrangement(递推 && 状压)

    Description The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem sett ...

  3. ACM学习历程——HDU4472 Count(数学递推) (12年长春区域赛)

    Description Prof. Tigris is the head of an archaeological team who is currently in charge of an exca ...

  4. ACM学习历程—HDU 5451 Best Solver(Fibonacci数列 && 快速幂)(2015沈阳网赛1002题)

    Problem Description The so-called best problem solver can easily solve this problem, with his/her ch ...

  5. ACM学习历程—HDU 5459 Jesus Is Here(递推)(2015沈阳网赛1010题)

    Sample Input 9 5 6 7 8 113 1205 199312 199401 201314 Sample Output Case #1: 5 Case #2: 16 Case #3: 8 ...

  6. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  7. ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...

  8. ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...

  9. ACM学习历程—HDU 5534 Partial Tree(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...

随机推荐

  1. 《JavaScript》——DOM

    DOM (Document Object Model) 即文档对象模型, 针对 HTML 和 XML 文档的 API (应用程序接口) .DOM 描绘了一个层次化的节点树,执行开发者加入.移除和改动页 ...

  2. coreos 之flannel

    提要: coreos 中 flannel 工具是coreos 网络划分工具.通过flannel 划分子网并向etcd 注册网络信息.可以做到宿主机集群中容器间网络通信. 1. 启动etcd2 服务: ...

  3. pooler [转]

    pooler和poolboy都是用erlang写的管理进程池的库. pooler/poolboygithub : seth/pooler · GitHubgithub : devinus/poolbo ...

  4. linux lamp

    1. 用yum安装Apache,Mysql,PHP. 1.1安装Apache yum install httpd httpd-devel 安装完成后,用/etc/init.d/httpd start ...

  5. 设置Eclipse中properties文件打开方式myeclipse一样有source和properties两个视图方法

    东北大亨: 说明:如果想在eclipse的properties文件打开的方式出现source和properties视图就需要添加JBossTools插件 下面介绍如果添加插件: 1.打开官网 http ...

  6. 各种jar包下方法的使用

    commons-codec-1.6.jar: DigestUtils.md5Hex(String str); httpclient-4.2.2.jar: HttpClient client=new D ...

  7. exception_action

    for i in range(3, -2, -1): try: print(4 / i) except Exception as e: print(Exception) print(e)

  8. debug x86 汇编程序指南

    --------------------------------------------------------------------------------------------------- ...

  9. Django—工程创建以及models数据库易错点

    Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...

  10. LeetCode:子集 II【90】

    LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...