(PAT)1004.Counting Leaves (30)
来源:互联网 发布:康师傅手机 windows 编辑:程序博客网 时间:2024/05/29 21:16
核心是个广度优先算法,利用C++的知识可以写的更简洁一点,为了联系C,故用数组代替,自己模拟一下队列以及记录当前层次以及下一层。
#include<stdio.h>#include<string.h>int n,m;int level[100+10];int family[100+10][100+10];int parse(char s[3]){ if(s[0]=='0')return (s[1]-'0'); else { if(strlen(s)==1)return (s[0]-'0'); else return (s[0]-'0')*10+(s[1]-'0'); }}void bfs(){ int queue[100+10]; for(int i=0;i<100+10;i++) queue[i]=0; int start=0; int end=0; int nextLevel=1; int curLevel=1; queue[start]=1; end=1; nextLevel=start+1; while(start<end) { if(start==nextLevel) { printf("%d ",level[curLevel]); nextLevel=end; curLevel++; } bool leaf=true; for(int i=1;i<=n;i++) { if(i==queue[start])continue; if(family[queue[start]][i]==1) { queue[end++]=i; leaf=false; } } if(leaf) level[curLevel]++; start++; } printf("%d",level[curLevel]);}int main(){ scanf("%d%d",&n,&m); for(int i=0;i<n;i++) { level[i]=0; for(int j=0;j<n;j++) family[i][j]=0; } for(int i=0;i<m;i++) { char pa[3]; int chNum; scanf("%s %d",pa,&chNum); for(int i=0;i<chNum;i++) { char child[3]; scanf("%s",child); int paInt=parse(pa); int chInt=parse(child); family[paInt][chInt]=1; } } bfs();}
0 0
- (PAT)1004.Counting Leaves (30)
- PAT 1004. Counting Leaves (30)(BFS)
- 1004. Counting Leaves (30)(PAT甲)
- PAT (A )1004. Counting Leaves (30)
- 1004. Counting Leaves (30)-PAT
- pat 1004. Counting Leaves (30)
- 【PAT】1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- pat 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT:1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- [PAT]1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT:1004. Counting Leaves (30)
- Eclipse 配置tomcat
- Android Service
- thrift找不到包含的类
- 简述Cocos2D-X中的内部函数
- 解决 Agent admitted failure to sign using the key 问题 with ssh
- (PAT)1004.Counting Leaves (30)
- http://blog.csdn.net/ithomer/article/details/5929428
- 开三个iframe不断刷访问量
- VB.Net程序设计:Excel开发释放资源问题(非杀死Excel进程)
- JavaScript与函数式编程
- Webservice 之.wsdl 生成客户端代码
- IO之字节流
- 地球的癌症
- html超级链接<a>的click事件之后,改变页面标题等