PAT_1004: Counting Leaves
2013-07-14 21:01
330 查看
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
备注:宽度优先搜索(breadth-first search), 用队列实现。
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
备注:宽度优先搜索(breadth-first search), 用队列实现。
#include<iostream> #include<queue> using namespace std; #define MAXNODES 101 #define ISLEAF -1 struct node { int k; int c[100]; }; int main() { int n,m,id,k; int i,j; int curlev_size; struct node tree[MAXNODES]; cin>>n>>m; /*只有一个root节点的情况*/ if(n==1) { cout<<1; return 0; } for(i = 0;i < MAXNODES; i++) tree[i].k = ISLEAF; /*初始化*/ for(i = 0; i < m; i++) { cin>>id>>k; tree[id].k = k; for (j = 0; j < k; j++) cin>>tree[id].c[j]; } /*breath-firth search*/ queue<struct node> q; q.push(tree[1]); while(!q.empty()) { curlev_size = q.size(); int count_leaf = 0; while(curlev_size>0) { struct node p = q.front(); q.pop(); if(p.k==ISLEAF) count_leaf++; else { for(i=0;i<p.k;i++) q.push(tree[p.c[i]]); } curlev_size--; } if(q.size()==0) cout<<count_leaf; else cout<<count_leaf<<" "; } return 0; }
相关文章推荐
- PAT (Advanced Level) Practise 1004 Counting Leaves (30)
- PAT:1004. Counting Leaves (30)
- 1004. Counting Leaves (30) PAT甲级
- pat 1004 Counting Leaves (30)
- PAT(Advanced Level) 1004 Counting Leaves 解题报告
- PAT-A | 1004 | Counting Leaves
- PAT:1004. Counting Leaves (30)
- PAT(Advanced Level)1004. Counting Leaves (30) 树的层次遍历
- PAT (甲)1004 Counting Leaves (30)(dfs)
- PAT1004:Counting Leaves
- 1004. Counting Leaves (30)-PAT甲级真题(bfs,dfs,树的遍历,层序遍历)
- Pat(Advanced Level)Practice--1004(Counting Leaves)
- PAT (A )1004. Counting Leaves (30)
- PAT 1004 Counting Leaves
- PAT-Java-1004-Counting Leaves (30)
- 浙大PAT 1004 Counting Leaves (简单深搜)
- 1004. Counting Leaves (30)(PAT甲)
- pat甲级1004. Counting Leaves (30)
- pat 1004 Counting Leaves
- PAT(甲级)1004. Counting Leaves (30)