PAT1004
来源:互联网 发布:国际顶级域名干嘛的 编辑:程序博客网 时间:2024/06/06 16:27
该题其实算是题目中的简单题,但是没想到竟然有三十分,可能是需要建立比较特别的树结构吧,所以分值相对来说比较高,其实如果看过《算法导论》的朋友应该都有印象,这里的树结构其实就是《算法导论》第三版中137页里提到的分支无限有根树,这里只需要使用“左孩子右兄弟”的方法就可以轻松表示出题目中提到的pedigree tree。题目和源代码结构如下:
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
本人的参考代码:
#include "iostream"using namespace std;struct LRTree{//int ID;int layer;int PtrOfLeftChild;int PtrOfBrother ;//LRTree *PtrOfLeftChild;//LRTree *PtrOfBrother;LRTree(){layer = 0;PtrOfBrother = 0;PtrOfLeftChild = 0;};LRTree(int L,int PB, int PC){layer = L;PtrOfBrother = PB;PtrOfLeftChild = PC;};};int NoChildMemberCount[102] = {0};int MaxLayerNum = 0;void MemberInput(LRTree PedigreeTree[]){int ID;int K;int i;cin>>ID>>K;cin>>PedigreeTree[ID].PtrOfLeftChild;ID = PedigreeTree[ID].PtrOfLeftChild;for(i = 1;i<K;i++){cin>>PedigreeTree[ID].PtrOfBrother;ID = PedigreeTree[ID].PtrOfBrother;}}void updateLayers(LRTree PedigreeTree[],const int ID, const int CurrentLayer){int i = ID;if(MaxLayerNum < CurrentLayer)MaxLayerNum = CurrentLayer;do{PedigreeTree[i].layer = CurrentLayer;if(PedigreeTree[i].PtrOfLeftChild != 0)updateLayers(PedigreeTree,PedigreeTree[i].PtrOfLeftChild, CurrentLayer + 1); // deep firstelseNoChildMemberCount[CurrentLayer]++;i = PedigreeTree[i].PtrOfBrother;}while(i != 0);}void printLayerNum(){int i = 1;while(i < (MaxLayerNum)){cout<<NoChildMemberCount[i]<<" ";i++;}cout<<NoChildMemberCount[i]<<endl;}int main(){int N,M;LRTree PedigreeTreeInit(0,0,0);LRTree PedigreeTree[101];int i;//while(1){for(i = 0;i < 101;i++){PedigreeTree[i] = PedigreeTreeInit;NoChildMemberCount[i] = 0;}NoChildMemberCount[i] = 0;MaxLayerNum = 0;cin>>N>>M;if(N == 1){cout<<1<<endl;}else{for(i = 0;i < M;i++){MemberInput(PedigreeTree);}updateLayers(PedigreeTree,01,1);printLayerNum();}}return 0;}
- PAT1004
- pat1004
- PAT1004
- pat1004
- pat1004
- PAT1004
- pat1004
- PAT1004
- PAT1004 成绩排名
- PAT1004. 成绩排名
- pat1004:成绩排名
- PAT1004. 成绩排名
- 【PAT1004】Counting Leaves
- PAT1004. Counting Leaves (30)
- pat1004 Counting Leaves
- [搜索]PAT1004 Counting Leaves
- PAT1004.BASIC 成绩排名 (20)
- PAT1004. Counting Leaves
- 我那些逝去的青春
- 最长有序子序列 Longest Ordered Subsequence POJ2533
- 用于操纵字符串的的String类
- trait特性萃取机
- 【最大流(EK)】hdu 3549 Flow Problem
- PAT1004
- Fragment使用
- 新年新的安排
- 项目总结—jQuery EasyUI-DataGrid动态加载表头
- 遍历Map和遍历List方法
- Window Server 2008 R2 +IIS7.5 部署WCF Rest问题
- queue
- 线段树。。。POJ 2777
- C#中给LABEL控件设置BACKGROUNDIMAGE属性