zoukankan      html  css  js  c++  java
  • 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
    ", cnt ); //遍历一下 输出
    
        }
    	return 0;
    }
    
  • 相关阅读:
    rand()和srand()
    advanced regression to predict housing prices
    数学建模
    python的对数
    八月总结——人工智能初学者
    看到的不错的项目
    学习笔记(七): Logistic Regression
    学习笔记(六): Regularization for Simplicity
    An Intuitive Explanation of Convolutional Neural Networks
    CentOS7的mysql5.7-rpm.bundle方式安装
  • 原文地址:https://www.cnblogs.com/yspworld/p/4684950.html
Copyright © 2011-2022 走看看