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 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;
}
View Code

 

 

 

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[105];
queue<int>q;
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int x;
        cin>>x;
        int num;
        cin>>num;
        for(int j=1;j<=num;j++)
        {
            int y;
            cin>>y;
            v[x].push_back(y);
        }
    }
    q.push(1);
    if(m==0)
        cout<<1;
    else if(n==0)
        cout<<0;
    else
    {
        if(v[1].size()==0)
        {
            cout<<1;
        }
        else
            cout<<0;
        int k=1;
        int kk=0;
        int nn=0;
        int count=0;
        while(!q.empty())
        {
            if(nn==k)
            {
                cout<<" "<<count;
                count=0;
                nn=0;
                k=kk;
                kk=0;
            }
            nn++;
            int x=q.front();
            q.pop();
            for(int j=0;j<v[x].size();j++)
            {
                int y=v[x].at(j);
                if(v[y].size()==0)
                    count++;
                else
                {
                    q.push(y);
                    kk++;
                }

            }
        }
        cout<<" "<<count;
    }
    return 0;
}

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM