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;
}
时间: 2024-12-25 22:20:11

NBUT 1186 Get the Width(DFS求树的宽度,水题)的相关文章

HDU1166 敌兵布阵 树状数组水题

中文题目,很简单的题目,区间求和,当然对于线段树来说也很水,为了练习一下树状数组,多做做水题吧,加深理解,并且打好基础,我算是被没打好基础给吓坏了,宁可多花几个小时 刷刷水题扎实点, 很裸的题目 操作也很裸,了解树状数组的肯定能做 #include<iostream> #include<cstdio> #include<list> #include<algorithm> #include<cstring> #include<string&g

poj2631 求树的直径裸题

题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: 假设 s-t这条路径为树的直径,或者称为树上的最长路 现有结论,从任意一点u出发搜到的最远的点一定是s.t中的一点,然后在从这个最远点开始搜,就可以搜到另一个最长路的端点,即用两遍广搜就可以找出树的最长路 证明: 1.设u为s-t路径上的一点,结论显然成立,否则设搜到的最远点为T则   dis(u

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

1112 - Curious Robin Hood PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick.

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

1112 - Curious Robin Hood PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick.

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

http://acm.hdu.edu.cn/showproblem.php?pid=6178 [题意] 给定一棵有n个结点的树,现在有k个猴子分布在k个结点上,我们可以删去树上的一些边,使得k个猴子每个猴子都至少和其他一个猴子相连 问树上最少保留多少条边 [思路] 每个猴子要至少和一个猴子相连,考虑保留的边最少,那么最优的情况一定是一条边的两个顶点放两个猴子,这些边的顶点都不重合 我们现在要找到给定的树中最多有多少条这样的边,即最大二分匹配 O(n)的DFS,对于每个结点,优先与叶子结点形成一条

PAT-1021. Deepest Root (25)-DFS求树的最大深度

这道题目主要是给你一个图,那么计算从任何一点开始,以此为根节点,树的最大深度.不保证图的连通性. 通过率挺低的,应该是那个大数据的测试用例,内存超出的问题卡住了,最大数据是10^4,如果用邻接矩阵的形式保存图形,那么将是n*n的空间复杂度,就是10^8*4B个数据,为4*10^5KB内存,题目是3.2*10^5KB内存,所以内存超出.卡在内存上了.所以我们想到的是用邻接表来保存图,但是我们实现的时候不用链表,而是用向量数组的形式来保存图,这个方式极好,要常用. 思路就是用dfs来搜索每一个节点的

POJ3107Godfather(求树的重心裸题)

Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to arrest the mafia leaders. Unfortunately, the structure of Chicago mafia is rather complicated. There are 

hdu 1541 Stars 树状数组水题

Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5210    Accepted Submission(s): 2053 Problem Description Astronomers often examine star maps where stars are represented by points on a pla

【不可能的任务1/200】bzoj1103树状数组水题

(卧槽,居然规定了修改的两点直接相连,亏我想半天) 非常水的题,用dfs序(而且不用重复,应该是直接规模为n的dfs序)+树状数组可以轻松水 收获:树状数组一遍A(没啥好骄傲的,那么简单的东西) 1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 int N=0,n,m,p,q,a[300000],l[300000],pos[300000],end[300000],son[300000],bro[30