PAT Advanced Level 1004:Counting Leaves
来源:互联网 发布:军事武器数据库 编辑:程序博客网 时间:2024/05/16 09:34
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<cstdio>#include<cstdlib>#include<algorithm>#include<cstring>#include<queue>using namespace std;struct Node{ int id; int level; struct Node * child[100];};int bfs(Node * root){ queue<Node> que; que.push(*root); printf("0"); int cLevel=2,count=0; while(!que.empty()){ Node c=que.front(); if(c.level==cLevel+1){ printf(" %d",count); count=0; cLevel++; } int i=0; while(c.child[i]!=NULL){ c.child[i]->level=c.level+1; que.push(*c.child[i++]); } if(i==0) count++; que.pop(); } printf(" %d",count);}int main(){ int N,M,k,pid,cid; Node nodes[105]; scanf("%d%d",&N,&M); if(N==1||M==0){ printf("1"); return 0; } while(M--){ scanf("%d%d",&pid,&k); Node * parent=&nodes[pid]; if(pid==1){ nodes[1].level=1; nodes[1].id=pid; parent=&nodes[1]; } int i=0; while(k--){ scanf("%d",&cid); nodes[cid].id=cid; nodes[cid].level=parent->level+1; parent->child[i++]=&nodes[cid]; } } bfs(&nodes[1]); return 0;}
- Pat(Advanced Level)Practice--1004(Counting Leaves)
- PAT Advanced Level 1004:Counting Leaves
- PAT (Advanced Level) Practise 1004 Counting Leaves
- PAT(Advanced Level) 1004 Counting Leaves 解题报告
- PAT (Advanced Level) Practise 1004 Counting Leaves (30)
- 【PAT Advanced Level】1004. Counting Leaves (30)
- 浙大 PAT Advanced level 1004. Counting Leaves
- PAT (Advanced Level) 1004. Counting Leaves (30)
- 【PAT】【Advanced Level】1004. Counting Leaves (30)
- 浙大PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- 浙大PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- [PAT (Advanced Level) ]1004. Counting Leaves解题文档
- PAT (Advanced Level) 1004. Counting Leaves (30) 解题报告
- PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- PAT (Advanced Level) 1004. Counting Leaves (30) 层序遍历,两个queue辅助
- @netserviceHandle及@符号的学习
- 第二章 图形界面还是命令窗口
- MATLAB EM2D 算法
- 查找替换使用方法
- Android--多线程之Handler
- PAT Advanced Level 1004:Counting Leaves
- win7 ghost安装教程
- MATLAB 视频读取及灰度化
- ubuntu安装使用笔记07——arrayvpn安装及使用
- Java关键字介绍之this与super
- 64位win7下安装oracle11g以及plsql developer链接时出现 ORA-12154: TNS: 无法解析指定的连接标识符
- 2014年最新前端开发面试题
- SupremeWord进度30.1%
- hbase性能调优