PAT-A-1004. Counting Leaves (30)
来源:互联网 发布:c语言实现面向对象 编辑:程序博客网 时间:2024/06/06 14:22
1004. Counting Leaves (30)
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 Input2 101 1 02Sample Output
0 1
#include<iostream>#include<vector>#include<cstdio>#include<stdio.h>using namespace std;int n, m;const int maxn = 110;struct node{ vector<int> child;}Node[maxn];void BFS(int root){ int q[maxn]; int front = -1, rear = -1; int last = 0; q[++rear] = root; int flag = 1; int p; int lef = 0; while (front < rear) { p = q[++front]; if (Node[p].child.size() == 0) lef++; for (int i = 0; i < Node[p].child.size(); i++) q[++rear] = Node[p].child[i]; if (last == front) { last = rear; if (flag) { cout << lef; flag = 0; } else cout << " " << lef; lef = 0; } }}int main(){ cin >> n >> m; int t, num,son; for (int i = 0; i < m; i++) { cin >> t; cin >> num; for (int j = 0; j < num; j++) { cin >> son; Node[t].child.push_back(son); } } BFS(1); system("pause"); return 0;}
- PAT A 1004. Counting Leaves (30)
- PAT-A 1004. Counting Leaves (30) (树)
- PAT(A) 1004. Counting Leaves (30)
- PAT A 1004. Counting Leaves (30)
- PAT-A-1004. Counting Leaves (30)
- PAT (A )1004. Counting Leaves (30)
- PAT-A 1004. Counting Leaves (30)
- PAT-A 1004.Counting Leaves
- 1004. Counting Leaves (30)-PAT
- pat 1004. Counting Leaves (30)
- 【PAT】1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- pat 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT:1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- [PAT]1004. Counting Leaves (30)
- 2017 Nuist ACM 集训计划
- 网络桥接和ipv6的配置
- 五环之歌之PHP分页
- HTML5利用Canvas绘制图形(使用图像、裁剪区域、使用文本。阴影效果和操作像素)
- Head First Jsp&Servlet笔记2 servlet
- PAT-A-1004. Counting Leaves (30)
- 蚁群算法代码实现
- Android开发中SQLite Expert的操作技巧
- 吝啬的国度
- 12、vue.js 之ref
- Android AOP三大金刚简介
- Hello Kotlin
- JAVA面试要点002_Git中fetch和pull的区别
- IOS技术支持