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 ...
随机推荐
- Smallest Difference(暴力全排列)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10387 Accepted: 2 ...
- 【Oracle】强制关闭会话
select sid, serial# from V$session where sid in (select sid from v$LOCK where TYPE in ('TM','TX')); ...
- Linux+Redis实战教程_day01_Linux介绍与安装
1.Linux介绍(了解) 1.1.Linux和Windows的区别 Linux是一款操作系统.正规开发 服务器项目部署都是放在Linux操作系统上. Windows一款操作系统,民用操作系统.娱乐. ...
- 修改Tomcat的默认访问目录
放在外网的应用,用户多是直接输入域名访问,而Tomcat的默认目录是ROOT,所以我们需要更改其默认目录. 更改Tomcat的默认目录很简单,只需要修改server.xml就可以了. 具体是是在< ...
- Android图片的合成示例
package com.example.demo; import android.os.Bundle; import android.app.Activity; import android.grap ...
- x264编码的图像出现乱码的问题
将YUV进行x264编码的时候,建议将 i_threads 参数设置成 X264_SYNC_LOOKAHEAD_AUTO//* 取空缓冲区继续使用不死锁的保证. 否则有可能编码出来的数据会出现IDR_ ...
- iOS开发-VFL初窥
VFL是苹果为了简化Autolayout的编码而推出的抽象语言,在上一篇博客中我们发现如果使用NSLayoutConstraint来添加约束是非常繁琐的. 一个简单的Frame需要添加四个NSLayo ...
- 基于Elasticsearch 5.4.3的商品搜索系统
源码已提交至http://github.com
- 关于vc工程包含多个lib库老是提示无法打开问题
在一个VC项目中,我要包含五个lib库,我在连接器->常规->附加库目录中输入了正确的库包含路径,然后再连接器->输入->附加依赖项中输入:ws2_32.lib;wsock32 ...
- Malab 常用数学函数
l 三角函数和双曲函数 名称 含义 名称 含义 名称 含义 sin 正弦 csc 余割 atanh 反双曲正切 cos 余弦 asec 反正割 acoth 反双曲余切 tan 正切 ac ...