序:
又是一道30分的题,仔细一看,这是一道层次遍历多叉树的问题,用queue应该可以搞定。
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
Input Specification:
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.
The input ends with N being 0. That case must NOT be processed.
Output Specification:
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
题目概述:
给出一棵树的节点数,以及所有爸爸节点及其的儿子节点。求每一层中的叶子节点。
分析:
1.找根,根节点是唯一一个不在儿子节点的节点,用一个数组进行0,1记录
2.存储,这里我用了一个map,把每个爸爸和对应的儿子序列对应
3.遍历,用queue遍历,一层层遍历。若遇到节点无儿子,cnt++;否则,将其儿子push进queue,等待下一层遍历
代码如下:
//层次遍历&队列
//找根
#include<bits/stdc++.h>
using namespace std;int visit[110];
unordered_map<int,vector<int>> mp;int main()
{int n, m;scanf("%d%d", &n, &m);int father, k, temp;for(int i = 0; i < m; i++){scanf("%d%d", &father, &k);for(int j = 0; j < k; j++){scanf("%d", &temp);visit[temp] = 1;mp[father].push_back(temp);}}int root = 1;while(visit[root] != 0) root++;queue<int> qq;qq.push(root);int floor = 1;while(!qq.empty()){int len = qq.size();int cnt = 0, ft;for(int i = 0; i < len; i++){ft = qq.front();qq.pop();if(mp[ft].size() == 0) cnt++;else{for(int j = 0; j < mp[ft].size(); j++)qq.push(mp[ft][j]);}}if(floor == 1) printf("%d", cnt);else printf(" %d", cnt);floor++;}printf("n");return 0;
}
总结:
1.queue中的front()是取队首元素,pop()踢掉队首元素
2.用floor作为表示,区分输出的空格
3.root应该从1开始遍历,因为题目中的节点序号对应
4.题目中说01固定为根,但我们给出的是通解。我们的解法可以适用不定根的情况。
2022.01.08 简单bfs就好 以前花里胡哨的
//bfs罢了
#include<bits/stdc++.h>
using namespace std;vector<vector<int>> v;int main() {int n, m;cin >> n >> size(n + 1);for(int i = 0; i < m; i++) {int id, k;cin >> id >> k;for(int j = 0; j < k; j++) {int child;cin >> child;v[id].push_back(child);}}//开始bfsint level = 0;queue<int> q;q.push(1);int flag = 0;while(!q.empty()) {int countLeaves = 0;int len = q.size();for(int i = 0; i < len; i++) {int now = q.front();q.pop();if(v[now].size() == 0) ++countLeaves;else {for(int j = 0; j < v[now].size(); j++) q.push(v[now][j]);}}if(flag == 0) {++flag;cout << countLeaves;}else {cout << " " << countLeaves;}}return 0;
}
本文发布于:2024-01-31 17:36:41,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170669380230231.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |