PAT (Advanced Level) Practise 1004. Counting Leaves (30)
2014-10-22 16:55
337 查看
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
提交代码
#include <iostream> using namespace std; int main() { int n,m,i,j,k,temp,t=0,w=0,mm=0,flag=0; int fa,chi; int a[110][110]={0}; int level[110]={0}; int child[110]={0}; int array[110]; scanf("%d %d",&n,&m); for(i=0;i<m;i++) { scanf("%d %d",&fa,&k); for(j=0;j<k;j++) { scanf("%d",&chi); a[fa][chi]=1; } } array[0]=1; level[1]=1; while(t<=w) { for(i=0;i<100;i++) { if(a[array[t]][i]==1) { child[array[t]]++; level[i]=level[array[t]]+1; if(level[i]>mm) mm=level[i]; w++; array[w]=i; } } t++; } for(i=1;i<=mm;i++) { temp=0; for(j=0;j<100;j++) if((level[j]==i)&&(child[j]==0)) temp++; if(flag==0) { printf("%d",temp); flag=1; } else printf(" %d",temp); } if(n==1) printf("1"); system("pause"); return 0; }
相关文章推荐
- PAT-PAT (Advanced Level) Practise 1018. Public Bike Management (30) 【四星级】
- PAT (Advanced Level) Practise 1131 Subway Map (30)
- PAT (Advanced Level) Practise 1018 Public Bike Management (30)
- PAT (Advanced Level) Practise 1099 Build A Binary Search Tree (30)
- 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1045 Favorite Color Stripe (30)
- 1004. Counting Leaves (30)——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1030. Travel Plan (30)
- 1049. Counting Ones (30)【计算1 的个数】——PAT (Advanced Level) Practise
- 1053. Path of Equal Weight (30)【树+搜索】——PAT (Advanced Level) Practise
- 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1111 Online Map (30)
- PAT (Advanced Level) Practise 1131 Subway Map (30)
- PAT (Advanced Level) Practise 1049 Counting Ones (30)
- 1004. Counting Leaves (30)——PAT (Advanced Level) Practise
- 1018. Public Bike Management (30) @ PAT (Advanced Level) Practise
- 1034. Head of a Gang (30)【离散化+并查集+搜索】——PAT (Advanced Level) Practise
- 1099. Build A Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1014. Waiting in Line (30)
- 1022. Digital Library (30)——PAT (Advanced Level) Practise