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> # ...
随机推荐
- python 函数1
一.背景 在学习函数之前,一直遵循:面向过程编程,即:根据业务逻辑从上到下实现功能,其往往用一长段代码来实现指定功能,开发过程中最常见的操作就是粘贴复制,也就是将之前实现的代码块复制到现需功能处,如下 ...
- C# 程序关闭和进程关闭
this.Close(); 只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出: 2.Application.Exit(); 强制所有消息中止, ...
- 重复数据插入unique列时,锁加在哪?
1.测试目的 当插入重复数据到有unique索引的表中时,采用何种加锁机制. 2.测试思路 利用10046确定是什么操作导致加锁阻塞了进程: dump锁定前最近一次操作的块结构来分析加锁机制. 3.测 ...
- VHDL程序的库
VHDL库存储和放置了可被其他VHDL程序调用的数据定义.器件说明.程序包等资源.VHDL库的种类有很多,但最常见的库有IEEE标准库.WORK库.IEEE标准库主要包括STD_LOGIC_1164. ...
- App页面显示优化
在开发移动端APP页面时,对各操作系统各种型号的手机进行适配是必须的.然鹅,上周在开发完一个落地页后,被测试给打了回来,其中列出了一个在我看来很小的问题:单击进入页面的时候,页面还没加载完的时候字体显 ...
- python面向对象高级编程
正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性.先定义class: >>> class Studen ...
- Android 网络请求详解
我们知道大多数的 Android 应用程序都是通过和服务器进行交互来获取数据的.如果使用 HTTP 协议来发送和接收网络数据,就免不了使用 HttpURLConnection 和 HttpClient ...
- android 对象传输及parcel机制
在开发中不少要用到Activity直接传输对象,下面我们来看看,其实跟java里面差不多 自定义对象的传递:通过intent传递自定义对象的方法有两个 第一是实现Serialization接口: ...
- 17.1.1.7 Setting Up Replication with New Master and Slaves 设置复制使用新的master和slaves:
17.1.1.7 Setting Up Replication with New Master and Slaves 设置复制使用新的master和slaves: 最简单和最直接方式是设置复制使用新的 ...
- 【HDOJ】2157 How many ways??
矩阵乘法,用DP做各种wa,后来发现原因了. #include <stdio.h> #include <string.h> typedef struct { ][]; } ma ...