PAT 1004 Counting Leaves
来源:互联网 发布:淘宝手绘照片是真的吗 编辑:程序博客网 时间:2024/05/17 18:12
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 <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]++;elsequeue.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 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
- 谈浮躁
- 移动端Touch事件与H5-Canvas像素点检测实现刮刮乐
- 打砖块
- 数据库笔试题及答案
- arm-eabi-addr2line 使用解析crash位置
- PAT 1004 Counting Leaves
- Bootstrap modal 被遮挡
- 使用备忘录的方法计算组合,例如计算C(n,k).
- 内存映射文件(专门读写大文件)
- UVA253-4.4(ti)-Cube painting
- 使用Struts2文件上传
- C语言邻接表的实现
- 类的继承练习题
- Java 机器学习库Smile实战(二)AdaBoost