PAT 1004 Counting Leaves
2017-01-19 21:28
309 查看
1004. Counting Leaves (30)
时间限制400 ms
内存限制
65536 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
//关键在于建树,对于建树,再另作总结 #include <cstdio> #include <cstring> #include <iostream> #include <queue> using namespace std; struct node{ int firstChild; int nextSibling; }; struct node tree[105]; int n,m; void Init(){ for(int i=1;i<=n;i++){ tree[i].firstChild=0; tree[i].nextSibling=0; } } void Build(){ int x,count,y; for(int i=1;i<=m;i++){ cin>>x>>count>>y; tree[x].firstChild=y; x=y; for(int j=1;j<count;j++){ scanf("%d",&y); tree[x].nextSibling=y; x=y; } } } void FindLeaves(){//思想为广搜(也可看作层序遍历),队列实现 //此处判断此层遍历结束的思路,即last的运用 很好 int level[n+1],top=0; int last=1; memset(level,0,sizeof(level)); queue<int> queue; queue.push(1); while(!queue.empty()){ int t1=queue.front(); queue.pop(); int t2=tree[t1].firstChild; if(!t2) level[top]++; while(t2){ queue.push(t2); t2=tree[t2].nextSibling; } if(t1==last){ top++; last=queue.back(); } } top--; for(int i=0;i<=top;i++){ printf("%d%c",level[i],i==top?'\n':' '); } } int main(){ cin>>n>>m; Init(); Build(); FindLeaves(); return 0; }
#include <cstdio> #include <cstring> #include <iostream> #include <queue> using namespace std; struct node{ int firstChild; int nextSibling; }; struct node tree[105]; int n,m; void Init(){ for(int i=1;i<=n;i++){ tree[i].firstChild=0; tree[i].nextSibling=0; } } void Build(){ int x,count,y; for(int i=1;i<=m;i++){ cin>>x>>count>>y; tree[x].firstChild=y; x=y; for(int j=1;j<count;j++){ scanf("%d",&y); tree[x].nextSibling=y; x=y; } } } void FindLeaves(){ int level[n+1],top,last; memset(level,0,sizeof(level)); queue<int> queue; queue.push(1); if(!tree[1].firstChild){ printf("%d\n",1); return; } top=1; last=1; while(!queue.empty()){ int t=queue.front(); queue.pop(); int t2=tree[t].firstChild; while(t2){ if(!tree[t2].firstChild) level[top]++; else queue.push(t2); t2=tree[t2].nextSibling; } if(t==last){ top++; last=queue.back(); } } top--; for(int i=0;i<=top;i++){ printf("%d%c",level[i],i==top?'\n':' '); } } int main(){ cin>>n>>m; Init(); Build(); FindLeaves(); return 0; }//vector建树
#include <cstdio> #include <cstring> #include <vector> #include <iostream> using namespace std; vector<int> tree[105]; int leaf[105]; int Max; void dfs(int x,int level){ if(tree[x].size()==0){ leaf[level]++; return; } for(int i=0;i<tree[x].size();i++){ Max=max(Max,level+1); dfs(tree[x][i],level+1); } } int main(){ int n,m; int node,num,child,flag=0; cin>>n>>m; for(int i=1;i<=m;i++){ cin>>node>>num; for(int j=1;j<=num;j++){ cin>>child; tree[node].push_back(child); } } Max=1; dfs(1,1); for(int i=1;i<=Max;i++){ printf("%d%c",leaf[i],i==Max?'\n':' '); } return 0; }
相关文章推荐
- PAT_1004: Counting Leaves
- PAT-1004 - Counting Leaves
- PAT (Advanced Level) Practise 1004 Counting Leaves (30)
- 1004. Counting Leaves (30) PAT甲级
- PAT:1004. Counting Leaves (30)
- pat 1004 Counting Leaves (30)
- 1004. Counting Leaves (30)-PAT甲级真题(bfs,dfs,树的遍历,层序遍历)
- PAT(Advanced Level) 1004 Counting Leaves 解题报告
- PAT-A | 1004 | Counting Leaves
- PAT:1004. Counting Leaves (30)
- PAT (A )1004. Counting Leaves (30)
- Pat(Advanced Level)Practice--1004(Counting Leaves)
- PAT (甲)1004 Counting Leaves (30)(dfs)
- PAT1004:Counting Leaves
- 1004. Counting Leaves (30)(PAT甲)
- 浙大PAT 1004 Counting Leaves (简单深搜)
- PAT(Advanced Level)1004. Counting Leaves (30) 树的层次遍历
- pat 1004 counting Leaves
- PAT-Java-1004-Counting Leaves (30)
- pat 1004 Counting Leaves