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.

输入

Each input file contains one test case. Each case starts with a line containing 0<N<1000<N<100, the number of nodes in a tree, and M(<N)M(<N), the number of non-leaf nodes. Then M lines follow, each in the format:

1
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.

The input ends with N being 0. That case must NOT be processed.

输出

For each test case, you are supposed to count those family members who have no child for every seniority levelstarting 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 02is 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.

样例

输入

1
2
2 1
01 1 02

输出

1
0 1

思路

大意为按照输入建树,输出树的每层叶节点的个数

一开始就想到了使用并查集来写,但是退役一个月之后菜的非常真实。。。并查集都写错好几次。。。好在后来改了改过了

不过我对PAT甲级的印象是考察各种数据结构的知识,既然噎死跟树相关,那我建立一棵树再扫一下的话肯定也是正确的。下面除了并查集的代码以外,再附加一份树的代码。

代码-并查集

/*
 * ===========================================================
 *
 *       Filename:  1004.cpp
 *
 *           Link:
 *
 *        Created:  2019/01/04 20时42分35秒
 *
 *         Author:  duny31030 , duny31030@126.com
 *   Organization:  QLU_浪在ACM
 *
 * ===========================================================
 */
#include <bits/stdc++.h>
using namespace std;
#define clr(a, x) memset(a, x, sizeof(a))
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define pre(i,a,n) for(int i=n;i>=a;i--)
#define ll long long
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 110;

bool b[N];
int f[N];
int c[N];


int getdeep(int i)
{
    if(f[i] == 0)
        return 1;
    return getdeep(f[i])+1;
}

void init()
{
    rep(i,0,N-1)    b[i] = 1;
    clr(f,0);
    clr(c,0);
}

int main()
{
    ios
    #ifdef ONLINE_JUDGE
    #else
        freopen("in.txt","r",stdin);
        // freopen("out.txt","w:",stdout);
    #endif
    int n,m,t1,t2,co;
    int mmax = 0;
    while(cin >> n >> m)
    {
        mmax = 0;
        init();
        rep(i,1,m)
        {
            cin >> t1 >> co;
            b[t1] = 0;
            rep(j,1,co)
            {
                cin >> t2;
                f[t2] = t1;
            }
        }
        rep(i,1,n)
        {
            if(b[i])
            {
                int deep = getdeep(i);
                c[deep]++;
                mmax = max(mmax,deep);
            }
        }
        rep(i,1,mmax)
        {
            cout << c[i];
            if(i == mmax)
                cout << endl;
            else
                cout << " ";
        }
    }
    fclose(stdin);
    // fclose(stdout);
    return 0;
}
原文地址:https://www.cnblogs.com/duny31030/p/14305287.html