http://pat.zju.edu.cn/contests/pat-practise/1004
来源:互联网 发布:java.io jar包 编辑:程序博客网 时间:2024/05/01 02:03
1004. Counting Leaves (30)
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 101 1 02Sample Output
0 1
- #include<iostream>
- #include<cstdio>
- #include<memory.h>
- #include<algorithm>
- #include<cstring>
- #include<queue>
- #include<cmath>
- #include<cstdlib>
- using namespace std;
- #define MAX 0x6FFFFFFF
- int isleaf[100];
- int level[100];
- int parent[100];
- int n;
- int maxv;
- int calclevel(int i){
- if(level[i]==0 && parent[i]!=0)
- level[i] = calclevel(parent[i]) + 1;
- if(level[i]>maxv)
- maxv = level[i];
- return level[i];
- }
- int main(){
- // freopen("in.txt", "r", stdin);
- int m;
- int id, k;
- int t;
- while(cin>>n>>m){
- memset(isleaf, 0, sizeof(isleaf));
- memset(level, 0, sizeof(level));
- memset(parent, 0, sizeof(parent));
- level[1] = 1;
- parent[1] = 1;
- maxv = 1;
- for(int i=0;i<m;++i){
- scanf("%d%d", &id, &k);
- if(k!=0)
- isleaf[id] = 1;
- for(int j=0;j<k;++j){
- scanf("%d", &t);
- parent[t] = id;
- }
- }
- for(int i=2;i<=n;++i)
- calclevel(i);
- bool first = true;
- for(int l=1;l<=maxv;++l){
- int cnt = 0;
- for(int i=1;i<=n;++i){
- if(level[i]==l && !isleaf[i])
- cnt++;
- }
- if(first){
- first = false;
- }else{
- printf(" ");
- }
- printf("%d", cnt);
- }
- printf("\n");
- }
- // fclose(stdin);
- return 0;
- }
- http://pat.zju.edu.cn/contests/pat-practise/1004
- http://pat.zju.edu.cn/contests/pat-practise/1030
- http://pat.zju.edu.cn/contests/pat-practise/1034
- http://pat.zju.edu.cn/contests/pat-practise/1016
- http://pat.zju.edu.cn/contests/pat-practise/1032
- http://pat.zju.edu.cn/contests/pat-practise/1037
- http://pat.zju.edu.cn/contests/pat-practise/1021
- http://pat.zju.edu.cn/contests/pat-practise/1040
- http://pat.zju.edu.cn/contests/pat-practise/1038
- http://pat.zju.edu.cn/contests/pat-practise/1025
- http://pat.zju.edu.cn/contests/pat-practise/1023
- http://pat.zju.edu.cn/contests/pat-practise/1029
- http://pat.zju.edu.cn/contests/pat-practise/1035
- http://pat.zju.edu.cn/contests/pat-practise/1024
- http://pat.zju.edu.cn/contests/pat-practise/1031
- http://pat.zju.edu.cn/contests/pat-practise/1028
- http://pat.zju.edu.cn/contests/pat-practise/1020
- http://pat.zju.edu.cn/contests/pat-practise/1015
- http://pat.zju.edu.cn/contests/pat-practise/1009
- http://pat.zju.edu.cn/contests/pat-practise/1007
- http://pat.zju.edu.cn/contests/pat-practise/1005
- 介绍Servlet容器
- Emacs org进阶
- http://pat.zju.edu.cn/contests/pat-practise/1004
- 数据结构——KMP算法的实现
- 28个Unix/Linux的命令行神器
- http://pat.zju.edu.cn/contests/pat-practise/1003
- pragma comment的使用
- setAttribute和setParameter方法的区别
- http://pat.zju.edu.cn/contests/pat-practise/1001
- 纽约时报称莫言获诺贝尔奖打破欧洲作家垄断-莫言-诺贝尔文学奖-村上春树
- python的json操作