PAT (Advanced Level) 1004 Counting Leaves(树的遍历)
题目链接:点击查看
题目大意:给出一个树状家谱,问每一代没有后代的节点个数
题目分析:其实就是个简单的树的遍历,奈何读不懂题。。遍历到每一层然后记录没有后代的个数即可,我是习惯性的建了无向图,遍历的时候没有后代的条件是度为1并且前置父节点不是-1,然后需要对根节点特判一下,特判一下是否只有一个根节点,两种代码,都写来练练手:
DFS:
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<unordered_map>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=110;int ans[N];int mmax=-1;vector<int>node[N];void dfs(int u,int fa,int step)
{if(node[u].size()==1&&fa!=-1||node[u].size()==0&&fa==-1){ans[step]++;mmax=max(mmax,step);return;}for(int i=0;i<node[u].size();i++){int v=node[u][i];if(v==fa)continue;dfs(v,u,step+1);}
}int main()
{
// freopen("input.txt","r",stdin);int n,m;scanf("%d%d",&n,&m);while(m--){int u,v,num;scanf("%d%d",&u,&num);while(num--){scanf("%d",&v);node[u].push_back(v);node[v].push_back(u);}}dfs(1,-1,0);cout<<ans[0];for(int i=1;i<=mmax;i++)cout<<' '<<ans[i];return 0;
}
BFS:
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<unordered_map>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=110;int ans[N];int mmax=-1;vector<int>node[N];struct Node
{int to,step,fa;Node(int TO,int FA,int STEP){to=TO;fa=FA;step=STEP;}
};void bfs()
{queue<Node>q;q.push(Node(1,-1,0));while(!q.empty()){Node cur=q.front();q.pop();int u=cur.to;int fa=cur.fa;int step=cur.step;if(node[u].size()==1&&fa!=-1||node[u].size()==0&&fa==-1){ans[step]++;mmax=max(mmax,step);continue;}for(int i=0;i<node[u].size();i++){int v=node[u][i];if(v==fa)continue;q.push(Node(v,u,step+1));}}
}int main()
{
// freopen("input.txt","r",stdin);int n,m;scanf("%d%d",&n,&m);while(m--){int u,v,num;scanf("%d%d",&u,&num);while(num--){scanf("%d",&v);node[u].push_back(v);node[v].push_back(u);}}bfs();cout<<ans[0];for(int i=1;i<=mmax;i++)cout<<' '<<ans[i];return 0;
}
PAT (Advanced Level) 1004 Counting Leaves(树的遍历)相关推荐
- PAT甲级1004 Counting Leaves (30分):[C++题解]树、邻接表存储树、dfs遍历树
文章目录 题目分析 题目链接 题目分析 题意重述:一棵树,求每一层的叶子节点数目. 分析 构造树,使用邻接表来存(相当于存储有向图). 需要一个头结点数组h[N],然后每个头节点往外形成一个单链表e[ ...
- PAT (Advanced Level) Practice 题目集合(1001 ~ 1050)(正在更新)
1001 A+B Format (20 分) 题目大意:计算a+b,结果按照西方的那种写数字的方式输出,从三个数一个逗号那种. #include<bits/stdc++.h> using ...
- PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642
PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642 题目描述: A Binary Search Tr ...
- PAT (Advanced Level) Practice 题解代码 - II (1051-1100)
PAT PAT (Advanced Level) Practice - II(1051-1100) -------------------------------------------------- ...
- PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642
PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642 题目描述: With the 2010 FIFA World Cu ...
- PAT (Advanced Level) Practise 1004 解题报告
GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 1600 ...
- 1004 Counting Leaves (30 分)【难度: 中 / 知识点: 树的遍历】
题目意思: 求每一层的叶子结点数 方法一: 深搜 数组模拟存储邻接表 #include<bits/stdc++.h> using namespace std; const int N=1e ...
- PAT (Advanced Level) Practice A1090 Highest Price in Supply Chain (25 分)(C++)(甲级)(树,深度)
原题链接:A1090 Highest Price in Supply Chain #include<algorithm> #include<iostream> #include ...
- PAT (Advanced Level) 1151——前序中序构造树+LCA+map ( Ps.数组开大点啊 )
题目传送门 终于遇到了LCA题,先复习一下LCA模板 题目中提到的key值取值范围在int之内,不能直接视为结点编号 我将preorder的index作为结点编号,即认为结点的前序遍历为1-n 同时使 ...
最新文章
- gradle 指定springcloud 版本_SpringCloud微服务架构开发实战:实现服务注册与发现
- 安装llvmlite报错解决:RuntimeError: llvm-config failed executing, please point LLVM_CONFIG to the path for
- web开发工程师出路
- 怎样将包含元组的列表转换为字典?
- Python必备知识点:对Json的基本使用方法
- Gartner:容器采用将迅速增长,但不会很快有利可图
- 计算机应用计算机电算化题库,2014年浙江省会计电算化客观题题库
- 大量CV职位!奥比中光2020届校招提前批内推启动!
- 花呗:已有5700万人正在使用花呗账单助手功能
- 46、练习:输出指定目录下的所有文件名称
- preHandle、postHandle与afterCompletion
- vs2010运行c++程序时,控制台一闪而过的解决方案
- 这是不是微软MSN的一个Bug呢?
- 一个苹果证书怎么多次使用——导出p12文件
- Python第三方库turtle的应用
- 大数据学习(三十一)数据仓库如何处理缓慢变化维
- 张萌韩墨羽——打包升级兼容适配
- 使用线性神经网络实现逻辑与和逻辑异或(一)
- 小程序支付报错:向微信请求统一下单失败:商户号该产品权限未开通,请前往商户平台>产品中心检查后重试
- Java的编程之旅——idea上新建文件