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.
2 1<br/>01 1 02
0 1
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;
} }
//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]); } }