PAT 甲级 1004 Counting Leaves
https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184
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, the number of nodes in a tree, and M (<), 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 1leaf node. Then we should output 0 1 in a line.
Sample Input:
2 1
01 1 02
Sample Output:
0 1
代码:
#include <bits/stdc++.h>
using namespace std; int N, M;
vector<int> v[110];
int vis[110];
int ans[110];
int deep = INT_MIN; void dfs(int st, int depth) {
if(v[st].size() == 0) {
ans[depth] ++;
deep = max(deep, depth);
} for(int i = 0; i < v[st].size(); i ++)
dfs(v[st][i], depth + 1);
} int main() {
scanf("%d%d", &N, &M);
memset(vis, 0, sizeof(vis));
while(M --) {
int p, k;
scanf("%d%d", &p, &k);
for(int i = 0; i < k; i ++) {
int c;
scanf("%d", &c);
vis[c] = 1;
v[p].push_back(c);
}
} dfs(1, 0);
printf("%d", ans[0]);
for(int i = 1; i <= deep; i ++)
printf(" %d", ans[i]);
return 0;
}
dfs
PAT 甲级 1004 Counting Leaves的更多相关文章
- PAT甲级 1004.Counting Leaves
参考:https://blog.csdn.net/qq278672818/article/details/54915636 首先贴上我一开始的部分正确代码: #include<bits/stdc ...
- PAT甲1004 Counting Leaves【dfs】
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 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甲级——A1004 Counting Leaves
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
- 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 ...
- 1004 Counting Leaves ——PAT甲级真题
1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
随机推荐
- wp apps
WP apps: 明星脸 用于 Windows 10 的 URL https://www.microsoft.com/store/apps/9nblggh5kq63 用于 Windows Phone ...
- .Net Core 管道机制
开篇先上一张中间件原理图,帮助大家对管道机制形成一个直观的认识 下面我们实现一个简单的管道机制,以此为例深入理解管道机制的原理 1. 首先定义一个委托,该委托接收一个上下文对象,返回值为Task,代码 ...
- AtCoder ExaWizards 2019 D Modulo Operations
题意 给出一个长度为\(n\)的数列和数字\(X\),对于数列的每一种排列,其权值\(X\)依次对排列中的数取模,求出\(n!\)种情况最后剩下的数的权值和 分析 如果大的数字排在小的数字后面,那么大 ...
- cogs791 [HAOI2012] 音量调节
大水题 这种题谁不能1A谁就吔屎/退役吧(说的就是我) 设dp[i][j]表示调完前i个音量为j有没有可能 没了 // It is made by XZZ // Fei Fan Ya Xi Lie~~ ...
- 总结hibernate框架的常用检索方式
1.hibernate框架的检索方式有以下几种: OID检索:根据唯一标识OID检索数据 对象导航检索:根据某个对象导航查询与该对象关联的对象数据 HQL检索:通过query接口对象查询 QBC检索: ...
- Git工具使用小结
昨天开始看一套java接口自动化的视频,今天看到的一章是关于git这个工具使用的,上大学那会用过svn作为版本管理工具,包括现在所在的公司,用的也还是svn进行管理.其实老早就听闻过Git,Githu ...
- flask中的宏
对于flask中的宏编程.我们使用 macro 来对宏起个名称 宏编程 对于我们来说是减少了代码的重用.以及简化了标签的操作,对与开发效率有很大的提升, 在html中.相信大多数都用到了.input ...
- Maven仓库 - 分发构件至远程仓库
分发构件至远程仓库 mvn install 会将项目生成的构件安装到本地Maven仓库,mvn deploy 用来将项目生成的构件分发到远程Maven仓库.本地Maven仓库的构件只能供当前用户使 ...
- SSL详解
SSL 1.整体结构 SSL是一个介于HTTP协议与TCP之间的一个可选层,其位置大致如下 SSL:(Secure Socket Layer,安全套接字层),为Netscape所研发,用以保障在Int ...
- 六、Django之表单和类视图-Part 4
一.表单form 为了接收用户的投票选择,我们需要在前端页面显示一个投票界面.让我们重写先前的polls/detail.html文件,代码如下: <h1>{{ question.quest ...