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. MySQL技术内幕InnoDB存储引擎(表&索引算法和锁)

    表 4.1.innodb存储引擎表类型 innodb表类似oracle的IOT表(索引聚集表-indexorganized table),在innodb表中每张表都会有一个主键,如果在创建表时没有显示 ...

  2. 认识 service worker

    离线缓存可以提升用户体验,可以节省网络资源,但是,浏览器对资源缓存和自定义网络请求的控制一直不够完善,service worker 的出现就是为了解决这些问题 它可以解决目前离线应用的问题,同时也可以 ...

  3. Android-Android进程间通讯之messenger

    转自‘https://www.cnblogs.com/makaruila/p/4869912.html 平时一说进程间通讯,大家都会想到AIDL,其实messenger和AIDL作用一样,都可以进行进 ...

  4. Android 红色小圆球提示气泡 BadgeView

    今天给大家分享两个实用有简单的一个小圆球提示气泡: BadgeView 参考地址: https://github.com/qstumn/BadgeView;       个人地址:http://git ...

  5. <转载> pycharm快捷键及一些常用设置

    1.编辑(Editing ) Ctrl + Space 基本的代码完成(类.方法.属性)Ctrl + Alt + Space 快速导入任意类Ctrl + Shift + Enter 语句完成Ctrl ...

  6. Angular入门(一) 环境配置

    angular/cli 安装 ♦ npm uninstall -g angular-cli /cnpm install -g angular-cli ※采用npm安装失败: Missing write ...

  7. python 搜集参数的共有项和所有项

    搜集共性项和所有项 ###搜集共有参数值 def intersect(*args): res=[] for x in args[0]: for other in args[1:]: if x not ...

  8. Redis之java增删改查

    jedis是java的redis客户端实现,要使用jedis须要加入jedis的maven依赖: <dependency> <groupId>redis.clients< ...

  9. MongoDB入门学习(三):MongoDB的增删查改

            对于我们这样的菜鸟来说,最重要的不是数据库的管理,也不是数据库的性能,更不是数据库的扩展,而是怎么用好这款数据库,也就是一个数据库提供的最核心的功能,增删查改.         由于M ...

  10. long_query_time 设置不生效问题

    由于原来的慢查询日志太大了,有1G多,并且其中包含上一次查询优化前的慢sql,所以想收集最近两天的慢查询语句,故 mysql> show global variables like 'slow% ...