(Java)1004 Counting Leaves

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

家族层次结构通常由系谱树表示。你的工作是统计那些没有孩子的家庭成员。

Input Specification:

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:

每个输入文件包含一个测试用例。每种情况都以一行开始,该行包含0<N<100(树中的节点数)和M(<N)(非叶节点数)。接下来是M行,每行的格式如下:

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.

其中ID是表示给定非叶节点的两位数字,K是其子节点的数量,后跟其子节点两位数字的ID序列。为了简单起见,让我们将根ID固定为01。

The input ends with N being 0. That case must NOT be processed.

输入以N为0结束。不得处理该案件。

Output Specification:

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.

示例案例表示一个只有2个节点的树,其中01是根,02是它唯一的子节点。因此,在根01级别上,存在0个叶节点;在下一级上,有1个叶节点。然后我们应该在一行中输出0 1。

Sample Input:

2 1
01 1 02

Sample Output:

0 1

思路:首先要理解题目意思!注意是要输出每一层中没有孩子的结点!!

           我们不需要把这棵树给构造出来,只需要用数组存储即可

           本题可以用dfs也可用bfs,相对来说bfs更容易理解更容易想到

DFS

import java.util.*;

class Main{
    static List<List<Integer>> child = new ArrayList<>(101);//存储每个结点的孩子
    static int []num_of_eachlevel = new int[101];//存储每层中无孩子的结点数目
    static int max_level = 0;//记录最大层
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for(int i=0; i < 101; ++i){//初始化
            List<Integer> l = new ArrayList<>();//这里一定要先创建对象,不然后面调用child.get().会报空类型异常
            child.add(l);
        }
        int m = sc.nextInt();
        while(m -- > 0)
        {
            int id = sc.nextInt();
            int k = sc.nextInt();
            while(k-- > 0)
            {
                int j = sc.nextInt();
                child.get(id).add(j);
            }
        }
        dfs(1,1);
        for(int i = 1 ; i <= max_level ;i++)
        {
            if(i > 1) System.out.print(" ");//注意空格
            System.out.print(num_of_eachlevel[i]);
        }
    }

    private static void dfs(int curid, int curlevel)
    {
        if(curlevel > max_level)
        {
            max_level = curlevel;//维护最大层
        }
        if(child.get(curid).size() != 0)//如果几点有孩子
        {
            for(int i = 0 ; i < child.get(curid).size() ;i++)
            {
                dfs(child.get(curid).get(i),curlevel+1);//深度优先搜索
            }
        }
        else//如果无孩子
        {
            num_of_eachlevel[curlevel]++;
        }
    }
}

BFS(转载自别人的)

import java.util.*;

public class Main {
    static List<List<Integer>> d = new ArrayList<>(101); // 相当于可变长二维数组
    static int[] res = new int[101]; // 每一层非叶子节点个数
    static int level = 0; // 当前的层数
    static Deque<Integer> deque = new LinkedList<>();

    public static void bfs(){
        while(!deque.isEmpty()){
            int size = deque.size();
            for (int i = size-1; i >= 0; --i) {
                int cur = deque.pollFirst();
                if(d.get(cur).size() == 0){
                    res[level]++;
                }else {
                    for (int j = 0; j < d.get(cur).size(); j++) {
                        deque.addLast(d.get(cur).get(j));
                    }
                }
            }
            level++;
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if(n == 0){
            return;
        }
        // 相当于给二维数组的第二维做初始化
        for(int i=0; i<101; ++i){
            List<Integer> l = new ArrayList<>();
            d.add(l);
        }
        int m = sc.nextInt();
        for(int i=0; i<m; ++i){
            int x = sc.nextInt();
            int y = sc.nextInt();
            for(int j=0; j<y; ++j){
                int z = sc.nextInt();
                d.get(x).add(z);
            }
        }
        deque.add(1);
        bfs();
        System.out.print(res[0]);
        for (int i = 1; i < level; i++) {
            System.out.print(" " + res[i]);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值