PTA 1004 Counting Leaves (30)(30 分)(建树dfs或者bfs,未AC 段错误)

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),晕死

2 1
01 1 02

2 2
01 2 02 03
02 2 04 05

3 3
01 3 02 05 06
02 1 03
05 2 04 07

4 1
01 2 02 03

2 3
01 2 02 03
02 2 04 05
05 2 06 07

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

bfs代码

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
    int key;
    queue<int>q;
}in[10005];

int a[10000005];
int rec[10005];
int n,m;
struct cys
{
    int k;
    int root;
};
queue<cys>Q;
void build()
{
    while(!Q.empty())
    {
        cys x=Q.front();
        Q.pop();
        int count=1;
        int k=x.k;
        int root=x.root;
        int pp=0;
        for(int i=1;i<=m;i++)
        {
            if(in[i].key ==x.k)
            {
                pp=i;
                break;
            }
        }
        if(pp==0) continue;
        while(!in[pp].q.empty())
        {
            int x=in[pp].q.front();
            in[pp].q.pop();
            a[n*root+rec[count]]=x;
            cys aa;
            aa.k=x;
            aa.root=n*root+rec[count];
            Q.push(aa);
            count=count+1;
        }
    }
}

int main()
{
    cin>>n>>m;
    for(int i=2;i<=n+1;i++)
    {
        rec[i-1]=i-n;
    }
    memset(a,0,sizeof(a));
    if(n==0)
        cout<<0;
    else if(m==0)
        cout<<1;
    int k1=0;
    for(int i=1;i<=m;i++)
    {
        cin>>in[i].key;
        if(in[i].key==1)
            k1=i;
        int x;
        cin>>x;
        for(int j=1;j<=x;j++)
        {
            int y;
            cin>>y;
            in[i].q.push(y);
        }
    }

    a[1]=1;
    cys aa;
    aa.k=k1;
    aa.root=1;
    Q.push(aa);
    build();

    int st=1,en=1;
    for(int i=1;i<=m*n;i++)
    {
        int sum=0;
        bool f=0;
        for(int j=st;j<=st+en-1;j++)
        {
            if(a[j]!=0&&a[n*j+rec[1]]==0)
                sum++;
            if(a[j]!=0)
                f=1;
        }
        if(f==0) break;
        if(st==1)
        {
            cout<<sum;
        }
        else
        {
            cout<<" "<<sum;
        }
        st=st+en;
        en=en*n;
    }
    return 0;
}

 

原文地址:https://www.cnblogs.com/caiyishuai/p/9443618.html

时间: 2024-10-12 21:29:52

PTA 1004 Counting Leaves (30)(30 分)(建树dfs或者bfs,未AC 段错误)的相关文章

1004 Counting Leaves (30 分)

#include <iostream> using namespace std; typedef struct { int level;//节点所在层次 int flag;//0没有孩子,1有孩子 int father;//父节点 }Node; int main() { Node node[205]; int n, m;//n个节点,m个非叶子节点 int nowNode, nowNodeNumber, childNode; int result[205] = { 0 }; int maxLe

1004 Counting Leaves (30)(30 分)

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 < 10

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 tre

1004. Counting Leaves (30)——PAT (Advanced Level) Practise

题目信息: 1004. Counting Leaves (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 contai

1004 Counting Leaves (30分)

今天在热心网友的督促下完成了第一道PAT编程题. 太久没有保持训练了,整个人都很懵. 解题方法: 1.读懂题意 2.分析重点 3.确定算法 4.代码实现 该题需要计算每层的叶子节点个数,所以选用BFS 还有一个关键问题是 如何记录一层的开始和结束 另外,对于新手来说,图的存储也是一个知识点 容易忽略特殊取值情况下的答案: 当非叶节点个数为0,只有根节点一个点,所以直接输出1 而其他情况下,第一层叶子节点数为0 /**/ #include <cstdio> #include <cstrin

PAT 1004. Counting Leaves (30) C#实现

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

1004. Counting Leaves (30)

时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 start

PAT甲题题解-1004. Counting Leaves (30)-统计每层叶子节点个数+dfs

统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> using namespace std; /* 统计每层的叶子节点个数 建树,然后dfs即可 */ const int maxn=105; int n,m; int layer[maxn]; //统计每层的叶子节点

PAT (Advanced Level) 1004. Counting Leaves (30)

简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<vector> using namespace std; const int maxn=100+10; vector<int>g[maxn]; int n,m; int ans[maxn]; int root; i