Description

Further research on zombie thought processes yielded interesting results. As we know from the previous problem, the nervous system of a zombie consists of n brains and m brain connectors joining some pairs of brains together. It was observed that the intellectual abilities of a zombie depend mainly on the topology of its nervous system. More precisely, we define the distance between two brains u and v(1 ≤ u, v ≤ n) as the minimum number of brain connectors used when transmitting a thought between these two brains. The brain latency of a zombie is defined to be the maximum distance between any two of its brains. Researchers conjecture that the brain latency is the crucial parameter which determines how smart a given zombie is. Help them test this conjecture by writing a program to compute brain latencies of nervous systems.

In this problem you may assume that any nervous system given in the input is valid, i.e., it satisfies conditions (1) and (2) from the easy version.

Input

The first line of the input contains two space-separated integers n and m (1 ≤ n, m ≤ 100000) denoting the number of brains (which are conveniently numbered from 1 to n) and the number of brain connectors in the nervous system, respectively. In the next m lines, descriptions of brain connectors follow. Every connector is given as a pair of brains ab it connects (1 ≤ a, b ≤ n and a ≠ b).

Output

Print one number – the brain latency.

Examples
input
4 3
1 2
1 3
1 4
output
2
input
5 4
1 2
2 3
3 4
3 5
output
3

求一棵树两点间的最大距离,我们可以先从第一个点开始dfs,走到据它最远的点,然后再拿这个点dfs,走到离这个点最远的点,最大值一直保持更新就行

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int inf=(1<<30);
const int maxn=100005;
int pos;
int n,ans,vis[maxn],in[maxn];
vector<int>e[maxn]; void dfs(int v,int cnt)
{
if(ans<cnt)
{
ans=cnt;
pos=v;
}
if(vis[v])return;
vis[v]=1;
for(int i=0;i<e[v].size();i++)
if(!vis[e[v][i]])
dfs(e[v][i],cnt+1);
} int main()
{
int n,m;
ans=0;
scanf("%d%d",&n,&m);
memset(in,0,sizeof(in));
memset(vis,0,sizeof(vis));
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}
dfs(2,0);
//printf("%d\n",ans);
// cout<<pos<<endl;
memset(vis,0,sizeof(vis));
dfs(pos,0); cout<<ans<<endl;
return 0;
}

  

Helvetic Coding Contest 2016 online mirror C2的更多相关文章

  1. CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)

    题目描述 Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一 ...

  2. Helvetic Coding Contest 2016 online mirror A1

    Description Tonight is brain dinner night and all zombies will gather together to scarf down some de ...

  3. Helvetic Coding Contest 2016 online mirror F1

    Description Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure ...

  4. Helvetic Coding Contest 2016 online mirror B1

    Description The zombies are gathering in their secret lair! Heidi will strike hard to destroy them o ...

  5. Helvetic Coding Contest 2016 online mirror D1

    Description "The zombies are lurking outside. Waiting. Moaning. And when they come..." &qu ...

  6. Helvetic Coding Contest 2016 online mirror C1

    Description One particularly well-known fact about zombies is that they move and think terribly slow ...

  7. Helvetic Coding Contest 2019 online mirror (teams allowed, unrated)

    http://codeforces.com/contest/1184 A1 找一对整数,使x^x+2xy+x+1=r 变换成一个分式,保证整除 #include<iostream> #in ...

  8. [Helvetic Coding Contest 2017 online mirror]

    来自FallDream的博客,未经允许,请勿转载,谢谢, 第一次在cf上打acm...和同校大佬组队打 总共15题,比较鬼畜,最后勉强过了10题. AB一样的题目,不同数据范围,一起讲吧 你有一个背包 ...

  9. 【Codeforces】Helvetic Coding Contest 2017 online mirror比赛记

    第一次打ACM赛制的团队赛,感觉还行: 好吧主要是切水题: 开场先挑着做五道EASY,他们分给我D题,woc什么玩意,还泊松分布,我连题都读不懂好吗! 果断弃掉了,换了M和J,然后切掉了,看N题: l ...

随机推荐

  1. PythonPath在Windows 下的设置

    今天在调试Evernote SDK时, 遇到PythonPath的问题. 查了很多资料,有说用系统环境变量添加PythonPath, 有说在注册表中的PythonPath添加新Default字段, 但 ...

  2. Poj2656(水题)

    一.Description Jinjin is a junior school student. Besides the classes in school, Jinjin's mother also ...

  3. HDOJ1166(线段树点修改)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. 通过设置swift中container的ACL提供匿名访问及用户授权读取服务

    在上层使用swift提供的云存储服务的过程中,提出了无需验证的使用需求. 在参考了:http://my.oschina.net/alanlqc/blog/160196(curl命令操作) 官方文档: ...

  5. ES6学习之let和const

    1.let 基本用法:let声明的变量,只在let命令所在的代码块内有效 { let a = 1; var b = 2; } console.log(a) //a is not defined con ...

  6. Uva 213

    1. 问题 第一次发现新的存储方式,int code[8][1<<8]; 用于存储二进制的形式 将字符以是十进制的方式存储到数组中 如何消除 \n \r 的影响,进行多行的输入 2. 代码 ...

  7. C# 判断路径和文件存在

    1.判断路径是否存在,不存在则创建路径: if (!System.IO.Directory.Exists(@"D:\Export")) { System.IO.Directory. ...

  8. MS SQL update set select

    有张表a,已经有数据 再有张表b,也已查询出数据 两张表有外键关联 需求如下: 更新表a中的某个字段,这个字段要加上(都是int型的数据)对应表b中的数据作为更新的最终数据 )) from #libL ...

  9. Ajax效果--个人收藏

    $.ajax({ url: "../../../Tools/WeChatMenu.ashx?action=get_menu", type: "post", da ...

  10. idea2016 64位 安装,jdk环境变量配置

      idea 激活服务器地址: 地址1: http://www.iteblog.com/idea/key.php     地址2:  http://idea.qinxi1992.cn/ intelli ...