http://pat.zju.edu.cn/contests/pat-practise/1004

 

1004. Counting Leaves (30)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

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 Input
2 1
01 1 02
Sample Output
0 1

[cpp]  view plain copy
  1. #include  
  2. #include  
  3. #include  
  4. #include  
  5. #include  
  6. #include  
  7. #include  
  8. #include  
  9. using namespace std;  
  10. #define MAX 0x6FFFFFFF  
  11. int isleaf[100];  
  12. int level[100];  
  13. int parent[100];  
  14. int n;  
  15. int maxv;  
  16. int calclevel(int i){  
  17.       
  18.     if(level[i]==0 && parent[i]!=0)  
  19.         level[i] = calclevel(parent[i]) + 1;  
  20.     if(level[i]>maxv)  
  21.         maxv = level[i];  
  22.     return level[i];  
  23. }  
  24.   
  25. int main(){  
  26.   
  27.    // freopen("in.txt", "r", stdin);  
  28.     int m;  
  29.     int id, k;  
  30.     int t;  
  31.     while(cin>>n>>m){  
  32.         memset(isleaf, 0, sizeof(isleaf));  
  33.         memset(level, 0, sizeof(level));  
  34.         memset(parent, 0, sizeof(parent));  
  35.         level[1] = 1;  
  36.         parent[1] = 1;  
  37.         maxv = 1;  
  38.           
  39.         for(int i=0;i
  40.             scanf("%d%d", &id, &k);  
  41.             if(k!=0)  
  42.                 isleaf[id] = 1;  
  43.             for(int j=0;j
  44.                 scanf("%d", &t);  
  45.                 parent[t] = id;  
  46.             }  
  47.         }  
  48.           
  49.         for(int i=2;i<=n;++i)  
  50.             calclevel(i);  
  51.           
  52.         bool first = true;  
  53.         for(int l=1;l<=maxv;++l){  
  54.             int cnt = 0;  
  55.             for(int i=1;i<=n;++i){  
  56.                 if(level[i]==l && !isleaf[i])  
  57.                     cnt++;  
  58.             }  
  59.             if(first){  
  60.                 first = false;  
  61.             }else{  
  62.                 printf(" ");  
  63.             }  
  64.             printf("%d", cnt);  
  65.         }  
  66.         printf("\n");  
  67.   
  68.     }  
  69.   
  70.     
  71.    // fclose(stdin);  
  72.     return 0;  
  73. }  

你可能感兴趣的:(PAT)