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 ...
随机推荐
- 关于vue中的nextTick深入理解
定义[nextTick.事件循环] nextTick的由来: 由于VUE的数据驱动视图更新,是异步的,即修改数据的当下,视图不会立刻更新,而是等同一事件循环中的所有数据变化完成之后,再统一进行视图更新 ...
- 前端JavaScript之DOM使用案例
1.弹出框点击关闭 (这个例子关键自己创建标签以及属性,不是太好想啊,而且作用相对来数也不是太大) <!DOCTYPE html> <html> <head> &l ...
- 史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)
转载请标明出处: https://www.fangzhipeng.com/springcloud/2017/07/12/sc03-feign/ 本文出自方志朋的博客 最新Finchley版本请访问: ...
- java面试题:已知一个数组[2,4,6,2,1,5],将该数组进行排序(降序,不能用工具类进行排序),创建两条线程交替输出排序后的数组,线程名自定义
package com.swift; import java.util.Arrays; import java.util.Comparator; public class ArrayThread_Te ...
- python代码实现截图识字
有次网上down了个截图实识的软件,发现一天只能用10次要钱才能解锁免费,很气,作为一个菜鸡怎么能忍受 于是自己用python写了个简单的小工具,分享给大家 代码如下: 先安装库 from PIL i ...
- 7-1 python 操作redis
1.安装并导入redis模块 # pip install redis 安装redis模块 import redis # 导入redis模块 2.连接一个或多个redis,指定数据库名,并指定返回字符串 ...
- oracle之bitmap索引
oracle常见的索引是BTree索引和Bitmap索引. BTree索引特点: 默认索引 适合大量增删改查 不能用or操作符 适合高基数的列(即唯一值多) 创建sql:create index li ...
- css页面出现滚动条左右跳动
问题描述:页面首次加载的时候内容是一屏,不会出现纵向滚动条,但是当点击加载的内容更多时页面会出现左右跳动一下的情况.出现跳动的主要原因就是出现了滚动条,滚动条的宽度影响了内容区域的宽度. 1.查看导航 ...
- Windows下安装Python数据库模块--MySQLdb
## 1.下载MySQLdb [去官网](http://pypi.python.org/pypi/MySQL-python/) 下载对应的编译好的版本(现在官网最新版本为1.2.5): MySQL-p ...
- PHP的IMAP函数
imap_8bit -转换的8位字符串的引用,打印字符串 imap_alerts -返回所有的I MAP邮件警报已经发生 imap_append -附加了一系列的信息到指定邮箱 imap_base64 ...