zoukankan      html  css  js  c++  java
  • pat1004

    1004. Counting Leaves (30)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue
    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 level starting 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
    

    提交代码

    题意就是 给你 2个数。第一个数就是n个点,m就是接下来m行,

    每行出入 a,x 

    a 这个点下面有 x个数 分别为 ......

    输出每一层没有孩子节点的个数。.

    这题用vector去存树==

    不懂 看下这个http://www.cnblogs.com/2014slx/p/7325328.html

    写一个bfs。

    #include<iostream>
    #include<vector>
    #include<cstdio>
    #include<string.h>
    using namespace std;
    int cnt[101];
    vector<int >mp[10000];
    void bfs(int q,int w)
    {
        if(mp[q].empty()==1)
        {
            cnt[w]++;
            return;
        }
        for(vector <int>::iterator it=mp[q].begin();it!=mp[q].end();it++)
        {
           bfs(*it ,w+1);
    
        }
    
    }
    
    
    int main()
    {
        memset(cnt,0,sizeof cnt);
        int n,m;
        scanf("%d%d",&n,&m);
        int x=n-m;
        while(m--)
        {
            int a,b,c;
            scanf("%d%d",&a,&b);
            for(int i=0;i<b;i++)
            {
                scanf("%d",&c);
    
    
                mp[a].push_back(c);
            }
        }
        bfs(1,0);
        printf("%d",cnt[0]);
        int num=cnt[0];
        for(int i=1;num<x;i++)
        {
            printf(" %d",cnt[i]);
            num+=cnt[i];
        }
        printf("
    ");
        return 0;
    }

    最后一部判断就是。n-m就是 下面没有点的值。加起来等于这个就要退出。因为我们不知道层数。

  • 相关阅读:
    Apache 阿帕奇 配置运行环境
    2019年6月多校联训b层——搜索算法 Problem A 宽搜 营救
    西安集训B Day1 test 问题 C: 小明的城堡
    西安集训B层Day1 test 问题 A: 旅行日记
    二分答案—洛谷P1182 数列分段`Section II`
    2019.5.25 Noip模拟测试2 T2题解
    2019.5.25 Noip模拟测试2 T1题解
    DP专题练习 toasting
    2019.5.1 DP专题训练 山峰数(hill)
    React 点击按钮显示div与隐藏div
  • 原文地址:https://www.cnblogs.com/2014slx/p/7796059.html
Copyright © 2011-2022 走看看