#include<bits/stdc++.h>
using namespace std;
vector<int>tree[1000010];
int sum=0;
int dfs(int x, int y)
{
    int first = 0, second = 0;
    for (int i = 0; i < tree[y].size(); ++i)
    {
        if (tree[y][i] == x)
            continue;
        int temp = dfs(y, tree[y][i]);
        if (temp>first)
        {
            second = first;
            first = temp;
        }
        else if (temp > second)
        {
            second = temp;
        }
    }
    sum=max(sum,first+second);
    return first+1;
}
int main()
{
    int n;
    scanf("%d",&n);
    int a,b;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&a,&b);
        tree[a].push_back(b);
        tree[b].push_back(a);
    }
    dfs(0,1);
    printf("%d",sum+1);
    return 0;
}

牛客网小白月赛6C(DFS,思维)的更多相关文章

  1. 牛客网小白月赛5I区间(差分数组)

    链接:https://www.nowcoder.com/acm/contest/135/I来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 32768K,其他语言65536 ...

  2. 牛客网-小白月赛6-J-洋灰三角

    题目链接https://www.nowcoder.com/acm/contest/136/J 这题我还是不找规律了,老老实实推吧,传说找规律也可以,我还是算了 递推式:f(n)=k*f(n-1)+p ...

  3. 牛客网小白月赛6H(最小生成树【克鲁斯卡尔】)

    #include<bits/stdc++.h>using namespace std;long long sum=0;struct node{    int a,b,len;}road[5 ...

  4. 牛客网小白月赛1 B,I

    #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> ...

  5. 牛客网 小白赛4 A三角形【贪心】

    [前驱]:在指定长度的棍子中找到能组成最大周长三角形的三根棍子 链接:https://www.nowcoder.com/acm/contest/134/A 来源:牛客网 题目描述 铁子从森林里收集了n ...

  6. 牛客网 牛客小白月赛12 B.华华教月月做数学-A^B mod P-快速幂+快速乘

    链接:https://ac.nowcoder.com/acm/contest/392/B来源:牛客网 华华教月月做数学 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其 ...

  7. 牛客网 牛客小白月赛1 J.おみやげをまらいました

    J.おみやげをまらいました   链接:https://www.nowcoder.com/acm/contest/85/J来源:牛客网     随便写写.   代码: 1 #include<ios ...

  8. 牛客网 牛客小白月赛1 I.あなたの蛙が帰っています-卡特兰数,组合数阶乘逆元快速幂

    I.あなたの蛙が帰っています   链接:https://www.nowcoder.com/acm/contest/85/I来源:牛客网     这个题有点意思,是卡特兰数,自行百度就可以.卡特兰数用处 ...

  9. 牛客网 牛客小白月赛1 H.写真がとどいています

    H.写真がとどいています   链接:https://www.nowcoder.com/acm/contest/85/H来源:牛客网     这个题数乱了,导致wa了好几次. 特别弱智,从A开始往上,就 ...

随机推荐

  1. 英语发音规则---L字母

    英语发音规则---L字母 一.总结 一句话总结:[l]音在词首和词尾的发音不同,/l/+其它音节./l/+元音./l/+/j/称它为清晰/l/,发清晰/l/音时,在下一个音即将出出时舌头离开上齿龈,迅 ...

  2. Android Studio 技巧备忘

    1.日志输入匹配 在日志输入框,点击Edit Filter Configuration 输入syso,并匹配规则 这样就过滤出自己想要的日志

  3. vuex使用mapActions报错解决办法

    先贴上报错: vuex2增加了mapGetters和mapActions的方法,借助stage2的Object Rest Operator 所在通过 methods:{ ...mapActions([ ...

  4. css绝对对齐

    方法1:使用text-align:justify 能够兼容所有的浏览器,但是一定要在模块和模块或者字之间存在空格,换行符或者制表符,这样才能起作用 ;;} /* 说明: 1.IE中要实现块内单行两端对 ...

  5. codeforces 710C C. Magic Odd Square(构造)

    题目链接: C. Magic Odd Square Find an n × n matrix with different numbers from 1 to n2, so the sum in ea ...

  6. torch7 安装中Missing dependencies for nn:moses >= 1错误解决办法

    Torch7.0安装步骤(默认安装路径是在home下): git clone https://github.com/torch/distro.git ~/torch --recursive cd ~/ ...

  7. 【leetcode刷题笔记】Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  8. [转]CSS3盒模型display:box详解

    时间:2014-02-25来源:网络作者:未知编辑:RGB display:box;box-flex是css3新添加的盒子模型属性,它的出现可以解决我们通过N多结构.css实现的布局方式.经典的一个布 ...

  9. 洛谷【P1100】高低位交换

    二进制前置技能:https://www.cnblogs.com/AKMer/p/9698694.html 题目传送门:https://www.luogu.org/problemnew/show/P11 ...

  10. 开启现有android项目

    开启工程 在屏幕上方的选单列上,选择「File->New->Project」, 会弹出「New Project」对话视窗.Eclipse 是通用的编辑环境,可根据你所安装的不同扩充套件而支 ...