1004 Counting Leaves (30)(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

1004.计算叶子个数

一个家庭的层级结构经常被表现为一个家谱树。你的任务是统计这些家庭成员中谁没有孩子。

输入

每个输入文件包含一个测试实例。每个实例开始的一行包含N和M,N指树中的结点个数(0<N<100),M指非叶结点的个数。然后下面有M行,每行的格式如下:

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

ID是一个两位数的数字,表示一个非叶结点。K表示其孩子的数量。随后是一个序列,序列中是该结点的孩子结点的两位数ID。为了简单起见,我们把根结点的ID固定为01。

输出

对于每个测试实例,你应该计算从根结点开始的每一层中没有孩子的家庭成员的个数。数字必须在一行内输出,用空格分隔,在每行结尾不能有多余的空格。

测试样例表示了一个只有两个结点的树,01是根结点,02是它仅有的孩子。因此在根结点01层级,没有叶节点。再下一层级,有一个叶结点。然后我们应该在一行内输出“0 1”。

我用dfs建的树,要注意特殊情况:

0 0 是0

1 0是1

dfs的第4个测试点一直段错误(27/30),求各位大佬帮助,bfs第4个也一直段错误,第2个答案错误(19/30),晕死


#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#define inf 0x3f3f3f3f
using namespace std;
queue<int>q[];
int a[];
int rec[];
int n,m;
void build(int root,int k)
{
int count=;
while(!q[k].empty())
{
int x=q[k].front();
q[k].pop();
a[n*root+rec[count]]=x;
if(!q[x].empty())
{
build(n*root+rec[count],x);
}
count++;
}
} int main()
{
cin>>n>>m;
for(int i=;i<=n+;i++)
{
rec[i-]=i-n;
}
memset(a,,sizeof(a));
if(n==)
cout<<;
else if(m==)
cout<<;
for(int i=;i<=m;i++)
{
int x;
cin>>x;
int nn;
cin>>nn;
for(int j=;j<=nn;j++)
{
int y;
cin>>y;
q[x].push(y);
}
}
a[]=;
build(,);
int st=,en=;
for(int i=;i<=m*n;i++)
{
int sum=;
bool f=;
for(int j=st;j<=st+en-;j++)
{
if(a[j]!=&&a[n*j+rec[]]==)
sum++;
if(a[j]!=)
f=;
}
if(f==) break;
if(st==)
{
cout<<sum;
}
else
{
cout<<" "<<sum;
}
st=st+en;
en=en*n;
}
return ;
}

bfs代码AC代码

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#define inf 0x3f3f3f3f
using namespace std;
int n,m;
vector<int>v[];
queue<int>q;
int main()
{
cin>>n>>m;
for(int i=;i<=m;i++)
{
int x;
cin>>x;
int num;
cin>>num;
for(int j=;j<=num;j++)
{
int y;
cin>>y;
v[x].push_back(y);
}
}
q.push();
if(m==)
cout<<;
else if(n==)
cout<<;
else
{
if(v[].size()==)
{
cout<<;
}
else
cout<<;
int k=;
int kk=;
int nn=;
int count=;
while(!q.empty())
{
if(nn==k)
{
cout<<" "<<count;
count=;
nn=;
k=kk;
kk=;
}
nn++;
int x=q.front();
q.pop();
for(int j=;j<v[x].size();j++)
{
int y=v[x].at(j);
if(v[y].size()==)
count++;
else
{
q.push(y);
kk++;
} }
}
cout<<" "<<count;
}
return ;
}
 

PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)的更多相关文章

  1. 1004 Counting Leaves (30 分)(树的遍历)

    给出一棵树,问每一层各有多少叶子节点 dfs遍历树 #include<bits/stdc++.h> using namespace std; vector<]; int n,m; i ...

  2. 1004 Counting Leaves (30 分)

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  3. 1004. Counting Leaves(30)—PAT 甲级

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  4. PTA 1004 Counting Leaves

    题目描述: A family hierarchy is usually presented by a pedigree tree. Your job is to count those family ...

  5. PAT甲1004 Counting Leaves【dfs】

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

  6. 1004 Counting Leaves (30分) DFS

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

  7. PAT 1004 Counting Leaves (30分)

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

  8. 1004. Counting Leaves (30)

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

  9. PAT 解题报告 1004. Counting Leaves (30)

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

随机推荐

  1. tornado源码分析系列一

    先来看一个简单的示例: #!/usr/bin/env python #coding:utf8 import socket def run(): sock = socket.socket(socket. ...

  2. 在.NET Core中连接使用Zookeeper

    一开始找到的是ZookeeperNetEx,但是很多API都很原始,不怎么好用. 最后确定用Rabbit.Zookeeper来做,他对ZookeeperNetEx进行了封装,要简单不少. 和c语言和j ...

  3. yii2.0 使用不同语言

    1.建立语言目录.文件.项目根目录建立messages文件夹.存放不同语言对应的目录文件. 例如中文和英文 message 下建立两个文件夹 en.zh_CN 里面可以对应着多个翻译文件 2.在mai ...

  4. (转)MapReduce Design Patterns(chapter 6 (part 2))(十二)

    Chain Folding 这是对job 链的一种优化.基本上是一种大体规则:每条记录都会提交给多个mapper,或者给reducer然后给mapper.这种综合处理方法会节省很多读文件和传输数据的时 ...

  5. android 城市选择

    我们在开发过程中兰冕会有选着城市地点等东西,这些都是常用的东西,所以我也就将他封装起来了先来看看效果吧 1.首先看下项目的结构: 2.看下整体的项目效果 三:主ativity private Cont ...

  6. if条件判断

    if 条件判断的是布尔值,常用的有以下几种 1.in 在不在它里面,返回的是布尔值 names='zhangsan lisi xiaoming' print("zhangsan" ...

  7. 02-C与OC语言的一些小知识

    1.        #import 跟#include.@class有什么区别?#import<> 跟 #import”"又什么区别? 1>  #import和#inclu ...

  8. 【剑指offer】二叉搜索树转双向链表,C++实现

    原创博文,转载请注明出处! # 题目 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 二叉树节点的定义 struct TreeNod ...

  9. docker下的Jenkins安装和体验【转】

    原文地址:http://blog.csdn.net/boling_cavalry/article/details/78942408 作为一款优秀的持续集成工具,jenkins在日常的项目中经常会用到, ...

  10. 模块(Modules)

    一.引入模块 模块:当编写更大的应用程序时,所有的代码肯定会分成多个文件,这样便于维护,另外已经编写好的函数和对象在被多个程序中使用时,不用把函数和对象拷贝到每个程序中. 模块支持以上功能,在Pyth ...