1004 Counting Leaves(详细注解+30行代码)

news/2024/7/15 18:59:31 标签: 深度优先, 图论, 算法

1004 Counting Leaves

分数 30

全屏浏览题目

切换布局

作者 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 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

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

代码如下:

#include<bits/stdc++.h>
using namespace std;
const int N=103;
vector<int>v[N];
int n,m;
int cnt[N];
int max_depth=-1;
void dfs(int node,int h){//node和h分别为结点和层数 
    if(v[node].size()==0){//没有孩子的结点,即叶结点 
        cnt[h]++;//该层的叶子数加1 
        max_depth=max(h,max_depth);//更新最大层数 
        return ;
    }
    for(int i=0;i<v[node].size();i++){//深度遍历 
        dfs(v[node][i],h+1);//每次层数加1 
    }
}
int main(){
    cin>>n>>m;
    while(m--){
        int id,k;
        cin>>id>>k;
        while(k--){
            int child;
            cin>>child;
            v[id].push_back(child);//保存每个结点的孩子结点 
        }
    }
    dfs(1,0);//深度遍历,记录每层叶子数,从根结点1和第0层开始遍历 
    cout<<cnt[0];//输出 
    for(int i=1;i<=max_depth;i++){//输出 
        cout<<' '<<cnt[i];
    }
    return 0;
}


http://www.niftyadmin.cn/n/299475.html

相关文章

消息队列中的事务消息

大家好&#xff0c;我是易安&#xff01;今天我们谈一谈消息队列中的事务消息这个话题。 一说起事务&#xff0c;你可能自然会联想到数据库。我们日常使用事务的场景&#xff0c;绝大部分都是在操作数据库的时候。像MySQL、Oracle这些主流的关系型数据库&#xff0c;也都提供了…

3BHB003154R0101确定每个控制器将如何知道设备地址、识别发给它的消息

3BHB003154R0101确定每个控制器将如何知道设备地址、识别发给它的消息 DNP3 协议用于各种 SCADA 系统组件之间的通信。这些系统组件包括 SCADA 主站或HMI、远程终端单元和智能电子设备。SCADA 系统的操作员可以在其操作中监控 DNP3 协议&#xff0c;以提高系统可靠性。这将通过…

RFID系统在物流仓储中的应用

RFID系统是一种无线识别技术&#xff0c;最近成为物流仓储行业的热门话题。本文将介绍RFID系统在物流仓储中的应用&#xff0c;包括如何使用RFID标签进行物流管理&#xff0c;如何使用RFID技术提高仓库的安全性&#xff0c;并细述RFID技术在物流仓储中的优势。除此之外&#xf…

【C++中可调用对象和function】

C中有如下几种可调用对象&#xff1a;函数、函数指针、lambda表达式、bind对象、仿函数。其中&#xff0c;lambda表达式和bind对象是C11标准中提出的(bind机制并不是新标准中首次提出&#xff0c;而是对旧版本中bind1st和bind2st的合并)。个人认为五种可调用对象中&#xff0c;…

数据结构算法-图技术点

引言 胡图图:“我成为电脑砖家(人们都在我吧上评论电脑配置).,按理说我应该开一家图图计算机研究科技公司…”! 于小美:“没错,图图应该开一家公司 来扩展你的专业知识” 何壮壮:“厉害是厉害 ,要不要大哥来帮帮你(至于钱,好说:月薪2万)…” 图图:“你狮子大开口! ,那你还是当…

DS200TCQCG1BKG什么是控制模式,控制模式如何分类?

​ DS200TCQCG1BKG.什么是控制模式&#xff0c;控制模式如何分类&#xff1f; 控制回路的功能是在受控变量偏离该值时将其恢复到其设定值&#xff0c;从而将过程保持在所需条件下。实现这一点的动作称为控制模式。 控制方式分为两类 连续模式包括比例、积分和微分模式。 什么…

带你玩转数据结构-单链表(适合初学者的文章,讲解的很仔细哦)

前言: &#x1f388;个人主页:&#x1f388; :✨✨✨初阶牛✨✨✨ &#x1f43b;推荐专栏: &#x1f354;&#x1f35f;&#x1f32f; C语言进阶 &#x1f511;个人信条: &#x1f335;知行合一 &#x1f349;本篇简介:>:讲解数据结构中链表的知识,;链表的分类,c语言实现单链…

【论文阅读】机器学习指导数学(Deepmind发表在Nature的论文)

前情 这个文章不是提供一个新的技术&#xff0c;而是把机器学习应用到一个新的领域上 拿出来这个问题&#xff0c;做出在这个领域好的结果 nature是一个周刊 可能nature上人工智能的文章&#xff0c;一年也不超过十篇 文章一般有八页 标题&#xff1a;AI怎么指导人的直觉…