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,

每一个输入文件包含一个测试用例,每一个测试用例由一行开始,包含N,

the number of nodes in a tree, and M (< N), the number of non-leaf nodes.

表示数节点的数目,和一个M,表示不是叶子的节点

Then M lines follow, each in the format:

接下来M行,每一行格式是这样的:

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

where ID is a two-digit number representing a given non-leaf node,

ID是一个两位数表示一个被给出的非叶子节点

K is the number of its children, followed by a sequence of two-digit ID's of its children.

K是它孩子的数量,接下来一个连续的两位数是它孩子的ID

For the sake of simplicity, let us fix the root ID to be 01.

为了让问题简单化,让我们定义跟节点ID为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.

01是根节点,02是它唯一的孩子

Hence on the root 01 level, there is 0 leaf node;

因此对于01所在的等级来说,没有叶子为0的节点。

and on the next level, there is 1 leaf node. Then we should output "0 1" in a line.

然后对于下一个等级来说,有一个叶子的节点,所以在一行中输出“0 1“

Sample Input

2 1
01 1 02

Sample Output

0 1
一开始我看到题目之后的没什么好的思路。
首先我想到的就是我们用什么去存放这棵树,结构体数组链表,肯定可以存放所有完整的关系。
但是当我建完结构体之后我就发现,其实对于这道题目的输出来说,父子的关系并没有那么重要,尤其是这个父亲有那几个孩子,这个信息基本用不到。
然后就是这个节点在这颗树的等级,其实只要知道自己的父亲是谁,自己的父亲等级是多少,那么就能知道自己的等级了。
分析完这几个点之后,下面就是要注意的地方了。
 
首先我们不能直接在输入的时候就确定等级,因为你输入的时候,虽然你知道你父亲是谁了,但是你不一定知道你父亲的等级是多少,有可能你父亲的等级还没有确定呢。所以我们只能先记录父亲。然后通过后面的循环去把所有的等级更新一遍。
这也导致了这是这道题时间复杂度最高的地方了。因为简单起见我用了结构体数组去存放,这样的存放相比树来说,更新值就比较麻烦了,需要循环每个值去更新。所以时间复杂度就一下到了N^2
如果用树存放的话一定会好很多。
 
还有可以优化的地方是,可以把最后的求每个等级最后答案的数组的循环放在这个N^2里面,这样还能再快一些。
 
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<string.h> using namespace std; int result[];
struct Node
{
int level;//当前节点所在的等级
int flag;//0没有孩子,1是有孩子
int father;//父节点
}; int main()
{
struct Node nodes[];
int n,m,i,j;
int nowNode,nowNodeNumber,childNode;
int maxLevel=;
cin>>n>>m; //初始化
for (i = ; i <= n; i++)
{
nodes[i].level = ;
nodes[i].flag = ;
nodes[i].father = ;
}
nodes[].level = ; //输入并保存关系
while (m--)
{
cin>>nowNode;
cin>>nowNodeNumber; if(nowNodeNumber != )
nodes[nowNode].flag = ; while (nowNodeNumber--)
{
cin>>childNode;
//保存自己的父亲是谁
nodes[childNode].father = nowNode;
}
} for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
//如果有一个点的父亲标识是自己,那么它就是你的儿子,那么他的等级,应该是你的等级+1
if(nodes[j].father == i)
{
nodes[j].level = nodes[i].level + ;
}
}
} //查询每一个等级有多少个没有孩子的点,记录在result数组中
for (i = ; i <= n; i++)
{
if(nodes[i].flag != && nodes[i].level > )
result[nodes[i].level]++;
//记录最大的等级,用于最后的输出
if(nodes[i].level > maxLevel)
maxLevel = nodes[i].level;
}
for (i = ; i < maxLevel; i++)
{
cout<<result[i]<<" ";
}
cout<<result[i];
return ;
}

PAT1004的更多相关文章

  1. PAT1004:Counting Leaves

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

  2. PAT-1004 Counting Leaves

    1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...

  3. pat1004. Counting Leaves (30)

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

  4. PAT (Advanced Level) Practice 1001-1005

    PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...

随机推荐

  1. Excel补全日期(日期按顺序补全)

    1.给出的数据 2.想补全缺失的日期,比如2015/3/1,2015/3/2,... 1)在D列输入完整的日期,如下图所示: 2)在E2处写函数“=IF(ISERR(VLOOKUP(D2,B:C,2, ...

  2. visible绑定(The "visible" binding)

    对visible进行绑定可以控制元素的显示和隐藏. 示例: <div data-bind="visible: shouldShowMessage"> You will ...

  3. CentOS 7 多网卡绑定

    根据官方文档Red_Hat_Enterprise_Linux-7-Networking_Guide-en-US用nmcli做起来还是相当容易的.下面把俺的步骤贴下. 1.查看目前网卡的名称和状态.#n ...

  4. zendstudio 安装 手册

    安装 http://jingyan.baidu.com/article/b907e627b14fbb46e6891c65.html 选择baidu官方32bit安装 补丁破解网址 http://dwt ...

  5. Jena 简介:通过 Jena Semantic Web Framework 在 Jave 应用程序中使用 RDF 模型

    简介: RDF 越来越被认为是表示和处理半结构化数据的一种极好选择.本文中,Web 开发人员 Philip McCarthy 向您展示了如何使用 Jena Semantic Web Toolkit,以 ...

  6. js循环POST提交添加辅助单位

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  7. JavaScript高级程序设计:第六章

    第六章 面向对象的程序设计 一.理解对象 1.属性类型: ECMAScript中有两种属性:数据属性和访问器属性. (1)数据属性: 数据属性包含一个数据值的位置.在这个位置可以读取和写入值.数据属性 ...

  8. csu1010: Water Drinking

    /* 本题的题意: 沙漠中有很多骆驼和一个池塘,0表示池塘,1-N表示骆驼,输入的两个数表示两只骆驼,其中前面的那一头靠近池塘,所有的骆驼队列不交叉不相连,求站在队尾但是离水井最近的骆驼编号 经过分析 ...

  9. A - 娜娜梦游仙境系列——诡异的钢琴

    A - 娜娜梦游仙境系列——诡异的钢琴 Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Othe ...

  10. UIWebView是什么

    UIWebView类是用来显示网络内容.要使用它,可以简单的创造一个UIWebView对象,放置到窗口上,并且发送一个指向网络内容的请求.通过这个类,可以控制网页历史的前进後退,也可以通过程序去控制网 ...