PAT 1004 Counting Leaves
来源:互联网 发布:薄暮知秋什么意思 编辑:程序博客网 时间:2024/06/06 09:20
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 childfor 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>using namespace std;struct qnode{int id;int level;}qu[110];int main(){ios::sync_with_stdio(false);int arr[110][110];int mark[110];for(int i=1;i<110;++i){qu[i].id=mark[i]=0;qu[i].level=-1;for(int j=1;j<110;++j){arr[i][j]=0;}}int n,m;cin>>n>>m;for(int i=1;i<=m;++i){int id,k;cin>>id>>k;if(mark[id]==0) mark[id]=1;for(int j=1;j<=k;++j){int tmp;cin>>tmp;arr[id][j]=tmp;if(mark[tmp]==0) mark[tmp]=1;}}if(arr[1][1]==0){cout<<1;return 0;}else{cout<<0;}qu[1].id=1;qu[1].level=0;mark[1]=2;int ti=1,tj=2;while(1){bool flag=0;for(int i=1;i<110;++i){if(mark[i]==1) {flag=1;break;}}if(!flag) break;int tmp=qu[ti].id;++ti;if(arr[tmp][1]==0) continue;for(int i=1;i<110&&arr[tmp][i]!=0;++i){qu[tj].id=arr[tmp][i];qu[tj].level=qu[tmp].level+1;if(mark[qu[tj].id]==1) mark[qu[tj].id]=2;++tj;}}int ii=2;int le=1;while(qu[ii].level==le&&qu[ii].id!=0){int ans=0;while(qu[ii].level==le&&qu[ii].id!=0){if(arr[qu[ii].id][1]==0) ans++;++ii;}cout<<' '<<ans;++le;}}
- pat 1004 Counting Leaves
- pat 1004 counting Leaves
- PAT 1004 Counting Leaves
- PAT 1004 Counting Leaves
- pat 1004 Counting Leaves
- pat 1004 Counting Leaves
- PAT 1004 Counting Leaves
- PAT 1004 Counting Leaves
- PAT 1004 Counting Leaves
- PAT-A | 1004 | Counting Leaves
- pat 1004 Counting Leaves (30)
- PAT甲级1004Counting Leaves
- 浙大PAT 1004题 1004. Counting Leaves
- Pat(Advanced Level)Practice--1004(Counting Leaves)
- PAT Advanced Level 1004:Counting Leaves
- 关于PAT 1004 Counting leaves 的疑问
- PAT (Advanced Level) Practise 1004 Counting Leaves
- PAT 1004 Counting Leaves (30) Python
- 自实现json解析库
- POJ 3169 Layout (差分约束系统)
- android系统的分区结构
- 徜徉在宋词里的女子
- ”树莓派“安装libjpeg库
- PAT 1004 Counting Leaves
- 【android】编程规范、常用技巧和性能优化
- Java排序算法(一):概述
- 【C#基础】【语法02】课堂作业题 01-08
- HDU 3488 Tour(二分图最优匹配:有向环覆盖)
- Spring框架搭建
- Java排序算法(二):直接选择排序
- opencv常用函数(c++版本)
- Java排序算法(三):堆排序