zoukankan      html  css  js  c++  java
  • 1004 Counting Leaves (30)

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

    Input

    Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), the number of non-leaf nodes. Then M lines follow, each in the format:

    ID K ID[1] ID[2] ... ID[K]
    

    where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 01.

    Output

    For each test case, you are supposed to count those family members who have no child for every seniority levelstarting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.

    The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output "0 1" in a line.

    Sample Input

    2 1
    01 1 02
    

    Sample Output

    0 1
    
     
    //DFS算法 
    #include<cstdio>
    #include<vector>
    using namespace std;
    const int maxn = 110;
    vector<int> node[maxn];
    int hashtable[maxn] = {0}; 
    int maxlevel = 0;
    void DFS(int index,int level){
        if(node[index].size() == 0){
            hashtable[level]++;
            if(level > maxlevel) maxlevel = level;
            return;
        }
        for(int i = 0; i < node[index].size(); i++){
            DFS(node[index][i],level+1);
        }
    }
    
    int main(){
       int n,m,father,child,k;
       scanf("%d%d",&n,&m);
       for(int i = 0; i < m; i++){
           scanf("%d%d",&father,&k);
           for(int j = 0; j < k; j++){
               scanf("%d",&child);
               node[father].push_back(child);
           }
       }
       DFS(1,0);
       for(int i = 0; i <= maxlevel; i++){
           if(i == 0)  printf("%d",hashtable[i]);
           else printf(" %d",hashtable[i]);
           }
           return 0;
    } 
    #include<cstdio>
    #include<queue>
    #include<vector>
    #include<algorithm>
    using namespace std;
    const int maxn = 110;
    
    vector<int> G[maxn];
    int h[maxn] = {0}; //树高度
    int leaf[maxn] = {0}; //每层节点统计
    int max_h = 0;
    void BFS(){
        queue<int> q;
        q.push(1);
        while(!q.empty()){
            int id = q.front();
            q.pop();
            max_h = max(max_h,h[id]);
            if(G[id].size() == 0){
                leaf[h[id]]++;
            }
            for(int i = 0; i < G[id].size(); i++){
                h[G[id][i]] = h[id] + 1;
                q.push(G[id][i]);
            }
        }
    }
    
    int main(){
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i = 0; i < m; i++){
            int father,child,k;
            scanf("%d%d",&father,&k);
            for(int j = 0; j < k; j++){
                scanf("%d",&child);
                G[father].push_back(child);
            }
        }
        h[1] = 1;
        BFS();
        for(int i = 1; i <= max_h; i++){
            if(i == 1)printf("%d",leaf[i]);
            else printf(" %d",leaf[i]);
        }
    
        return 0;
    } 
  • 相关阅读:
    光线步进——RayMarching入门
    MATLAB GUI制作快速入门
    Python中用绘图库绘制一条蟒蛇
    node 常见的一些系统问题
    webpack 入门指南
    利用 gulp 来合并seajs 的项目
    移动端 解决自适应 和 多种dpr (device pixel ratio) 的 [淘宝] 解决方案 lib-flexible
    富有哲理的文章
    NodeJS 难点(网络,文件)的 核心 stream 四: writable
    Vue.js 源码学习笔记 -- 分析前准备2 -- Object.defineProperty
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/9295457.html
Copyright © 2011-2022 走看看