首页 > 其他分享 >1004 Counting Leaves(dfs):邻接表版:写的太多了

1004 Counting Leaves(dfs):邻接表版:写的太多了

时间:2024-07-11 17:30:53浏览次数:17  
标签:case 01 int Leaves dfs depth Counting root ID

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

代码

//就是找叶子节点数,然后每一层输出

#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

const int N = 110*110,M = 2*N;

int e[M],ne[M],h[N],idx;
bool st[N];
int depth[N];
int n,m;
int f[N];
int res;

void add(int a,int b){
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}

void dfs(int u,int fa){
    for(int i=h[u];~i;i=ne[i]){
        int j=e[i];
        if(j==fa)continue;
        depth[j]=depth[u]+1;
        if(j==0){
            f[depth[u]]++;
            return;
        }
        dfs(j,u);
        res=max(res,depth[j]);
    }
}

int main(){
    cin>>n>>m;

    memset(h,-1,sizeof h);

    for(int i=1;i<=m;i++){
        int root,k;
        cin>>root>>k;
        st[root]=true;
        while(k--){
            int x;
            cin>>x;
            add(root,x);
            add(x,root);
        }
    }
    
    if(!m){
        cout<<n<<endl;
        return 0;
    }
    
    for(int i=1;i<=n;i++){
        if(!st[i]){
            add(i,0);
            add(0,i);
        }
    }
    
    depth[1]=1;
    dfs(1,-1);
    for(int i=1;i<res;i++){
        cout<<f[i]<<' ';
    }

    cout<<f[res];

    return 0;
}

标签:case,01,int,Leaves,dfs,depth,Counting,root,ID
From: https://blog.csdn.net/m0_60738889/article/details/140354316

相关文章

  • 一文了解HDFS
    1.简介1.1.概述HDFS是基于流数据访问模式的分布式文件系统,其设计建立在“一次写入、多次读取”的基础上,提供高吞吐量、高容错性的数据访问,能很好地解决海量数据的存储问题。流数据:是指数千个数据源持续生成的数据,可以理解为随时间延续而无限增长的动态数据集合......
  • spark程序在hdfs集群执行,提示: “main“ org.apache.spark.SparkException: Failed to
    1.执行代码spark在hadoop上以集群模式执行代码bin/spark-submit\--masteryarn\--deploy-modecluster\--executor-memory1G\--total-executor-cores2\/root/word_count_cluster.py2.错误截图错误原因:找不到spark目录3.解决办法在/etc/profile文件中配置spa......
  • 深度优先搜索 DFS
    <目录深度优先搜索\(\sf\small\color{gray}Depth\First\Search\)基本思想基于递归的执着的枚举算法\(\hspace{2cm}\)——\(\sf\small\color{black}David\)这句话里有三个关键词。\(\boxed{\sf递归}\boxed{\sf\textit{执着}}\boxed{\sf枚举}\)枚举枚举,可谓是解决问......
  • 【DFS(深度优先搜索)详解】看这一篇就够啦
    【DFS详解】看这一篇就够啦......
  • 【DFS】深度优先搜索 洛谷选数例题讲解
    DFS深搜选数问题看一看题......
  • HDFS分布式集群搭建
    1、集群简介Hadoop集群具体来说包含两个集群:HDFS集群和YARN集群,两者逻辑上分离,但物理上常在一起。另外,对于Hadoop的集群来讲,可以分为两大类角色:master和slave。(1)HDFS集群:负责海量数据的存储,集群中的角色主要有:NameNode(一个,master)、DataNode(若干,slave)和SecondaryName......
  • 回溯大合集+主元素+DFS图
    子集和问题importjava.util.Scanner;​publicclassMain{  staticintn;//元素个数  staticinttarsum;//目标和  staticintremainSum=0;//当前元素加到最后一个元素的总和,剩余元素和  staticintsesum=0;//已选元素之和  static......
  • 力扣第22题:括号生成 深度优先搜索(DFS)和它的优化(C++)
    数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。示例:输入:n=3输出:["((()))","(()())","(())()","()(())","()()()"]思路递出去,再归回来,是为递归。DFS算法是利用递归思想的一种搜索算法。想象一个矿井,从地面到井底有多层......
  • 【AcWing】842. 排列数字(DFS)
    DFS是树形结构,深度优先搜索。dfs可以算作递归。横线上填和前面不一样的数字。每一次向下探索都一条路走到黑,直到不能走再回溯。#include<iostream>usingnamespacestd;constintN=10;intn;intpath[N];//存放走过的路径boolst[N];//存放1~n哪个数用过了,全局b......
  • C#实现DFS和BFS
    以图示为例:Noderoot=newNode("A",newNode("B",newNode("C"),newNode("D")),newNode("E",newNode("F"),newNode("......