PAT 1004 Counting Leaves 结构体法
#include <stdio.h>
int main()
{
struct node{
int c;int la;int v;int str[100];}cl[105];int n;scanf("%d",&n);if(n==0)return 0;int m;scanf("%d",&m);if(n==1&&m==0){
printf("1");return 0;}int x[105]={
0},i,j;for(i=0;i<100;i++){
cl[i].c=0;cl[i].la=0;cl[i].v=0;}int t,b;for(i=0;i<m;i++){
scanf("%d %d",&t,&b);cl[t].c=b;cl[t].v=1;int q;for(j=0;j<cl[t].c;j++){
scanf("%d",&q);cl[t].str[j]=q;cl[q].v=1;}}for(i=0;i<cl[1].c;i++)cl[cl[1].str[i]].la=1;for(i=1;i<100;i++){
if(cl[i].v==1){
for(j=0;j<cl[i].c;j++){
cl[cl[i].str[j]].la=cl[i].la+1;}}}int max=0,k;for(i=0;i<100;i++){
if(cl[i].c==0&&cl[i].v==1) {
k=cl[i].la;x[k]++;}if(cl[i].la>max) max=cl[i].la;}for(i=0;i<=max;i++){
if(i!=max) printf("%d ",x[i]);if(i==max) printf("%d",x[i]);}}