IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> PAT 1004 Counting Leaves (C++) -> 正文阅读

[C++知识库]PAT 1004 Counting Leaves (C++)

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

Input Specification:

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.

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

Output Specification:

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

题意:给出n个点个m个有孩子的节点,问每一层无孩节点的个数

测试点2是节点就只有根节点(n=1,m=0),输出1

解析:我们先读入每个有孩子的节点,然后开一个sd[ i ]数组来记录第 i 节点在哪一层,因为根节点在第1层,因此我们可以遍历所有点,他们的孩子的深度就是父节点深度+1,然后利用广搜来进行答案记录

#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
const int N=105;
vector<int> v[N];
int g[N],n,m,sd[N];//g记录每个深度无孩节点的个数,sd[i]记录第i节点所在的层数
void solve()
{
    queue<int> q;
    q.push(1);
    while(q.size())
    {
        int u=q.front();
        q.pop();
        for(int i=0;i<v[u].size();i++)
        {
            int j=v[u][i];
            if(v[j].size()==0) g[sd[j]]++;//对应节点没有孩子,对应深度满足条件节点个数+1
            else q.push(j);
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    sd[1]=1;//根节点在深度1
    int maxn=0;//记录最大深度,为后输出
    while(m--)
    {
        int fa,k,p;
        scanf("%d%d",&fa,&k);
        while(k--)
        {
            scanf("%d",&p);
            v[fa].push_back(p);//父节点存储儿子
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<v[i].size();j++)
        {
            sd[v[i][j]]=sd[i]+1;//儿子深度=父亲深度+1
            maxn=max(maxn,sd[i]+1);//更新最大深度
        }
    }
    solve();
    for(int i=1;i<=maxn;i++)
    {
        if(i!=1) printf(" ");
        printf("%d",g[i]);
    }
    if(maxn==0) printf("1");//特判,节点就只有根节点,输出1
    printf("\n");
    return 0;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-11-05 00:06:35  更:2022-11-05 00:11:30 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 5:06:21-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码