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 ...
随机推荐
- selenium chrome登陆手机 pc淘宝
接口登录淘宝,困难度极高,没有人已经实现过. 淘宝登录selenium 手机版 pc版. 由于每天需要使用ip代理大批量的异地登录淘宝帐号,这种情况必然会出现淘宝滑动验证码,使用ActionChai ...
- MTK 时区修改
1.修改packages/apps/Settings/res/xml-xx-xx/timezones.xml (xx-xx表示不同的语言和区域),添加下面的内容: <!-- timezo ...
- 为什么要使用JS模板引擎
我之前在写一个输入联想控件的时候,改过好几个版本,每个版本不是因为性能不好就是因为代码凌乱而被推翻,最后用了understore模板引擎,效果有明显改善.整好这两天在研究互联网技术架构,发现很多的开发 ...
- 架构设计:系统存储(28)——分布式文件系统Ceph(挂载)
(接上文<架构设计:系统存储(27)--分布式文件系统Ceph(安装)>) 3. 连接到Ceph系统 3-1. 连接客户端 完毕Ceph文件系统的创建过程后.就能够让客户端连接过去. Ce ...
- C mysql (C API Commands out of sync; you can't run this command now)
错误出现在当一个用户使用查询,另一个用户再使用此sql连接进行查询的时候: 原因是因为上一次使用此sql连接进行查询时没有将所有的结果集给释放掉,在所有使用此sql连接进行查询的地方将所有的结果集给释 ...
- centos7修改root密码
1.重启系统,在下面界面时按e键 2.出现可编辑新内容,按向下键向下滑动,找到ro,并修改为rw 后,在LANG=en_US.UTF-8后面再加init=/bin/sh,结果如下图 3.然后按下ctr ...
- MongoDB(三)-- 执行JS、界面工具
一.执行Js脚本 1.开启mongod服务 2.连接mongodb客户端,./mongo --host 192.168.80.128 --port 27017 3.创建数据库:use testdb1 ...
- Splash 简介与安装
Splash 说白了就是一个轻量级的浏览器,利用它,我们同样可以实现跟其他浏览器一样的操作,我们使用 Docker 来安装 Splash: [root@localhost ~]# docker run ...
- ListView实现多种item布局的方法和注意事项
这篇文章的效果也是大家常见的,各种通讯应用的对话列表都是这种方式,像微信.whatsapp.易信.米聊等.我们这篇文章也权当为回忆,形成简单的笔记.这篇文章参考了2009年Google IO中的< ...
- WIN32编程经验总结
一 窗口和消息 1. 前缀: 2 WPARAM和LPARAM的意义在Windows是一种16位系统时,WndProc的第三个参数被定义为WORD,是一个16位的无符号整数,而第四个参数被定义为一个LO ...