1004. Counting Leaves (30)

原创 2015年11月21日 19:12:05
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
------------------------------华丽的分割线----------------------------
分析:
可以用图来求解这一题,先用邻接矩阵把输入存下来,graph[i][j]=1(i!=j)代表j是i的子节点,graph[i][i]代表i节点处于第几层。
代码:
#include<cstdio>
#include<queue>
using namespace std;

#define Maxn 101

int graph[Maxn][Maxn];
int NumOfNoLeaf[Maxn];
int Level=1;
bool HaveNoChild=true;
queue<int> findnoleaf;

int main(void)
{
	int N,M,K;
	int i,j;
	scanf("%d %d",&N,&M);
	for(i=0;i<M;++i)
	{
		int parent,child;
		scanf("%2d %d",&parent,&K);
		for(j=0;j<K;++j)
		{
			scanf("%2d",&child);
			graph[parent][child] = 1;
		}
	}

	int startnode=1;
	graph[startnode][startnode] = 1;
	int thisnode=0;

	findnoleaf.push(startnode);
	while(!findnoleaf.empty()){
		thisnode = findnoleaf.front();
		findnoleaf.pop();
		for(i=1;i<=N;++i)
		{
			if(graph[thisnode][i] != 0 && thisnode != i)
			{
				graph[i][i] = graph[thisnode][thisnode]+1;
				findnoleaf.push(i);
				if(graph[i][i] > Level)
					Level = graph[i][i];
				HaveNoChild = false;
			}
		}
		if(HaveNoChild)
		{
			++NumOfNoLeaf[graph[thisnode][thisnode]];
		}
		else
		{
			HaveNoChild = true;
		}
	}

	for(i=1;i<=Level;++i)
	{
		printf("%d",NumOfNoLeaf[i]);
		if(i!=Level)
			printf(" ");
	}
	return 0;
}


版权声明:本文为jungleni原创文章,转载请注明出处。

相关文章推荐

1004. Counting Leaves (30)

  • 2017年01月04日 15:57
  • 2KB
  • 下载

1004. Counting Leaves (30)-DFS

考查知识点:树的遍历思路:这个题重点在于要体会到用邻接表来存储树,刚开始构建树的时候考虑要怎么一层套一层啊,显然是对邻接表的理解不深刻,构建完树后遍历此树找到叶子节点即可,其中遍历方式可分为dfs和b...

PAT 1004. Counting Leaves (30)

1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 ...

1004. Counting Leaves (30)-PAT甲级真题(bfs,dfs,树的遍历,层序遍历)

1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
  • liuchuo
  • liuchuo
  • 2016年08月15日 21:18
  • 220

【C++】1004. Counting Leaves (30)

1004. Counting Leaves (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者...

PAT (Advanced Level) 1004. Counting Leaves (30) 解题报告

1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 ...

pat 甲级 1004. Counting Leaves (30)

1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 ...

PAT_A 1004. Counting Leaves (30)

1004. Counting Leaves (30) 树
  • scylhy
  • scylhy
  • 2016年11月20日 15:03
  • 203

pat 1004 Counting Leaves (30)

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member...

1004. Counting Leaves (30)

PAT题目: 1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree...
  • ysc6688
  • ysc6688
  • 2014年09月28日 20:19
  • 361
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:1004. Counting Leaves (30)
举报原因:
原因补充:

(最多只允许输入30个字)