【PAT】1004. Counting Leaves (30)
来源:互联网 发布:python定义json字符串 编辑:程序博客网 时间:2024/04/30 06:51
1004. 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 Input2 101 1 02Sample Output
0 1
分析:层序遍历
细节:flag标记数量;temp标记节点;temp2标记每层的最后一个节点;
#include <iostream>#include <cstdio>#include <cmath>#include <queue>#include <vector>#include <functional>using namespace std;const int Max=100;int root[Max];struct node{ int info; vector<int> leaves;};class _tree{ public: node tree[Max]; _tree(); int createTree(int N, int M); //return root void insertRoot(int el); void insertLeaves(int el, int x); void Major(int root);};_tree::_tree(){ return;}int _tree::createTree(int N, int M){ for (int i=0;i<M;++i){ int el; cin>>el; insertRoot(el); int k; cin>>k; while (k--){ int x; cin>>x; insertLeaves(el,x); } } for (int i=1;i<=N;++i){ if (root[i]!=1) return i; }}void _tree::insertRoot(int el){ tree[el].info=el;}void _tree::insertLeaves(int el,int x){ root[x]=1; tree[el].leaves.push_back(x);}void _tree::Major(int root){ queue<int> q; q.push(root); int flag=1,flag2=0; int temp=root,temp2; while (!q.empty()){ int v=q.front(); flag2+=tree[v].leaves.size(); if (tree[v].leaves.size()) --flag; for (int i=0;i<tree[v].leaves.size();++i){ q.push(tree[v].leaves[i]); temp2=tree[v].leaves[i]; } q.pop(); if (temp==v){ temp=temp2; cout<<flag; if (!q.empty()) cout<<' '; flag=flag2; flag2=0; } } cout<<endl;}int main(){//freopen("test.txt","r",stdin); _tree t; int n,m; cin>>n>>m; int root=t.createTree(n,m); t.Major(root);return 0;}
- 1004. Counting Leaves (30)-PAT
- pat 1004. Counting Leaves (30)
- 【PAT】1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- pat 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT:1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- [PAT]1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT:1004. Counting Leaves (30)
- PAT:1004. Counting Leaves (30)
- 【PAT】1004. Counting Leaves (30)
- pat 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- 关于mysql中由于类型转换导致索引无法使用的问题
- LeetCode 231. Power of Two
- CodeForces 59AWord 水题
- Spring自动检测并装配bean
- 远程调用的几种方式
- 【PAT】1004. Counting Leaves (30)
- 在linux环境中编译JNI的文件为.so包
- 积分
- CodeForces 59BFortune Telling
- Android嵌套滑动控件的冲突解决和ViewPager适配当前子控件高度不留空白的办法
- FreeRTOS之配置详解、裁剪(FreeRTOSConfig.h)
- 谈谈spring 事务同步 transactionSynchronization 的作用
- 南邮离散数学实验四---图的随机生成及欧拉(回)路的确定
- volatile关键字小结