Input
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.
Output
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.
Sample Input2 1 01 1 02Sample Output
0 1#include<iostream> using namespace std; struct qnode { int id; int level; }qu[110]; int main() { ios::sync_with_stdio(false); int arr[110][110]; int mark[110]; for(int i=1;i<110;++i) { qu[i].id=mark[i]=0; qu[i].level=-1; for(int j=1;j<110;++j) { arr[i][j]=0; } } int n,m; cin>>n>>m; for(int i=1;i<=m;++i) { int id,k; cin>>id>>k; if(mark[id]==0) mark[id]=1; for(int j=1;j<=k;++j) { int tmp; cin>>tmp; arr[id][j]=tmp; if(mark[tmp]==0) mark[tmp]=1; } } if(arr[1][1]==0) { cout<<1; return 0; } else { cout<<0; } qu[1].id=1; qu[1].level=0; mark[1]=2; int ti=1,tj=2; while(1) { bool flag=0; for(int i=1;i<110;++i) { if(mark[i]==1) {flag=1;break;} } if(!flag) break; int tmp=qu[ti].id; ++ti; if(arr[tmp][1]==0) continue; for(int i=1;i<110&&arr[tmp][i]!=0;++i) { qu[tj].id=arr[tmp][i]; qu[tj].level=qu[tmp].level+1; if(mark[qu[tj].id]==1) mark[qu[tj].id]=2; ++tj; } } int ii=2; int le=1; while(qu[ii].level==le&&qu[ii].id!=0) { int ans=0; while(qu[ii].level==le&&qu[ii].id!=0) { if(arr[qu[ii].id][1]==0) ans++; ++ii; } cout<<' '<<ans; ++le; } }