• [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求树的宽度,水题)的更多相关文章

  1. HDU 4607 Park Visit 两次DFS求树直径

    两次DFS求树直径方法见 这里. 这里的直径是指最长链包含的节点个数,而上一题是指最长链的路径权值之和,注意区分. K <= R: ans = K − 1; K > R:   ans = ...

  2. poj2631 求树的直径裸题

    题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...

  3. Lightoj 1112 - Curious Robin Hood 【单点改动 + 单点、 区间查询】【树状数组 水题】

    1112 - Curious Robin Hood PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB ...

  4. 【DFS求树的最大二分匹配+输入外挂】HDU 6178 Monkeys

    http://acm.hdu.edu.cn/showproblem.php?pid=6178 [题意] 给定一棵有n个结点的树,现在有k个猴子分布在k个结点上,我们可以删去树上的一些边,使得k个猴子每 ...

  5. POJ3107Godfather(求树的重心裸题)

    Last years Chicago was full of gangster fights and strange murders. The chief of the police got real ...

  6. bzoj1103树状数组水题

    (卧槽,居然规定了修改的两点直接相连,亏我想半天) 非常水的题,用dfs序(而且不用重复,应该是直接规模为n的dfs序)+树状数组可以轻松水 收获:树状数组一遍A(没啥好骄傲的,那么简单的东西) #i ...

  7. ytu 2030: 求实数绝对值(水题)

    2030: 求实数绝对值 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 10  Solved: 10[Submit][Status][Web Board] ...

  8. SPOJ Query on a tree 树链剖分 水题

    You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

  9. [poj2777] Count Color (线段树 + 位运算) (水题)

    发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...

随机推荐

  1. project.json

    概述 项目相关配置,由原来的cocos2d.js中转移到project.json中,该文件需要与index.html同级,一般建议放在根目录下. 字段说明 debugMode 相当于原来的COCOS2 ...

  2. javascript基础总结

    一.语法 ------------------------------ javascript中的一切(变量.函数名和操作符)都区分大小写. 标识符:就是指变量.函数.属性的名字,标识符要求如下 1 第 ...

  3. Windows环境下配置Vim为Python的IDE

    (一)安装Python 2.7 在官网下载Python,并安装,我的安装路径是D:\Program Files\Python.安装完成后编辑环境变量Path,在其后添加;D:\Program File ...

  4. Java Hour 46 SLF4J

    SLF4J(Simple Logging Facade for Jave). 上个章节在配置启动项目后,会报一个奇怪的错误,一开始以为是Maven 依赖有冲突,其实是和slf4j 密切相关的. 本文原 ...

  5. wp8 入门到精通 WebClient Post

    WebClient wc = new WebClient(); var URI = new Uri("http://your_uri_goes_here"); //If any e ...

  6. poj 2245 水题

    求组合数,dfs即可 #include<cstdio> #include<iostream> #include<algorithm> #include<cst ...

  7. matlab练习程序(矩形变换为单连通形状)

    变换使用的模板必须是单连通的,而且模板中心必须在模板内,如果在模板中打个结或是月牙形,这里的程序就处理不了了. 虽然非单连通模板也有办法处理,不过不是这里要讨论的. 这里用到的方法和矩形变换为圆那片文 ...

  8. 电赛菜鸟营培训(一)——STM32F103CB之LED控制

    一.STM32F103C8 引脚分布 二.LED的共阴.共阳接法 这里应该是七段数码管的接法. 限流电阻选择为470,在Multism中仿真,也需要接入,否则会出现闪烁情况.或者直接更改属性. 三.消 ...

  9. python 自定义排序函数

    自定义排序函数 Python内置的 sorted()函数可对list进行排序: >>>sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36] 但 ...

  10. spring获取webapplicationcontext,applicationcontext几种方法详解

    法一:在初始化时保存ApplicationContext对象代码: ApplicationContext ac = new FileSystemXmlApplicationContext(" ...