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

#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. MySQL基础之 统计函数总结

    五种统计函数:count().max().avg().min().max()函数 count()函数 count()函数在进行计算的时候,是分情况进行计算的,主要是一下两种 1.采用count(*)对 ...

  2. 加载驱动三种execute

    executeQuery executeUpdate executeQueryBatch

  3. LVM操作

    创建LVM,并挂载 1.对磁盘进行分区fdisk /dev/sdb [root@testdb ~]# fdisk /dev/sdbDevice contains neither a valid DOS ...

  4. libco协程库上下文切换原理详解

    缘起 libco 协程库在单个线程中实现了多个协程的创建和切换.按照我们通常的编程思路,单个线程中的程序执行流程通常是顺序的,调用函数同样也是 “调用——返回”,每次都是从函数的入口处开始执行.而li ...

  5. BZOJ3673/3674:可持久化并查集

    Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...

  6. jQuery 使用ajax,并刷新页面

    <script> function del_product_information(id) { $.ajax({ url: "{% url 'del_product_inform ...

  7. C#中使用WeiFenLuo.WinFormsUI.Docking.dll实现窗口停靠效果

    很酷的效果,很值得好好去学习的哈. 重置工具箱: 新建一个WinForm程序,项目名称为TestDockPanelControl.选中Form1窗体后选择工具箱--->>新建个添加选项卡命 ...

  8. multi-voltage design apr

    在先进制程中,为了降低芯片功耗,经常会采用 muti-voltage design,在一颗芯片内部划分出多个 power domain,不同 domain 采用不同的电压,有时候还会将其中某些 pow ...

  9. char a='1'和char a=1区别

    char a='1'表示:把字符为1,ASSIC码为49的值赋值给a: char a= 1表示:把ASSIC码为1的值赋值给a

  10. SQL 字符串分割表函数

    --字符串分割表函数 ) ) declare @i int; declare @count int; ); ); declare @Index int; )) declare @rowID int; ...