1004 Counting Leaves (30 分)

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

题意:

给定一棵树和节点之间的关系。要求统计每一层的叶子节点个数。

思路:

就建树,暴力dfs就好了。maxn=105的时候WA和RE了,改成了1005就过了。

 #include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f const int maxn = ;
int n, m;
struct node{
int v, nxt;
}edge[maxn];
int head[maxn], tot = ;
int cnt[maxn], dep = -; void addedge(int u, int v)
{
edge[tot].v = v;
edge[tot].nxt = head[u];
head[u] = tot++;
edge[tot].v = u;
edge[tot].nxt = head[v];
head[v] = tot++;
} void dfs(int rt, int fa, int h)
{
int sum = ;
dep = max(dep, h);
for(int i = head[rt]; i != -; i = edge[i].nxt){
if(edge[i].v == fa)continue;
sum++;
dfs(edge[i].v, rt, h + );
}
if(sum == ){
cnt[h]++;
} } int main()
{
scanf("%d%d", &n, &m);
memset(head, -, sizeof(head));
for(int i = ; i < m; i++){
int u, k;
scanf("%d %d", &u, &k);
for(int j = ; j < k; j++){
int v;
scanf("%d", &v);
addedge(u, v);
}
} dfs(, -, );
//cout<<dep<<endl;
printf("%d", cnt[]);
for(int i = ; i <= dep; i++){
printf(" %d", cnt[i]);
}
printf("\n");
return ;
}

PAT甲1004 Counting Leaves【dfs】的更多相关文章

  1. PAT Advanced 1004 Counting Leaves

    题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...

  2. PAT A 1004. Counting Leaves (30)【vector+dfs】

    题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...

  3. PAT Advanced 1004 Counting Leaves (30) [BFS,DFS,树的层序遍历]

    题目 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family mem ...

  4. PAT 甲级 1004 Counting Leaves

    https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...

  5. PAT甲级 1004.Counting Leaves

    参考:https://blog.csdn.net/qq278672818/article/details/54915636 首先贴上我一开始的部分正确代码: #include<bits/stdc ...

  6. PAT 解题报告 1004. Counting Leaves (30)

    1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  7. PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)

    1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...

  8. PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  9. 1004 Counting Leaves (30分) DFS

    1004 Counting Leaves (30分)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

随机推荐

  1. 关于 wsdl2Java 自动生成客户端调取webservice接口

    webservice地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl wsdl2Java 自动生成类名: 客户端调 ...

  2. 【scala】 scala 基础(一)

    至于什么是scala,摘录一段 维基百科的解释: scala 下载 安装 省略 1.环境变量配置完成后 命令行报错,因为scala 的安装路径里边包含空格 修改后即可.由于我的本地包含空格,此处CLI ...

  3. android studio 导入第三方库的记录

    android studio 导入第三方库的记录.jar包 和 库 一.jar包 1.jar包的话很简单,首先换成project模式,将你要用的jar包复制到lib下面.如图 2.然后右键选择Add ...

  4. 手机APP支付--整合支付宝支付控件

    长话短说,本文根据支付宝官方说明文档,简单总结下,并且说明下开发过程碰到的问题以及该如何解决. 整合步骤: 1 登录商家服务网站,下载开发包,地址:https://b.alipay.com/order ...

  5. SpringMVC由浅入深day01_1springmvc框架介绍

    springmvc 第一天 springmvc的基础知识 课程安排: 第一天:springmvc的基础知识 什么是springmvc? springmvc框架原理(掌握) 前端控制器.处理器映射器.处 ...

  6. logback -- 配置详解 -- 一 -- <configuration>及子节点

    附: logback.xml实例 logback -- 配置详解 -- 一 -- <configuration>及子节点 logback -- 配置详解 -- 二 -- <appen ...

  7. Web实时通信之Socket.IO

    前面两篇文章使用了Ajax long polling和WebSocket两种常用的Web实时通信方式构建了简单的聊天程序. 但是,由于浏览器的兼容问题,不是所有的环境都可以使用WebSocket这种比 ...

  8. ios开发之--UITableView中的visibleCells的用法

    先上图: 具体代码如下: #import "ViewController.h" @interface ViewController ()<UITableViewDelegat ...

  9. mysql 行转列 (case when的使用)

    现有 table1 如下 table2 如下 需要统计:各个部门每个月的绩效总和 实现如下:

  10. 使用 Beautiful Soup

    Beautiful Soup 用法: (1) 前面我们爬取一个网页,都是使用正则表达式来提取想要的信息,但是这种方式比较复杂,一旦有一个地方写错,就匹配不出来了,因此我们可以使用 Beautiful ...