PAT A 1004. Counting Leaves (30)【vector+dfs】
题目链接:https://www.patest.cn/contests/pat-a-practise/1004
大意:输出按层次输出每层无孩子结点的个数
思路:vector存储结点,dfs遍历
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=1e2+;
int n,m,k,x,f[maxn],deep,y;
vector<int> t[maxn];
void init()
{
cin>>n>>m;
while(m--)
{
cin>>x>>k;
while(k--)
{
cin>>y;
t[x].push_back(y);
}
}
}
void dfs(int x,int dep)
{
deep=max(dep,deep);
if(t[x].size()==)f[dep]++;
for(int i=;i<t[x].size();i++)
dfs(t[x][i],dep+);
} int main()
{
init();
dfs(,);
for(int i=;i<=deep;i++)
printf("%s%d",i?" ":"",f[i]);
printf("\n");
return ;
}
PAT A 1004. Counting Leaves (30)【vector+dfs】的更多相关文章
- 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 (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 ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 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 ...
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 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 Advanced Level】1004. Counting Leaves (30)
利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...
随机推荐
- ubuntu update dns server
edit: /etc/resolvconf/resolv.conf.d/base nameserver 114.114.114.114 execute this: $ resolvconf -u f ...
- 【GoLang】深入理解slice len cap什么算法? 参数传递有啥蹊跷?
先上结论 .内置append函数在现有数组的长度 < 时 cap 增长是翻倍的,再往上的增长率则是 1.25,至于为何后面会说. .Go语言中channel,slice,map这三种类型的实现机 ...
- Buzz words
给你一个字符串和字典,从头扫到位,如果到当前的字符的字符串存在于字典中,则显示 buzz. 例子: ILOVEPINEAPPLEJUICE 字典: [pine, apple, pineapple, j ...
- 【leetcode】Symmetric Tree
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- 23. javacript高级程序设计-数据存储
1. 数据存储 1.1 cookie HTTP Cookie, cookie,最初用来在客户端存储回话信息. (1). 限制,不同浏览器对特定域名下的cookie 的个数有限制,每个域名下最好不要操过 ...
- modelsim do文件仿真
1.编写sim.do文件 ##### Quit the Simulation ##### quit -sim ##### Make work directory && Create ...
- Effective C++ -----条款30:透彻了解inlining的里里外外
将大多数inlining限制在小型.被频繁调用的函数身上.这可使日后的调试过程和二进制升级(binary upgradability)更容易,也可使潜在的代码膨胀问题最小化,使程序的速度提升机会最大化 ...
- codeforces 556B. Case of Fake Numbers 解题报告
题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每 ...
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【python】入门学习(七)
设置字符串格式: format % values >>> x =/ >>> print(x) 0.012345679012345678 >>> p ...