1004. Counting Leaves (30)(PAT甲)
2017-05-23 11:15
405 查看
Counting Leaves (30)
时间限制
400 ms
内存限制
65536 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
之后将用指针写再编辑,还是不习惯vector,一卡一卡的。
时间限制
400 ms
内存限制
65536 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
之后将用指针写再编辑,还是不习惯vector,一卡一卡的。
#include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<vector> #include<functional> using namespace std; vector<int> node[105]; int sum[105]; int main(){ int n,m; scanf("%d %d",&n,&m); int i,j,num,first,v; for(i=1;i<=m;i++) { scanf("%d %d",&first,&num); while(num--) { scanf("%d",&v); node[first].push_back(v); } } int last,e,cur,level=1; queue<int> q; if(node[1].size()) { last=e=1; q.push(1); level++; } else{ sum[level]++; } while(!q.empty())//广度优先遍历 { cur=q.front(); q.pop(); for(j=0;j<node[cur].size();j++) { if(node[node[cur][j]].size()) { q.push(node[cur][j]); last=node[cur][j]; } else{ sum[level]++; } } if(e==cur)//若是叶子节点数量加1 { e=last; level++; } } printf("%d",sum[1]); for(i=2;i<level;i++) { printf(" %d",sum[i]); } printf("\n"); return 0; }
相关文章推荐
- pat甲级1004. Counting Leaves (30)
- PAT:1004. Counting Leaves (30)
- PAT:1004. Counting Leaves (30)
- PAT 1004 Counting Leaves (30) Python
- PAT(甲级)1004. Counting Leaves (30)
- PAT(Advanced Level)1004. Counting Leaves (30) 树的层次遍历
- 1004. Counting Leaves (30)-PAT甲级真题(bfs,dfs,树的遍历,层序遍历)
- (PAT)1004.Counting Leaves (30)
- PAT:1004. Counting Leaves (30)
- PAT (甲)1004 Counting Leaves (30)(dfs)
- PAT (A )1004. Counting Leaves (30)
- 1004. Counting Leaves (30) PAT甲级
- PAT (Advanced Level) Practise 1004 Counting Leaves (30)
- pat 1004 Counting Leaves (30)
- PAT-Java-1004-Counting Leaves (30)
- 关于PAT 1004 Counting leaves 的疑问
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)