PAT(甲级)1004. Counting Leaves (30)
2018-01-18 09:48
609 查看
Counting Leaves (30)
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
题目大意:族谱用树来描述,求每一代没有子女的个数,即每一层的叶子节点数目,默认根节点为1,共n个节点从1到N ,m个非叶子节点,然后m行,当前非叶子节点 孩子个数 孩子id
分析:开始没考虑到 当前非叶子节点未出现过 则其孩子的层数不能确定 所以不能一边输入数据一边确定层数 后来利用了并查集中 一种find函数的写法进行拓展 求得所有节点的层数 用vis标记节点是否有孩子 ,相结合可以统计出每层叶子节点个数
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
题目大意:族谱用树来描述,求每一代没有子女的个数,即每一层的叶子节点数目,默认根节点为1,共n个节点从1到N ,m个非叶子节点,然后m行,当前非叶子节点 孩子个数 孩子id
分析:开始没考虑到 当前非叶子节点未出现过 则其孩子的层数不能确定 所以不能一边输入数据一边确定层数 后来利用了并查集中 一种find函数的写法进行拓展 求得所有节点的层数 用vis标记节点是否有孩子 ,相结合可以统计出每层叶子节点个数
/*附上一组数据 10 5 01 3 02 03 04 02 1 05 06 2 08 09 03 2 06 07 08 1 10 */ #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 110 using namespace std; int high=0; int pre[maxn],rec[maxn]; void pre_find(int x){//求得x的层数并对所经过的点赋层数 int r=x; int cnt=1; while(pre[r]!=r){//计算层数 r=pre[r]; cnt++; } high=max(high,cnt); while(pre[x]!=x){//沿路赋层数 rec[x]=cnt; x=pre[x]; cnt--; } return ; } int main() { int n,m,val,num,to; bool vis[maxn]; memset(rec,0,sizeof(rec)); memset(vis,false,sizeof(vis)); scanf("%d%d",&n,&m); rec[1]=1;//根节点层数为1 for(int i=1;i<=n;++i)//初始化 pre[i]=i; for(int i=0;i<m;++i){ scanf("%d%d",&val,&num); vis[val]=true; while(num--){ scanf("%d",&to); pre[to]=val; } } for(int i=1;i<=n;++i){//遍历计算所有叶子节点 if(!vis[i]) pre_find(i); } // for(int i=1;i<=n;++i) // printf("%d ",rec[i]); int i=1,j=1; while(i<=high){//计算每层叶子节点数目 int sum=0; for(;j<=n;++j){ if(rec[j]>i) break; if(!vis[j]&&rec[j]==i) sum++; } if(i==1) printf("%d",sum); else printf(" %d",sum); i++; } return 0; }
相关文章推荐
- 1004. Counting Leaves (30)-PAT甲级真题(bfs,dfs,树的遍历,层序遍历)
- pat甲级1004. Counting Leaves (30)
- 1004. Counting Leaves (30) PAT甲级
- PAT (A )1004. Counting Leaves (30)
- (PAT)1004.Counting Leaves (30)
- PAT (甲)1004 Counting Leaves (30)(dfs)
- PAT:1004. Counting Leaves (30)
- 浙大Pat | 浙大pat 牛客网甲级 1004 Acute Stroke (30) BFS
- PAT:1004. Counting Leaves (30)
- PAT:1004. Counting Leaves (30)
- PAT (Advanced Level) Practise 1004 Counting Leaves (30)
- 【Pat】甲级1004 - Counting Leaves(树 & bfs)
- PAT-Java-1004-Counting Leaves (30)
- PAT 1004 Counting Leaves (30) Python
- PAT(Advanced Level)1004. Counting Leaves (30) 树的层次遍历
- 1004. Counting Leaves (30)(PAT甲)
- pat 1004 Counting Leaves (30)
- PAT甲级 1135.Is It A Red-Black Tree (30)
- 浙大pat | 浙大pat 牛客网甲级 1001Public Bike Management (30) 迪杰斯特拉改进算法
- 1004. Counting Leaves (30)