1004 Counting Leaves (30 分)(树的遍历)
给出一棵树,问每一层各有多少叶子节点
dfs遍历树
#include<bits/stdc++.h> using namespace std;
vector<int>p[];
int n,m;
int node ,k;
int vis[];
int maxn=-;
void dfs(int node,int step)
{
if(p[node].empty()){
vis[step]++;
maxn=max(step,maxn);
}
else{
for(int i=;i<p[node].size();i++){
dfs(p[node][i],step+);
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=;i<m;i++){
cin>>node>>k;
int t;
for(int j=;j<k;j++){
cin>>t;
p[node].push_back(t);
}
}
dfs(,);
cout<<vis[];
for(int i=;i<=maxn;i++){
cout<<" "<<vis[i];
}
cout<<endl;
return ;
}
bfs遍历求树
#include<bits/stdc++.h> using namespace std;
struct NODE
{
int data;
int step;
NODE(){}
NODE(int data1,int step1):data(data1),step(step1){}
};
vector<int>p[];
int n,m;
int node ,k;
int vis[];
int maxn=-;
void bfs(int node,int step)
{
queue<NODE>Q;
Q.push(NODE(node,step));
while(!Q.empty()){
NODE u=Q.front();
Q.pop();
if(p[u.data].empty()){
maxn=max(u.step,maxn);
vis[u.step]++;
}
for(int i=;i<p[u.data].size();i++){
Q.push(NODE(p[u.data][i],u.step+));
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=;i<m;i++){
cin>>node>>k;
int t;
for(int j=;j<k;j++){
cin>>t;
p[node].push_back(t);
}
}
bfs(,);
cout<<vis[];
for(int i=;i<=maxn;i++){
cout<<" "<<vis[i];
}
cout<<endl;
return ;
}
1004 Counting Leaves (30 分)(树的遍历)的更多相关文章
- 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 (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 【PAT甲级】1004 Counting Leaves (30 分)(BFS)
题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100 ...
- 1004 Counting Leaves (30 分)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
- 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)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 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)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family membe ...
- PAT A 1004. Counting Leaves (30)【vector+dfs】
题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...
随机推荐
- Centos 5.5 编译安装mysql 5.5.9
下载mysql wget http://mysql.mirrors.pair.com/Downloads/MySQL-5.5/mysql-5.5.9.tar.gz 创建mysql用户 [root@x ...
- 八数码(map版)
八数码 map真是个奇技淫巧好东西 可以十分简单的实现hash,当然速度就不敢保证了 因为九位数不算很大,完全可以用int存下,所以便将八数码的图像转换成一个int型的数字 #include<i ...
- c++ 11 线程池的简单封装
#include <condition_variable> #include <queue> #include <thread> #include <vect ...
- LeetCode705. Design HashSet
题目 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中是否存在这个值. ...
- rsync+lsyncd实现实时同步
1.接收端安装rsync,修改/etc/rsyncd.conf配置文件,然后启动服务. uid = rootgid = rootuse chroot = nomax connection = 4sec ...
- Go单元测试与基准测试
Go单元测试 Go单元测试框架,遵循规则整理如下: 1.文件命名规则: 含有单元测试代码的go文件必须以_test.go结尾,Go语言测试工具只认符合这个规则的文件 单元测试文件名_test.go前面 ...
- angular常见问题总结
本文引自:https://www.cnblogs.com/zhoulujun/p/8881414.html 这篇是对angularJS的一些疑点回顾,是对目前angularJS开发的各种常见问题的整理 ...
- Linux(CentOS6.5 x64)下版本安装及升级kangle+EasyPanel
说明:(easypanel集成了kangle web 服务器和mysql,仅支持centos 5和centos 6) .执行下面的命令即可,安装程序将自动安装或者升级: yum -y install ...
- python3 练习题100例 (十六)鸡尾酒疗法
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'Fan Lijun' n = input('请输入一个大于1,小于等于20的整 ...
- 2、spring boot 配置文件
配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: •application.properties •application.yml 配置文件的作用:修改SpringBoot自 ...