PAT 1004. Counting Leaves (30)
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
- 此题我首先想到的是用两次广搜,第一次bfs等到每个节点的高度,第二次bfs得到根据每个节点所在高度得到每层(对应于高度)的叶子节点数。具体见代码:
- #include <iostream>
- #include <list>
- #include <vector>
- #include <stack>
- #include <algorithm>
- using namespace std;
- const int N=;
- vector<list<int> > Tree(N);
- int nodeHeight[N]={-}; //height from 0
- int heightLeaves[N];
- void bfs_getHeight()
- {
- stack<int> s;
- s.push();
- nodeHeight[]=;
- int curNode,childNode;
- while(!s.empty())
- {
- curNode=s.top();
- s.pop();
- for(list<int>::iterator iter=Tree[curNode].begin();iter!=Tree[curNode].end();++iter)
- {
- childNode=*iter;
- nodeHeight[childNode]=nodeHeight[curNode]+;
- if(Tree[childNode].size()!=)
- s.push(childNode);
- }
- }
- }
- void bfs_getLeaves()
- {
- stack<int> s;
- s.push();
- int curNode,childNode;
- size_t size;
- while(!s.empty())
- {
- curNode=s.top();
- s.pop();
- for(list<int>::iterator iter=Tree[curNode].begin();iter!=Tree[curNode].end();++iter)
- {
- childNode=*iter;
- if(==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=;i<m;++i)
- {
- cin>>ID>>k;
- for(int j=;j<k;++j)
- {
- cin>>IDbuf;
- Tree[ID].push_back(IDbuf);
- }
- }
- bfs_getHeight();
- bfs_getLeaves();
- int maxHeight=*max_element(nodeHeight,nodeHeight+N);
- for(int i=;i<maxHeight;++i)
- {
- cout<<heightLeaves[i]<<' ';
- }
- cout<<heightLeaves[maxHeight];
- return ;
- }
但是结果有一个3分的测试点过不了,也不知道错在哪里,实在是没招了,就用了深搜,而且正好可以在每次递归的时候传递层数,反而只需要一次dfs就可以搞定了,相对于第一种解法,第二种解法根据N-M的值为叶子节点数,在输出的时候做了优化。具体见代码:
- #include <stdio.h>
- #include <map>
- #include <vector>
- using namespace std;
- const int N=;
- map<int,vector<int> > adjlist;
- int levelLeaves[N]={};
- void dfs(int node,int level)
- {
- if(adjlist[node].empty())
- {
- ++levelLeaves[level];
- return;
- }
- vector<int>::iterator iter=adjlist[node].begin();
- for(;iter!=adjlist[node].end();++iter)
- dfs(*iter,level+);
- }
- 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(,);
- printf("%d",levelLeaves[]);
- cnt=levelLeaves[];
- for(int i=;cnt<leaves;++i)
- {
- printf(" %d",levelLeaves[i]);
- cnt+=levelLeaves[i];
- }
- printf("\n");
- return ;
- }
PAT 1004. Counting Leaves (30)的更多相关文章
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)
1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT A 1004. Counting Leaves (30)【vector+dfs】
题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...
- 【PAT Advanced Level】1004. Counting Leaves (30)
利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...
- PAT (Advanced Level) 1004. Counting Leaves (30)
简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...
- PAT甲题题解-1004. Counting Leaves (30)-统计每层叶子节点个数+dfs
统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> # ...
随机推荐
- php 接收二进制流转换成图片
php 接收二进制流转换成图片,图片类imageUpload.php如下: <?php /** * 图片类 * @author http://blog.csdn.net/haiqiao_2010 ...
- Activity与WindowManagerService服务的连接过程分析
Activity组件与WindowManagerService服务之间的连接模型如下图所示: 1. Activity组件 -> WindowManagerService的连接 Activity会 ...
- luarocks在macOS系统上的安装
luarocks是基于lua开发的一个包管理工具,所以在安装luarocks之前需要先安装lua(见博客同目录下“lua在MacOS系统上的安装”).具体的安装步骤如下: 1.源码安装部署luaroc ...
- web系列教程之php 与mysql 动态网站 。检索 与更新。
接着上次WEb 系列开发之php 与mysql动态网站入门. 个人觉得,学习技术就像一棵大树,主干很重要,枝叶其次.对于学习技术,我们应该分清主次关系.怎么学?为什么要学?有一个较好的分寸. 有时候觉 ...
- mac 布置 git仓库服务器
创建管理员账户 例如:git 使用git账户登录 开启git账户的远程登陆 创建仓库文件夹 sudo git --bare init 更改配置文件 cd /Users/userName/project ...
- 代码块(block)简介
代码块是对C语言中函数的扩展,由C语言实现,所以在以C为基础的语言内都是有效的,包括Objective_C,C++和Objective-C++,在Xcode的GCG与Clang工具中是有效的,但这不属 ...
- 韦东山教程ARM的时钟设置出现的问题及其解决方法
时钟设置是一个非常重要的环节,如果系统没有合适的时钟,根本无法工作. S3C2440的时钟复杂,分为FCLK,HCLK,PCLK. 在程序测试中,曾出现这样一个错误.系统当前FCLK为400 ...
- 【技术贴】解决 myeclipse打不开报错an error has occurred, see .
方法1.右键选中快捷方式属性选项,在快捷方式页,目标一项最后加上-clean选项,如C:\MyEclipse6\eclipse.exe -clean. 然后重新启动一下MyEclipse. 方法2. ...
- 两个有关Knockout自定义拓展方法fn的小技巧
Adding custom functions using "fn" 让observable自增/自减 最简单的方法是self.num(self.num() + 1), 但是这个写 ...
- 水平/竖直居中在旧版Safari上的bug
今天调了两个出现在旧版Safari上的layout bug. 它们最初是在同事的iPad上被发现的, 我在自己桌面上安装的Safari 5.1.7上也能够复现. Bug1: .vertical-cen ...