PAT甲级 1004.Counting Leaves (30) 题目翻译与答案

题目来源自PAT网站  https://www.patest.cn/

题目描述:

1004. Counting Leaves (30)

Afamily hierarchy is usually presented by a pedigree tree. Your job is to countthose family members who have no child.

Input

Each input filecontains 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-leafnodes. Then M lines follow, each in the format:

ID K ID[1] ID[2]... ID[K]

whereID is a two-digit number representing a given non-leaf node, K is the number ofits children, followed by a sequence of two-digit ID's of its children. For thesake 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 forevery seniority level starting from the root. The numbers must beprinted in a line, separated by a space, and there must be no extra space atthe end of each line.

The sample caserepresents a tree with only 2 nodes, where 01 is the root and 02 is its onlychild. 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.

SampleInput

2 1

01 1 02

SampleOutput

0 1

时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

 

题目翻译:

1004.计算叶子个数

一个家庭的层级结构经常被表现为一个家谱树。你的任务是统计这些家庭成员中谁没有孩子。

 

输入

每个输入文件包含一个测试实例。每个实例开始的一行包含N和M,N指树中的结点个数(0

ID K ID[1] ID[2] ...ID[K]

ID是一个两位数的数字,表示一个非叶结点。K表示其孩子的数量。随后是一个序列,序列中是该结点的孩子结点的两位数ID。为了简单起见,我们把根结点的ID固定为01。

 

输出

对于每个测试实例,你应该计算从根结点开始的每一层中没有孩子的家庭成员的个数。数字必须在一行内输出,用空格分隔,在每行结尾不能有多余的空格。

测试样例表示了一个只有两个结点的树,01是根结点,02是它仅有的孩子。因此在根结点01层级,没有叶节点。再下一层级,有一个叶结点。然后我们应该在一行内输出“0 1”。

 

样例输入

2 1
01 1 02

样例输出

0 1

答案代码:

#include
#include
struct Node
{
	int father;
	int level;
	bool NoChild;
};
Node v[100];
char level[100]="\0";
int main()
{
	int N,M,i,j,c,ID,child,MAXLevel=1;
	scanf("%d%d",&N,&M);
	for(i=0;i<100;++i)
	{
		v[i].father=0;
		v[i].level=0;
		v[i].NoChild=1;
	}
	for(i=0;iMAXLevel)
					MAXLevel=v[j].level;
			}
		}
	
	for(i=1;i<=N;++i)
		if(v[i].NoChild==1)
			level[v[i].level]++;
	for(i=1;i

算法参考了这篇文章

http://www.cnblogs.com/linkstar/p/5674895.html

目测在测试点中,父节点的ID值总比子节点要小。不然33-42行代码就是错的了。

另外,我一开始想的算法是直接构建一颗树(用二叉树形式存储),然后用遍历做。但是只是部分正确,测试点1,3却出现段错误。。。

怀疑是递归次数太多爆栈了,不然是有些测试点的输入是无序的,用输入不能直接构建树。

代码如下:

//部分正确  剩下的段错误 

#include
#include
#include
#define MAXN 205
struct Node
{
	char data;
	struct Node* left;
	struct Node* right;
};
typedef struct Node* Tree;
Tree SearchT(Tree T,char s);
char count[MAXN]='\0';
int CountT(Tree T,int i);

int main()
{
	Tree T;
	T=(Tree)malloc(sizeof(struct Node));
	T->data=1;
	T->left=T->right=NULL;
	Tree Tp,Tpp;
	int N,M,i,c,i1,ID;
	scanf("%d%d\n",&N,&M);
	for(i=0;ileft=Tpp->right=NULL;
			Tpp->data=ID;
			if(i1==0)
			{
				Tp->left=Tpp;
				Tp=Tpp;
			}
			else
			{
				Tp->right=Tpp;
				Tp=Tpp;
			}
		}
	}
	i1=CountT(T,0);
	for(i=0;idata==s)
		return T;
	Tree tp;
	if((tp=SearchT(T->left,s))!=NULL)
		return tp;
	return SearchT(T->right,s);
}
int CountT(Tree T,int i)
{
	int i1=0,i2=0;
	if(T->left==NULL)
		count[i]++;
	else
		i1=CountT(T->left,i+1);
	if(T->right!=NULL)
		i2=CountT(T->right,i);
	if(T->left==NULL && T->right==NULL)
		return i;
	return (i1>i2)?i1:i2;
}


你可能感兴趣的:(PAT甲级)