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 ...
随机推荐
- ubuntu 13.10 amd64安装ia32-libs
很多软件只有32位的,有的依赖32位库还挺严重的:从ubuntu 13.10已经废弃了ia32-libs,但可以使用多架构,安装软件或包apt-get install program:i386.有的还 ...
- 堆的判断(codevs 2879)
2879 堆的判断 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 堆是一种常用的数据结构.二叉堆 ...
- Myeclipse中把java代码导成UML类图
Myeclipse中把java代码导成UML类图 1.右键点击项目名称,选择New-------àUML2 Model 2.给类图命名 3.导成类图 1)如果要把整个项目导成类图,则把整个项目拖到类图 ...
- PHP常用类型判断函数
1.gettype():获取变量类型 2.is_array():判断变量类型是否为数组类型 3.is_double():判断变量类型是否为倍浮点类型 4.is_float():判断变量类型是否为浮点类 ...
- Guid和Int还有Double、Date的ToString方法的常见格式(转载)
Guid的常见格式: 1.Guid.NewGuid().ToString("N") 结果为: 38bddf48f43c48588e0d78761eaa1ce6 2.Gu ...
- WPF中的画图
1.border(边框): <Border BorderBrush="Blue" BorderThickness="0,1,1,1" Grid. ...
- Diskpart命令安装系统小结
<diskpart命令安装系统小结> 今天给同学安装系统,win8改win7.同学是预装了win8的联想y480,分区表采用的是GPT格式,捣鼓了半天才知道.GPT格式是新式的分区格式,相 ...
- Groovy安装配置
一.介绍 Groovy是可以运行在 Java 平台上进行动态语言,使用方式基本与使用 Java 的方式相同,Groovy和java基本是可以实现无缝整合,它有以下一些特性: 是一个基于Java虚拟机的 ...
- selector选择器
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="ht ...
- Oracle创建表空间和表
创建表空间和表ORACLE物理上是由磁盘上的以下几种文件:数据文件和控制文件和LOGFILE构成的oracle中的表就是一张存储数据的表.表空间是逻辑上的划分.方便管理的.数据表空间 (Tablesp ...