PAT1004. Counting Leaves (30) dfs和bfs
2018-03-16 17:06
369 查看
题目 PAT1004. Counting Leaves (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), 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. Output 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 1 leaf node. Then we should output "0 1" in a line. Sample Input 2 1 01 1 02 Sample Output 0 1
题目要求
给你一棵树(不一定是二叉树),输出该树的叶子数。题目分析
首先我们要理解在计算机科学中叶子结点的定义,一棵树当中没有子结点(即度为0)的结点称为叶子结点,简称“叶子”。 叶子是指度为0的结点,又称为终端结点。既然是寻找叶子结点,便可以用dfs(深度优先遍历)或bfs(广度优先遍历)解决。如果对dfs和bfs有些生疏,可以点击这里。dfs(深度优先遍历)
思路定义一个二维数组存储树的结点,数组的第一列为之后给定的M行的第一个结点,该节之后跟的是它的子节点,如输入
6 2 01 3 02 03 04 02 2 05 06
数组为
01 02 03 04 00 ... 02 05 06 00 00 ... 00 00 00 00 00 ... ...
可以理解为数组内第一列全部为非叶子结点,该行第一个元素之后的元素全部为它的子结点,这里我们还需要统计非叶子结点的子结点数目,定义size
数组,其中第i个结点
if(size[i] == 0) this node is leaf-node;
定义一个数组book
,记录每层叶子结点的数目,其中第depth(树的层数)层
if(size[i] == 0) book[depth]++;
在这里使用的递归算法中的边界条件是遍历到叶子结点,每次递归都遍历当前层数的下一层。
完整代码
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <cstring> #include <map> using namespace std; int size[100] = {0,}; //record the number of child in a node int book[100],maxdepth = -1; int t[100][100]; void dfs(int index,int depth){ if(size[index] == 0){ //this node is leaf book[depth]++; //leaves plus one at this depth maxdepth = max(depth,maxdepth); //update maxdepth return ; } for(int i = 0;i < size[index];i++){ //not leaf dfs(t[index][i],depth+1); //recursive } } int main(int argc, char *argv[]) { int n,m,k,f,c; //freopen("Input.txt","r",stdin); //Comment this line of code when submit cin >> n >> m; for(int i = 0;i < m;i++){ cin >> f >> k; //f stands for index size[f] = k; //k children for(int j = 0;j < k;j++){ cin >> c; t[f][j] = c; //f-th row stores it's children } } //the root ID is 01 dfs(1,0); cout << book[0]; for(int i = 1;i <= maxdepth;i++) cout << " " << book[i]; return 0; }
bfs(广度优先遍历)
思路广度优先遍历在进一步遍历图中顶点之前,先访问当前顶点的所有邻接结点。定义一个数组level,用来保存结点的层数
level[child-node] = level[parent-node] + 1 if(size[i] == 0) book[level[i]]++;
完整代码
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <queue> #include <stack> #include <algorithm> #include <vector> using namespace std; vector<int> v[100]; int book[100]; //record the number of leaves int maxdepth = -1; int level[100],maxlevel = -1; void bfs(){ queue<int> q; q.push(1); //start at 1 level[1] = 0; while(!q.empty()){ int index = q.front(); q.pop(); maxlevel = max(level[index],maxlevel); //update maxlevel if(v[index].size() == 0){ book[level[index]]++; } for(int i = 0;i < v[index].size();i++){ q.push(v[index][i]); level[v[index][i]] = level[index] + 1; //level of child plus 1 } } } int main(int argc, char *argv[]) { int n,m,k,node,c; // 96bc freopen("Input.txt","r",stdin); cin >> n >> m; for(int i = 0;i < m;i++){ cin >> node >> k; for(int j = 0;j < k;j++){ cin >> c; v[node].push_back(c); } } bfs(); cout << book[0]; for(int i = 1;i <= maxlevel;i++){ cout << " " << book[i]; } return 0; }
参考
https://www.liuchuo.net/archives/2229相关文章推荐
- 1004. Counting Leaves (30) 树的遍历 DFS BFS
- 1127. ZigZagging on a Tree (30)[二叉树构建-dfs-bfs]
- PAT (Advanced Level) 1091. Acute Stroke (30) 3维DFS(10M数据时栈溢出)、3维BFS(AC)
- 1107. Social Clusters (30) -- 简单 DFS / BFS
- 1004. Counting Leaves (30)(*****dfs/bfs)
- 2007-2008 Winter Petrozavodsk Camp, Andrew Stankevich Contest 30-E - New Mayors - 二分图染色 (BFS+DFS)
- 1004. Counting Leaves (30)-PAT甲级真题(bfs,dfs,树的遍历,层序遍历)
- 1076. Forwards on Weibo (30)--和层数有关的最好用bfs,用dfs容易不然出现短路或者环的情况
- 1091. Acute Stroke (30) BFS or DFS
- HDU 1175 连连看(dfs,bfs)
- poj Find The Multiple(DFS,BFS)
- poj 3083 Children of the Candy Corn (dfs+bfs)
- bfs及dfs模版
- POJ1426 Find The Multiple (BFS | DFS)
- 图的遍历(DFS和BFS)
- hdu Nightmare(BFS||DFS)(不标记的剪枝)
- BFS,DFS
- 算法学习笔记 二叉树和图遍历—深搜 DFS 与广搜 BFS
- Destroying the bus stations (dfs+bfs)
- poj Children of the Candy Corn(BFS+DFS)