PAT甲1004 Counting Leaves【dfs】
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】的更多相关文章
- PAT Advanced 1004 Counting Leaves
题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...
- PAT A 1004. Counting Leaves (30)【vector+dfs】
题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...
- 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 ...
- PAT 甲级 1004 Counting Leaves
https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...
- PAT甲级 1004.Counting Leaves
参考:https://blog.csdn.net/qq278672818/article/details/54915636 首先贴上我一开始的部分正确代码: #include<bits/stdc ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 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 ...
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
随机推荐
- linux中高亮显示文本的工具 -- bat
bat 的项目地址 https://github.com/sharkdp/bat bat 是用rust 开发的, 在centos中安装bat需要rust的环境, 我们可以通过安装rust的包管理工具c ...
- Android开发真机测试方法 (转)
转自:http://blog.csdn.net/china_lzn/article/details/7461963 Android的AVD无比的慢,无语啊,程序编的再好,都无法体验到流畅的感觉,索性, ...
- 用XYNTService把Python程序变为服务
1. XYNTService的使用 1.1. 介绍 1.2. XYNTService 2. 用XYNTService把Python程序变为服务 1. XYNTService的使用 1.1. 介绍 通常 ...
- C 修改命令行文本颜色
#include <Windows.h> #include <stdio.h> int main() { HANDLE h = GetStdHandle(STD_OUTPUT_ ...
- iOS try catch
最近看一些第三方的代码有@try,一副看不懂的样子,真心没用过,于是查了些资料收集在这里,以后遇到就不会再蒙比了.其实这东西确实不怎么用,下文有解释.Objective-C 异常机制 :-- 作用 : ...
- 如何查看MySQL的当前存储引擎?
如何查看MySQL的当前存储引擎? 一般情况下,mysql会默认提供多种存储引擎,你可以通过下面的查看: 看你的mysql现在已提供什么存储引擎: mysql> show engines; ...
- Kubernetes 相关概念
Node: (1) Node(节点)也就是宿主机,宿主机可以是物理机 .云主机 .虚拟机等等,我们可以在一个宿主机上跑多个容器(container)(2) Node 上会被分配一些工作负载(即 doc ...
- popupWindow 在指定位置上的显示
先看效果图,免得浪费大家时间,看是不是想要的效果 . 直接上代码 ,核心方法. private void showPopupWindow(View parent) { if (popupWindow ...
- J2EE学习篇之--Struts1详解
今天来看一下Struts1的相关知识,其实Struts现在是出名的,每个Web开发者都会知道的,也是现在比较流行的框架,下面就来看一下我们为什么要用Struts框架呢? 摘要 1.建立在mvc这种好的 ...
- 【转载】经典.net面试题目【为了笔试。。。。。】
. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有成员, 在类的内部才可以访问. protected : 保护成员 ...