首页 > 其他 > 详细

HDU-5326 Work

时间:2015-08-05 17:55:05      阅读:201      评论:0      收藏:0      [点我收藏+]

http://acm.hdu.edu.cn/showproblem.php?pid=5326

Work

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 847 Accepted Submission(s): 534


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
 

 

Author
ZSTU
 题目大意:给一个树,点之间是雇佣关系 A B表示A是B上司,问有几个人有k个下属

 

Source
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int dp[205],n,m,len,vis[205];
int head[205];
struct node
{
    int now,next;
}tree[205];
void add(int x,int y)
{
    tree[len].now=y;
    tree[len].next=head[x];
    head[x]=len++;
}
void dfs(int root)
{

    int i,j,son;
    for(i=head[root];i!=-1;i=tree[i].next)
    {
            son=tree[i].now;
            if(vis[son]==0)
            {
           //  printf("son===%d",son);
                vis[son]=1;
            // printf("root:%d=%d,son:%d=%d\n",root,dp[root],son,dp[son]);
             dp[root]+=dp[son];
              dfs(son);
              dp[root]+=dp[son]+1;
             vis[son]=0;

            }

     }
}

int main()
{
    int i,root,x,y,father[200],brother[200],son[200];
    bool in[105];
   while(~scanf("%d%d",&n,&m))
   {
       len=1;//len=0,是根节点,一定要注意。
       memset(dp,0,sizeof(dp));
         memset(dp,0,sizeof(dp));
       memset(head,-1,sizeof(head));
       memset(in, false, sizeof(in));
       for(i=1;i<n;i++)
       {
           scanf("%d%d",&x,&y);
        add(x,y);
           in[y]++;

       }
        int root;
        for(int i = 1; i <= n; i++)
            {
                if(in[i] ==0)
                    root=i;
            }

         dfs(root);
      int  s=0;
     for(i=1;i<=n;i++)
       {
           //printf("dp[]=%d\n",dp[i]);
           if(dp[i]==m)
              s++;
       }
       printf("%d\n",s);
   }
   return 0;
}

 

 

HDU-5326 Work

原文:http://www.cnblogs.com/cancangood/p/4705240.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!