1004. Counting Leaves (30)
2015-09-03 16:08
344 查看
题目地址:http://www.patest.cn/contests/pat-a-practise/1004
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
提交代码
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
提交代码
/* 1004. Counting Leaves (30) http://www.patest.cn/contests/pat-a-practise/1004 树,求出最大层数 和每一层的叶子节点数目, 利用队列和map */ #include <cstdio> #include <iostream> #include <vector> #include <set> #include <algorithm> #include <stack> #include <string> #include <queue> #include <unordered_map> #include <iterator> using namespace std; #define N 100 int n , m; vector<int> v ; int cengci ; unordered_map<int ,int> um; void mfun() { queue<int> que; que.push(1); cengci[1] = 1; int maxx = 0 ; while(!que.empty()) { int nowNum = que.front(); que.pop(); if(cengci[nowNum] > maxx) maxx = cengci[nowNum]; if((int)v[nowNum].size() == 0) um[cengci[nowNum]] ++; int len = v[nowNum].size(); for(int i = 0 ; i < len ; i ++) { int nextNum = v[nowNum][i]; cengci[nextNum] = cengci[nowNum] + 1; que.push(nextNum); } } printf("%d",um[1]); for(int i = 2 ; i <= maxx ; i++) { printf(" %d",um[i]); } } int main() { //freopen("in.txt","r",stdin); int i , j; scanf("%d%d",&n,&m); int id , k ,tmpid; for(i = 0 ; i < m ;i++) { scanf("%d%d" , &id , &k); for(j = 0 ; j < k ;j ++) { scanf("%d",&tmpid); v[id].push_back(tmpid); } } mfun(); printf(""); return 0; }
#include <cstdio> #include <iostream> #include <cstring> #include <vector> #include <queue> using namespace std; #define N 102 vector<int> ve ; int cengci ; int cengCount ; // N个结点 最多N层 int m , n ; void bfs() { memset(cengCount , 0 , sizeof(int)*N); queue<int> que ; que.push(1) ; cengci[1] = 1 ; int maxCeng = 1 ; while(!que.empty()) { int topNum = que.front(); que.pop() ; int len = ve[topNum].size(); if(cengci[topNum] > maxCeng) { maxCeng = cengci[topNum] ; } if(len == 0) { cengCount[cengci[topNum]] ++ ; }else{ for(int i = 0 ; i < len ; i++) { int nextNum = ve[topNum][i] ; cengci[nextNum] = cengci[topNum] + 1 ; que.push(nextNum) ; } } } printf("%d",cengCount[1]); for(int i = 2 ; i <= maxCeng ; i++) { printf(" %d",cengCount[i]); } printf("\n"); } int main() { //freopen("in.txt" , "r" , stdin) ; scanf("%d%d" , &n , &m) ; int i , j ; int id , k , idTmp ; for(i = 0 ; i < m ; i ++) { scanf("%d%d",&id , &k); for(j = 0 ; j < k ; j++) { scanf("%d" , &idTmp) ; ve[id].push_back(idTmp); } } bfs(); return 0 ; }
相关文章推荐
- amazon近期出版的书
- ExtJs 入门教程二十二[动态复选框:RemoteCheckboxGroup]
- hdu2041(递推)
- ExtJs 入门教程二十一[继承:Extend]
- ExtJs 入门教程二十[数据交互:AJAX]
- vs2012的属性管理器和项目属性的一些问题(opencv+qt+vs2012)
- ExtJs 入门教程十九[API的使用]
- Java 程序死锁问题原理及解决方案(来源IBM开发者论坛本文作者文章)
- 对人渣,要往死里打
- ExtJs 入门教程十八[树:TreePanel]
- ExtJs 入门教程十七[列表:GridPanel]
- 腾讯后台开发面试题--整理1
- ExtJs 入门教程十六[页面布局:Layout]
- ExtJs 入门教程十五[员工信息表Demo:AddUser]
- iOS 横线贯穿文字
- maven -- 问题解决(一)解决eclipse中maven项目出现的问题
- 树形 DP hdu4714 Tree2cycle
- ExtJs 入门教程十四[文本编辑器:Editor]
- 刷题的网站
- hdu2457 DNA repair(AC自动机+dp)