题目很简单,就是统计一下每层的节点数,输出节点数最多的个数和对应的层数即可。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h> using namespace std;
const int maxn=;
int num[maxn];
int head[maxn];
int tot; struct Edge{
int to;
int next;
}edge[maxn]; void init(){
memset(head,-,sizeof(head));
tot=;
}
void add(int x,int y){
edge[tot].next=head[x];
edge[tot].to=y;
head[x]=tot++;
}
void dfs(int root, int layer){
num[layer]++;
for(int i=head[root];i!=-;i=edge[i].next){
int v=edge[i].to;
dfs(v,layer+);
}
}
int main()
{
int n,m,k,a,b;
init();
scanf("%d %d",&n,&m);
for(int i=;i<=m;i++){
scanf("%d %d",&a,&k);
for(int j=;j<k;j++){
scanf("%d",&b);
add(a,b);
}
}
memset(num,,sizeof(num));
dfs(,);
int maximum=,layer;
for(int i=;i<maxn;i++){
if(num[i]>maximum){
maximum=num[i];
layer=i;
}
}
printf("%d %d\n",maximum,layer);
return ;
}

1094. The Largest Generation (25)-(dfs,树的遍历,统计每层的节点数)的更多相关文章

  1. PTA甲级1094 The Largest Generation (25分)

    PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...

  2. PAT Advanced 1094 The Largest Generation (25) [BFS,DFS,树的遍历]

    题目 A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level ...

  3. PAT (Advanced Level) Practise - 1094. The Largest Generation (25)

    http://www.patest.cn/contests/pat-a-practise/1094 A family hierarchy is usually presented by a pedig ...

  4. 【PAT甲级】1094 The Largest Generation (25 分)(DFS)

    题意: 输入两个正整数N和M(N<100,M<N),表示结点数量和有孩子结点的结点数量,输出拥有结点最多的层的结点数量和层号(根节点为01,层数为1,层号向下递增). AAAAAccept ...

  5. 1094. The Largest Generation (25)

    A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...

  6. PAT (Advanced Level) 1094. The Largest Generation (25)

    简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  7. PAT练习——1094 The Largest Generation (25 point(s))

    题目如下: #include<iostream> #include<vector> #include<algorithm> using namespace std; ...

  8. PAT甲级——1094 The Largest Generation (树的遍历)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93311728 1094 The Largest Generati ...

  9. pat1094. The Largest Generation (25)

    1094. The Largest Generation (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

随机推荐

  1. Jenkins 角色 项目权限管理

    插件名称: Role-based Authorization Strategy 新建 两用户 配置项目安全策略  在系统管理页面点击Manage and Assign Roles进入角色管理页面: 进 ...

  2. November 06th, 2017 Week 45th Monday

    The education of a man is never completed until he dies. 一个人的学习之路,到死才结束. Being a life-long learning ...

  3. [ML学习笔记] XGBoost算法

    [ML学习笔记] XGBoost算法 回归树 决策树可用于分类和回归,分类的结果是离散值(类别),回归的结果是连续值(数值),但本质都是特征(feature)到结果/标签(label)之间的映射. 这 ...

  4. 026.6 网络编程 tomcat

    ###############Tomcat中相关文件作用    bin:启动关闭服务器的脚本    Conf:配置文件    Lib:Tomcat的jar包,只要部署项目到Tomcat,所有项目可共用 ...

  5. PyQt5--QProgressBar

    # -*- coding:utf-8 -*- ''' Created on Sep 20, 2018 @author: SaShuangYiBing Comment: ''' import sys f ...

  6. PyQt5--Position

    # -*- coding:utf-8 -*- ''' Created on Sep 13, 2018 @author: SaShuangYiBing ''' import sys from PyQt5 ...

  7. python第二十七课——os模块

    演示os模块中常用的属性和函数: 1.os模块: 作用:管理文件和目录 属性: os.name:返回系统类型 常用的windows系统 --> nt os.environ:返回当前系统所有的环境 ...

  8. OpenCV——Harr特征

  9. 写脚本时出现: Permission denied

    例如对文件 remove.sh sudo chmod -R 777 remove.sh

  10. Java UDP和TCP的区别

    为什么要写这篇博客:是这样的,最近听朋友说,有不少公司面试的时候会问道TCP和UDp的却别,所以就写出一篇简单的来描述他们之间的区别,送给那些即将面试的朋友们. UDP: 1.UDP, a.将数据以及 ...