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. javascript的特殊条件语句

    摘要: ​ 由于javascript语言的特殊性导致它有很多特殊的条件判断,下面我列出了一些特殊的条件判断语句和他们对应的结果. if(condition) { console.log(true); ...

  2. AWS系列-创建 IAM 用户

    创建 IAM 用户(控制台) 官方文档 https://docs.aws.amazon.com/zh_cn/IAM/latest/UserGuide/introduction.html 通过 AWS ...

  3. NetBpm 数据库(9)

    原文:http://blog.csdn.net/adicovofer/article/details/1718592 关注NetBpm也很久了,可是一直没有静下心来研究,为了生活的琐事,太过浮躁……今 ...

  4. 升级MAC OX上的Python到3.4

    第1步:下载Python3.4 下载地址如下: 下载Mac OS X 64-bit/32-bit installerhttps://www.python.org/downloads/release/p ...

  5. flexbox子盒子align-self属性

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. lua 注释

    1. 单行注释 --  功能等同于C++中的// 2. 多行注释  --[[  注释的内容  ]]   功能等同于C++中的 /**/ 3. 多行注释   --[====[   注释和内容  ]=== ...

  7. {"errorCode":50} 的解决办法

    # 无反爬 import urllib.parse import urllib.request import json content = input('请输入需要翻译的词语:') # url = ' ...

  8. linux给当前用户添加环境变量

    比如当前用户为oracel,则添加环境变量操作为: vim  /home/oracel/.bashrc

  9. 【RF库Collections测试】Remove From List

    Name:Remove From ListSource:Collections <test library>Arguments:[ list_ | index ]Removes and r ...

  10. HttpClient(一)-- HelloWorld

    一.简介 HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的 ...