NBUT 1186 Get the Width(DFS求树的宽度,水题)
[1186] Get the Width
- 时间限制: 1000 ms 内存限制: 65535 K
- 问题描述
- 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.
- 样例输入
1
6
4 -1 -1
2 4 5
5 -1 -1
1 2 3
6 -1 -1
3 -1 6- 样例输出
3
题目链接:NBUT 1186
闲来无事水一发简单的= =,听说大二要学数据结构,原来树的宽度是这么个意思,建图dfs一下即可
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=100010;
struct edge
{
int to;
int pre;
};
edge E[N];
int head[N],tot;
int cnt[N],vis[N];
void add(int s,int t)
{
E[tot].to=t;
E[tot].pre=head[s];
head[s]=tot++;
}
void init()
{
CLR(cnt,0);
CLR(head,-1);
tot=0;
CLR(vis,0);
}
void dfs(int cur,int dep)
{
++cnt[dep];
vis[cur]=1;
for (int i=head[cur]; ~i; i=E[i].pre)
{
int son=E[i].to;
if(!vis[son])
dfs(son,dep+1);
}
}
int main(void)
{
int tcase,i,j,n,p,a,b;
scanf("%d",&tcase);
while (tcase--)
{
init();
scanf("%d",&n);
while (n--)
{
scanf("%d%d%d",&p,&a,&b);
if(a!=-1)
add(p,a);
if(b!=-1)
add(p,b);
}
dfs(1,1);
printf("%d\n",*max_element(cnt+1,cnt+N));
}
return 0;
}
NBUT 1186 Get the Width(DFS求树的宽度,水题)的更多相关文章
- HDU 4607 Park Visit 两次DFS求树直径
两次DFS求树直径方法见 这里. 这里的直径是指最长链包含的节点个数,而上一题是指最长链的路径权值之和,注意区分. K <= R: ans = K − 1; K > R: ans = ...
- poj2631 求树的直径裸题
题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...
- Lightoj 1112 - Curious Robin Hood 【单点改动 + 单点、 区间查询】【树状数组 水题】
1112 - Curious Robin Hood PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB ...
- 【DFS求树的最大二分匹配+输入外挂】HDU 6178 Monkeys
http://acm.hdu.edu.cn/showproblem.php?pid=6178 [题意] 给定一棵有n个结点的树,现在有k个猴子分布在k个结点上,我们可以删去树上的一些边,使得k个猴子每 ...
- POJ3107Godfather(求树的重心裸题)
Last years Chicago was full of gangster fights and strange murders. The chief of the police got real ...
- bzoj1103树状数组水题
(卧槽,居然规定了修改的两点直接相连,亏我想半天) 非常水的题,用dfs序(而且不用重复,应该是直接规模为n的dfs序)+树状数组可以轻松水 收获:树状数组一遍A(没啥好骄傲的,那么简单的东西) #i ...
- ytu 2030: 求实数绝对值(水题)
2030: 求实数绝对值 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 10 Solved: 10[Submit][Status][Web Board] ...
- SPOJ Query on a tree 树链剖分 水题
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
- [poj2777] Count Color (线段树 + 位运算) (水题)
发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...
随机推荐
- CodeForces - 405C
Unusual Product Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Sub ...
- July 31st, Week 32nd Sunday, 2016
If you wept for the missing sunset, you would miss all the shining stars. 如果你为错过夕阳而哭泣,那你有可能也会错过灿烂的星空 ...
- Android之自定义控件深入
本文主要讲述两个知识点:popwindow的使用和通过继承View实现一个自定义控件,实现点击,手动按钮的效果. popwindow的使用 //定义 popupWindow popWin = new ...
- Struts2 Action与Servlet API耦合
单元测试在开发中是非常重要的一个环节程序员在写完代码时,相应的单元测试也应写完整,否则你的代码就是不能让人信服的Struts2将Action与Servlet的API进行解耦之后,就使得单元测试变得非常 ...
- Java Hour 16 来个CURD吧!
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 突然想到我最近一直在追的小说,作者每天都会更新两章,而且质量挺高.所以从这篇开 ...
- hdu 2184 01背包变形
转自:http://blog.csdn.net/liuqiyao_01/article/details/8753686 题意:这是又是一道01背包的变体,题目要求选出一些牛,使smartness和fu ...
- Android的四大组件
Android的四大组件:Activity.Service.BroadcastReceiver.Content Provider. Content Provider 属于Android应用程序的组件之 ...
- Hark的数据结构与算法练习之归并排序
算法说明: 归并排序的思路就是分而治之,将数组中的数字递归折半进行排序. 递归到最底层就只剩下有两个数字进行比较,再从底层往下进行排序合并.最终得出结果. 同样,语言描述可能对于不知道这个算法的人来说 ...
- python 类的初始化
虽然我们可以自由地给一个实例绑定各种属性,但是,现实世界中,一种类型的实例应该拥有相同名字的属性.例如,Person类应该在创建的时候就拥有 name.gender 和 birth 属性,怎么办? 在 ...
- HTML-Canvas03
颜色合成 globalCompositeOperation 属性: //先绘制一个图形. ctx.fillStyle = "#00ff00"; ctx.fillRect(10,10 ...