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 maxLevel = 1;
    cin >> n >> m;

    //初始化
    for (int i = 0;i <= n; i++) {
        node[i].level = 0;
        node[i].flag = 0;
        node[i].father = 0;
    }
    //编号为1的根节点层号为1
    node[1].level = 1;
    //输入并保存关系
    while (m--) {
        cin >> nowNode;//节点编号
        cin >> nowNodeNumber;//孩子数
        //标记该节点有孩子
        if (nowNodeNumber > 0) {
            node[nowNode].flag = 1;
        }
        for (int j = 0;j < nowNodeNumber;j++) {
            cin >> childNode;
            //保存自己的父亲节点
            node[childNode].father = nowNode;
        }
    }

    for (int i = 1;i <= n;i++) {
        for (int j = 0;j <= n;j++) {
            if (node[j].father == i) {
                node[j].level = node[i].level + 1;
            }
        }
    }
    for (int i = 1;i <= n;i++) {
        if (node[i].flag == 0) {//叶节点
            result[node[i].level]++;
        }
        //记录下最大的层号
        if (node[i].level > maxLevel) {
            maxLevel = node[i].level;
        }
    }

    for (int i = 1;i < maxLevel;i++) {
        cout << result[i] << " ";
    }
    cout << result[maxLevel];
    return 0;
}

参考博客:http://www.cnblogs.com/linkstar/p/5674895.html

原文地址:https://www.cnblogs.com/chance-zou/p/10476932.html

时间: 2024-10-22 03:36:59

1004 Counting Leaves (30 分)的相关文章

1004 Counting Leaves (30分)

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

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

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

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

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

#include<stdio.h> #include<vector> const int MAX=510; using namespace std; int n,m,le=0; //节点数,非叶子节点数,最深层叶层数 vector<int> child[MAX]; //存储孩子情况 int number[MAX]; //每一层叶子数 void DFS(int s,int l) { if(child[s].size()==0) { ++number[l]; if(le&l