2015 多校训练第3场 1011 【树形结构问题】

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

这是这场比赛里面最简单的一道题,个人觉的这道题不错,就把它拿来写博客了。

这个问题模拟公司里的职位关系,一个职工它在公司里可能会有下属,也可能会有上司。现在给你n个人,输入n-1有向边,请问拥有下属的个数等于k的有多少人。

分析:很明显这样的关系组出来就是一棵树,每个员工都只会有一个上司,换句话说就是,每一个人都只会有一个父亲节点,除了根节点的父亲是他自己外。

根据n-1条有向边建立节点间的父子关系。每次在新添加一条父子关系事,将当前父亲节点往上回溯到根节点,这些节点都是这个儿子节点的“父亲”或称祖先。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <math.h>
#define eps 1e-8
#include <algorithm>

using namespace std;

int son[200];
int fa[200];

int main()
{
    int n, k;
    int i, j;
    int u, v;

    while(scanf("%d %d", &n, &k)!=EOF){
        memset(son, 0, sizeof(son));//出事哈每个人都有0个儿子
        for(i=1; i<=n; i++){
            fa[i]=i; //每个人的父亲是它自己
        }

        for(i=1; i<n; i++){
            scanf("%d %d", &u, &v);
            fa[v]=u; //将v的父亲确定为u
            son[u]++; //u的儿子数量+1
            while( fa[u]!=u ){//如果u的父亲不是他自己 说明u不是根节点
                u=fa[u];  //往上面回溯 直到根节点跳出
                son[u]++; //经过的这些节点的儿子数全部+1
            }
        }
        int cnt=0;
        for(i=1; i<=n; i++)
            if(son[i]==k)
                cnt++;
        printf("%d\n", cnt ); //遍历一下 输出

    }
	return 0;
}
时间: 2024-11-05 18:49:58

2015 多校训练第3场 1011 【树形结构问题】的相关文章

hdu5379||2015多校联合第7场1011 树形统计

http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little sun is an artist. Today he is playing mahjong alone. He suddenly feels that the tree in the yard doesn't look good. So he wants to decorate the tree.(The tree has n vertexs, i

2015 多校赛 第七场 1011 (hdu 5379)

题意:给定一棵树,树上有 n 个节点.问有多少种方案,使得在每个节点上依次放置数 1~n 后,每个节点的儿子节点上的数连续(比如 1 为根,有1-2,1-3,1-4,则令2,3,4上的数连续),每个子树上的数连续. 注释在代码里. #pragma comment(linker, "/STACK:102400000,102400000") #include<iostream> #include<cstdio> #include<algorithm> #

hdu 4970 Killing Monsters(简单题) 2014多校训练第9场

Killing Monsters                                                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Kingdom Rush is a popular TD game, in which you should b

hdu 4923 Room and Moor(数学题)2014多校训练第6场

Room and Moor                                                                          Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of

hdu 4902 Nice boat(2014多校训练第4场 1006)

Nice boat                                                                           Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description There is an old country and the king fell in love with a d

hdu 4960 Another OCD Patient(dp)2014多校训练第9场

Another OCD Patient                                                                         Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) pat

hdu5353||2015多校联合第六场1001 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=5353 Problem Description There are n soda sitting around a round table. soda are numbered from 1 to n and i-th soda is adjacent to (i+1)-th soda, 1-st soda is adjacent to n-th soda. Each soda has some candies

hdu 4965 Fast Matrix Calculation(矩阵快速幂)2014多校训练第9场

Fast Matrix Calculation                                                                   Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description One day, Alice and Bob felt bored again, Bob knows Ali

hdu 4920 Matrix multiplication(矩阵相乘)多校训练第5场

Matrix multiplication                                                                           Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Given two matrices A and B of size n×n, find the