首页 > 试题广场 >

Counting Leaves (30)

[编程题]Counting Leaves (30)
  • 热度指数:3013 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

输入描述:
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.


输出描述:
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.
示例1

输入

2 1<br/>01 1 02

输出

0 1


不知道为什么在PAT里测试点4总是过不去,牛客全都通过了,哪位大佬能够帮忙解答一下??
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

private static int N;
private static int[][] FTree;
private static int[] numLevel;
private static int maxLevel;

public static void main(String[] args) throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    String str;
    String[] strs;

    int M;
    int vertex;

    while((str=br.readLine())!=null) {

        strs = str.split(" ");

        N = Integer.valueOf(strs[0]);
        M = Integer.valueOf(strs[1]);

        FTree = new int[100][N+1];

        for(int i=0; i<M; i++) {
            strs = br.readLine().split(" ");

            vertex = Integer.valueOf(strs[0]);
            for(int j=1; j<strs.length; j++) {
                FTree[vertex][j-1] = Integer.valueOf(strs[j]);
            }
        }

        numLevel = new int[M + 1];
        maxLevel = 0;

        preorder(1, 0);

        if(N == 0) {
            System.out.println(1);
            continue;
        }

        for(int i=0; i<maxLevel; i++) {
            System.out.print(numLevel[i] + " ");
        }
        System.out.println(numLevel[maxLevel]);
    }

}

private static void preorder(int root, int level) {
    if(FTree[root][0]==0) {
        numLevel[level]++;
        if(maxLevel < level) {
            maxLevel = level;
        }
    } else {
        int K = FTree[root][0];
        for(int k=1; k<=K; k++) {
            preorder(nextNode(root, k), level+1);
        }
    }
}

private static int nextNode(int root, int k) {
    if(k<=FTree[root][0]) {
        return FTree[root][k];
    }
    return 0;
}

}

发表于 2018-09-27 10:53:56 回复(5)
//BFS import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;


public class test1004 {
	public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {//注意while处理多个case
        		String s=in.nextLine();
        		String[] r=s.split(" ");
        		int a=Integer.parseInt(r[0]);
        		int b=Integer.parseInt(r[1]);
        		int index=0;
        		if(b==0) {
        			System.out.println(1);
        			continue;
        		}
        		ArrayList<Integer>[] arr=new ArrayList[a+1];
        		for (int i = 0; i < arr.length; i++) {
					arr[i]=new ArrayList<Integer>();
				}
            while(index++<b){
            	String temp=in.nextLine();
            	String[] s1=temp.split(" ");
            	int index1=Integer.parseInt(s1[0]);
            	for (int i = 2; i < s1.length; i++) {
					arr[index1].add(Integer.parseInt(s1[i]));
				}
            }
            do_1(a,b,arr);
        }
	
	}

	private static void do_1(int a, int b, ArrayList<Integer>[] arr) {
		Queue<Integer> queue=new LinkedList<Integer>();
		queue.add(1);
		int depth=0;
		int[] res=new int[a];
		while(!queue.isEmpty()){
			int size=queue.size();
			for (int i = 0; i < size; i++) {
				int temp=queue.remove();
				if(arr[temp].size()==0) res[depth]++;
				for (int j = 0; j <arr[temp].size(); j++) {
					queue.add(arr[temp].get(j));
				}
			}
			depth++;
		}
		for (int i = 0; i < depth-1; i++) {
			System.out.print(res[i]+" ");
		}
		System.out.print(res[depth-1]);
	}
} 


编辑于 2016-08-03 17:45:43 回复(0)

问题信息

难度:
2条回答 8583浏览

热门推荐

通过挑战的用户

Counting Leaves (30)