博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT 1004. Counting Leaves (30)
阅读量:4628 次
发布时间:2019-06-09

本文共 3350 字,大约阅读时间需要 11 分钟。

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 101 1 02

Sample Output

0 1 此题我首先想到的是用两次广搜,第一次bfs等到每个节点的高度,第二次bfs得到根据每个节点所在高度得到每层(对应于高度)的叶子节点数。具体见代码:
#include 
#include
#include
#include
#include
using namespace std;const int N=100;vector
> Tree(N);int nodeHeight[N]={-1}; //height from 0 int heightLeaves[N];void bfs_getHeight(){ stack
s; s.push(1); nodeHeight[1]=0; int curNode,childNode; while(!s.empty()) { curNode=s.top(); s.pop(); for(list
::iterator iter=Tree[curNode].begin();iter!=Tree[curNode].end();++iter) { childNode=*iter; nodeHeight[childNode]=nodeHeight[curNode]+1; if(Tree[childNode].size()!=0) s.push(childNode); } }}void bfs_getLeaves(){ stack
s; s.push(1); int curNode,childNode; size_t size; while(!s.empty()) { curNode=s.top(); s.pop(); for(list
::iterator iter=Tree[curNode].begin();iter!=Tree[curNode].end();++iter) { childNode=*iter; if(0==Tree[childNode].size()) ++heightLeaves[nodeHeight[childNode]]; else s.push(childNode); } }}int _tmain(int argc, _TCHAR* argv[]){ int n,m; cin>>n>>m; int ID,k,IDbuf; for(int i=0;i
>ID>>k; for(int j=0;j
>IDbuf; Tree[ID].push_back(IDbuf); } } bfs_getHeight(); bfs_getLeaves(); int maxHeight=*max_element(nodeHeight,nodeHeight+N); for(int i=0;i
View Code

但是结果有一个3分的测试点过不了,也不知道错在哪里,实在是没招了,就用了深搜,而且正好可以在每次递归的时候传递层数,反而只需要一次dfs就可以搞定了,相对于第一种解法,第二种解法根据N-M的值为叶子节点数,在输出的时候做了优化。具体见代码:

#include 
#include
#include
using namespace std;const int N=101;map
> adjlist;int levelLeaves[N]={
0};void dfs(int node,int level){ if(adjlist[node].empty()) { ++levelLeaves[level]; return; } vector
::iterator iter=adjlist[node].begin(); for(;iter!=adjlist[node].end();++iter) dfs(*iter,level+1);}int _tmain(int argc, _TCHAR* argv[]){ freopen("1004.txt","r",stdin); int N,M,ID,K,childID,leaves,cnt; scanf("%d%d",&N,&M); leaves=N-M; while(M--) { scanf("%d%d",&ID,&K); while(K--) { scanf("%d",&childID); adjlist[ID].push_back(childID); } } dfs(1,0); printf("%d",levelLeaves[0]); cnt=levelLeaves[0]; for(int i=1;cnt
View Code

 

转载于:https://www.cnblogs.com/wwblog/p/3704428.html

你可能感兴趣的文章
maven的配置-2019-4-13
查看>>
进程调度
查看>>
百练 2973 Skew数 解题报告
查看>>
C# 温故而知新:Stream篇(二)
查看>>
回首2016,展望2017
查看>>
你为什么应该经常访问招聘网站?招聘网站至少有4个方面的价值!
查看>>
HashMap源码分析(一)
查看>>
玩转Android之二维码生成与识别
查看>>
Python学习之路基础篇--10Python基础,函数进阶
查看>>
count http://www.cplusplus.com/reference/algorithm/count/
查看>>
回调那些事儿
查看>>
Selenium2(WebDriver)总结(二)---Firefox的firebug插件参数设置(补充)
查看>>
个人冲刺1
查看>>
OS模块
查看>>
用node实现websocket协议
查看>>
Cocos2d-x学习笔记(三十)之 游戏存档
查看>>
对相机所看的视角截屏保存为图片
查看>>
最快地复制一张表
查看>>
Asp.Net 构架(HttpModule 介绍)
查看>>
PHP-错误处理
查看>>