PAT甲级真题及训练集(24)--1004. Counting Leaves (30)
2017-07-09 16:44
459 查看
1004. Counting Leaves (30)
时间限制400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
提交代码
/**
作者:一叶扁舟
时间:21:36 2017/7/8
思路:统计树每一层结点的个数
*/
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <math.h>
#include <string.h>
#include <vector>
using namespace std;
#define SIZE 100001
typedef struct TreeNode{
int data;
vector<int> child;
}TreeNode;
int level[SIZE] = { 0 };//定义每一层统计树节点数
//定义的树,可以用结构体,也可以用直接用vector<int> tree来定义
TreeNode treeNode[SIZE];
int IsRoot[SIZE] = { 0 };//0默认是根节点,1是子结点
//树的最大深度
int maxLevel = 0;
//得到每一层结点个数
void getLevelNodeNum(int rootNum, int depth){
//到了叶子节点
if (treeNode[rootNum].child.size() == 0){
level[depth]++;
if (depth > maxLevel){
maxLevel = depth;
}
return;
}
for (unsigned int i = 0; i < treeNode[rootNum].child.size(); i++){
//递归访问root的子结点
getLevelNodeNum(treeNode[rootNum].child[i], depth + 1);
}
}
int getTreeRoot(int N){
for (int i = 1; i <= N; i++){
if (IsRoot[i] != 1){
return i;
}
}
return 0;
}
int main(){
int N;
int m;//有孩子结点的个数
scanf("%d %d", &N, &m);
for (int i = 0; i < m; i++){
int num, nodeNo;//nodeNo父节点,num为父节点nodeNo后面的子结点的个数
scanf("%d %d", &nodeNo, &num);
for (int j = 0; j < num; j++){
int temp;
scanf("%d", &temp);
IsRoot[temp] = 1;//子结点再这里出现了,说明不可能是根节点
//存入对应的孩子结点
treeNode[nodeNo].child.push_back(temp);
}
}
//得到根结点
int rootNum = getTreeRoot(N);
getLevelNodeNum(rootNum, 1);
double result = 0;
//输出每一层的结点个数
for (int i = 1; i <= maxLevel; i++){
//找到depth大于0的
printf("%d",level[i]);
if (i != maxLevel){
printf(" ");
}
}
system("pause");
return 0;
}
相关文章推荐
- 1068. Find More Coins (30)-PAT甲级真题(01背包)
- 1087. All Roads Lead to Rome (30)-PAT甲级真题-Dijkstra + DFS
- PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)
- 1095. Cars on Campus (30)-PAT甲级真题(map,排序)
- PAT甲级真题及训练集(14)--1058. A+B in Hogwarts (20)
- PAT甲级真题及训练集(18)--1097. Deduplication on a Linked List (25)
- PAT甲级真题及训练集(23)--1079. Total Sales of Supply Chain (25)(未全部测试通过,pat24分)
- 1045. Favorite Color Stripe (30)-PAT甲级真题
- 1103. Integer Factorization (30)-PAT甲级真题(dfs深度优先)
- PAT甲级真题及训练集(25)--1094. The Largest Generation (25)
- 1034. Head of a Gang (30)-PAT甲级真题(图的遍历dfs)
- PAT甲级真题及训练集(19)--1020. Tree Traversals (25)(后序,中序建树,很重要)
- 1091. Acute Stroke (30)-PAT甲级真题(广度优先搜索)
- 1080. Graduate Admission (30)-PAT甲级真题
- 1030. Travel Plan (30)-PAT甲级真题
- PAT甲级真题及训练集(17)--1052. Linked List Sorting (25)
- PAT甲级真题及训练集(20)--1086. Tree Traversals Again (25)(前序,中序建二叉树,很重要!)
- PAT甲级真题及训练集(26)--1106. Lowest Price in Supply Chain (25)
- 1107. Social Clusters (30)-PAT甲级真题
- 1115. Counting Nodes in a BST (30)-PAT甲级真题(二叉树的遍历,dfs)