PAT1004. Counting Leaves (30)
来源:互联网 发布:js水泥基怎么使用方法 编辑:程序博客网 时间:2024/06/17 12:33
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1004
考查链表。
记录下叶子节点信息,然后逐层嵌套count就可以了。
代码如下:
//#include "StdAfx.h"#include <iostream> #include <memory.h> using namespace std; int count[100]={0}; int maxlevel=1; struct childLink //孩子链表 { int id; childLink *next; childLink(){next=0;} }; struct Node //树节点 { int childNum; childLink *childHead; Node(){childNum=0;childHead=0;} }*node; void countLeaves(int l,int id) //嵌套求解不同层次节点中叶子节点的个数 { childLink *cl = node[id].childHead; l++; while(cl) { if(maxlevel<l)maxlevel=l; if(node[cl->id].childNum==0)count[l]++; else countLeaves(l,cl->id); cl=cl->next; } } int main() { //freopen("D://test.txt","r",stdin); int n,m; int i,j,id; cin>>n>>m; node=(Node *)malloc(sizeof(Node)*(n+1)); memset(node,0,sizeof(Node)*(n+1)); childLink *cl=0; for(i=0;i<m;i++) { cin>>id; cin>>node[id].childNum; for(j=0;j<node[id].childNum;j++) { cl = new childLink; cin>>cl->id; if(node[id].childHead) cl->next=node[id].childHead; node[id].childHead = cl; } } if(node[1].childNum==0){count[0]=1;maxlevel=0;} else { count[0]=0; countLeaves(0,1); } for(i=0;i<maxlevel;i++) cout<<count[i]<<" "; cout<<count[i]<<endl; return 0; }
- PAT1004. Counting Leaves (30)
- 【PAT1004】Counting Leaves
- pat1004 Counting Leaves
- [搜索]PAT1004 Counting Leaves
- PAT1004. Counting Leaves
- 【PAT1004】Counting Leaves
- pat1004 Counting Leaves
- PAT1004. Counting Leaves bfs
- PAT1004 Counting Leaves
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004 Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- hdu1035
- linux 下使用flash 安装gnome
- 浅谈Service Manager成为Android进程间通信(IPC)机制Binder守护进程之路
- Servlet学习笔记2之通过继承GenericServlet开发Servlet
- NSString 用法注意事项
- PAT1004. Counting Leaves (30)
- 解决ASP.NET中的各种乱码问题
- Zend Framework简单环境搭建
- 09 拦截器的使用
- Android深入浅出之Binder机制
- Ubuntu常用命令大全
- 网络入侵一般步骤
- Java设计模式之二十五(访问者模式)
- JDBC分布式管理