package AdvancedLevelPractice;
import java.util.Scanner;
public class pat1004 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
try {
int N = scanner.nextInt();
int M = scanner.nextInt();
int[][] tree = new int[N+1][N+1];
int[] num = new int[N+1];
int ID, K;
for(int i = 0; i < M; i++){
ID = scanner.nextInt();
K = scanner.nextInt();
num[ID] = K;
for(int j = 0; j < K; j++){
tree[ID][j] = scanner.nextInt();
}
}
int[] q = new int[N+1];
int[] results = new int[N+1];
int[] nodes = new int[N+1];
int front = 0, end = 1, layers = 0, count = 0;
q[front] = 1;
nodes[layers] = 1;
while(front < end){
count = 0;
while (count < nodes[layers]) {
if(num[q[front]] == 0){
results[layers]++;
}
nodes[layers + 1] += num[q[front]];
for(int i = 0; i < num[q[front]]; i++){
q[end++] = tree[q[front]][i];
}
count++;
front++;
}
layers++;
}
for(int i = 0; i < layers-1; i++){
System.out.print(results[i]+" ");
}
System.out.print(results[layers-1]);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
scanner.close();
}
}
pat1004
最新推荐文章于 2022-09-06 15:56:28 发布