浙大PAT 1004 Counting Leaves (简单深搜)
2014-03-15 16:32
267 查看
1004. Counting Leaves (30)
时间限制400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
用record数组记录每一层上的无孩子节点个数。
#include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include <vector> using namespace std; map<int,vector<int> >adjlist; int record[101]={0}; void DFS(int id,int level){ if(adjlist[id].empty()){ ++record[level]; return; } vector<int>::iterator ite = adjlist[id].begin(); for(;ite!=adjlist[id].end();++ite){ DFS(*ite,level+1); } } int main(){ //freopen("in.txt","r",stdin); int n,m,id,k,sid,i,cle,cnt; scanf("%d %d",&n,&m); cle = n-m; while(m--){ scanf("%d %d",&id,&k); while(k--){ scanf("%d",&sid); adjlist[id].push_back(sid); } } DFS(1,0); printf("%d",record[0]); cnt = record[0]; for(i=1;cnt<cle;++i){ printf(" %d",record[i]); cnt += record[i]; } printf("\n"); return 0; }
相关文章推荐
- (PAT)1004.Counting Leaves (30)
- PAT:1004. Counting Leaves (30)
- 1004. Counting Leaves (30)(PAT甲)
- pat甲级1004. Counting Leaves (30)
- PAT(Advanced Level) 1004 - Counting Leaves(树)
- 1004. Counting Leaves (30) PAT甲级
- pat1004 Counting Leaves
- PAT1004 Counting Leaves
- pat 1004 Counting Leaves (30)
- PAT Advanced Level 1004:Counting Leaves
- 浙大PAT 1004. 成绩排名 (解题思路)
- PAT (Advanced Level) Practise 1004 Counting Leaves
- pat 1004 Counting Leaves
- 关于PAT 1004 Counting leaves 的疑问
- PAT 1004 Counting Leaves (30) Python
- PAT 1004 仿写 结构体的简单使用
- Pat(Advanced Level)Practice--1004(Counting Leaves)
- PAT (甲)1004 Counting Leaves (30)(dfs)
- PAT 1004 Counting Leaves
- PAT(Advanced Level) 1004 Counting Leaves 解题报告