http://cdn.ac.nbutoj.com/Problem/view.xhtml?id=1186
来源:互联网 发布:知乎 图标资源 编辑:程序博客网 时间:2024/06/10 12:45
- 问题描述
- It's an easy problem. I will give you a binary tree. You just need to tell me the width of the binary tree.
The width of a binary tree means the maximum number of nodes in a same level.
For example, the width of binary tree above is 3. - 输入
- The first line is an integer T, means the number of cases.
Then follow T cases.
For each case, the first line is an integer N, means the number of nodes. (1 <= N <= 10)
Then follow N lines. Each line contains 3 integers P A B; indicate the number of this node and its two children node. If the node doesn’t have left child or right child, then replace it by -1.
You can assume the root is 1. - 输出
- For each case, output the width.
- 样例输入
164 -1 -12 4 55 -1 -11 2 36 -1 -13 -1 6
- 样例输出
3
AC代码:
#include<iostream>#include<string.h>#include<string>#include<algorithm>#include<cstdio>#define N 20using namespace std;typedef struct {int to;int next;}Node;Node s[3*N];int lev[N];int head[N];int res;void init(){res=0;memset(lev,0,sizeof(lev));memset(head,-1,sizeof(head)); }void add(int a,int b){s[res].to=b;s[res].next=head[a];head[a]=res++;}void dfs(int cur,int now){ lev[cur]++; if(head[now]==-1) return; for(int i=head[now];i!=-1;i=s[i].next) dfs(cur+1,s[i].to);}int main(){int T; scanf("%d",&T);while(T--){int n;init();scanf("%d",&n);for(int i=0;i!=n;++i){int a,b,c;scanf("%d%d%d",&a,&b,&c);if(b!=-1) add(a,b);if(c!=-1) add(a,c);}dfs(0,1);printf("%d\n",*max_element(lev,lev+n));}return 0;}
- http://cdn.ac.nbutoj.com/Problem/view.xhtml?id=1186
- http://cdn.ac.nbutoj.com/Problem/view.xhtml?id=1001&&纸牌游戏
- http://cdn.ac.nbutoj.com/Problem/view.xhtml?id=1003&&矩阵链乘
- http://cdn.ac.nbutoj.com/Problem/view.xhtml?id=1187&&Hole Breaker
- http://cdn.ac.nbutoj.com/Problem/view.xhtml?id=1184&&Elaine's Queue
- http://cdn.ac.nbutoj.com/Problem/view.xhtml?id=1182&&Counter-Strike
- http://cdn.ac.nbutoj.com/Problem/view.xhtml?id=1181
- http://cdn.ac.nbutoj.com/Problem/view.xhtml?id=1180
- http://cdn.ac.nbutoj.com/Problem/view.xhtml?id=1198&& I can do it!
- Enumerate the Triangles&&http://cdn.ac.nbutoj.com/Contest/view/id/14/problem/E.xhtml
- http://acm.nbut.cn:8081/Problem/view.xhtml?id=1317&&线段树单点查询
- Anger Begins&&http://acm.nbut.cn:8081/Contest/view/id/29/problem/A.xhtml
- C函数传递结构体指针 题目地址:http://ac.jobdu.com/problem.php?id=1061
- http://ac.jobdu.com/problem.php?pid=1008
- http://ac.jobdu.com/problem.php?pid=1009
- http://ac.jobdu.com/problem.php?pid=1007
- http://ac.jobdu.com/problem.php?pid=1033
- http://ac.jobdu.com/problem.php?pid=1005
- Emacs 入门指引(一) Emacs简介
- [QT] Qt学习方法(丁林松总结)
- 寻找丑数问题 HDOJ 1058 Humble Numbers
- 一些看起来有用但没用过的函数
- 学习linux 的方法(个人总结)
- http://cdn.ac.nbutoj.com/Problem/view.xhtml?id=1186
- struts中的constant详解
- android 获取gps位置并标注
- struts中的constant详解
- ArrayAdapter和BaseAdapter的区别与用法举例
- Javascript中new的疑惑
- 函数指针的好处
- Packing Rectangles
- [c语言 ] 用libev 写个echo服务器