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. 【LeetCode】048. Rotate Image

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  2. MySQL on Azure高可用性设计 DRBD - Corosync - Pacemaker - CRM (二)

    在上一篇文章中描述了MySQL HA on Azured 设计思路,本篇文章中将描述具体的部署,每个组件的安装和配置. 整体的设计架构如下: 下面将是所有组件的安装配置过程,所有的虚拟机是CentOS ...

  3. Puppet master nginx 扩展提升性能(puppet自动化系列4)

    puppet使用SSL(https)协议来进行通讯,默认情况下,puppet server端使用基于Ruby的WEBRick HTTP服务器.由于WEBRick HTTP服务器在处理agent端的性能 ...

  4. stm32 奇怪的位赋值问题 出错了

    转载请注明出处:http://blog.csdn.net/qq_26093511/article/category/6094215 1.在51单片机里 ,下面这两种操作方法都是一样的,没有什么问题! ...

  5. telnet IP:ERROR

    实验环境:CentOS6.8 主机:172.16.xxx.xxx:80 客户端:172.16.xxx.xxx [root@www ~18:32:27]#telnet 172.16.xxx.xxx 80 ...

  6. Python函数(十一)-生成器

    首先看一下什么是列表生成式 >>> [i*2 for i in range(10)] [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] >>> ...

  7. docker-machine create --driver virtualbox myvm1 创建失败

    1. 问题描述 docker-machine create --driver virtualbox myvm1 安装完 virtualbox 后,无法创建. 输出内容为: Running pre-cr ...

  8. Ajax调用后台方法报未定义

    需要在web.config中<system.webServer>节点下添加如下内容 <handlers> <add name="ajax" verb= ...

  9. Unity实现支持泛型的事件管理以减少使用object作为参数带来的频繁装拆箱

    如果不用C#自身的event关键字而是要自己实现一个可统一管理游戏中各种消息事件通知管理的系统模块EventManger时,通常都是把事件delegate的参数定义为object类型以适应所有的数据类 ...

  10. 使用mahout fpgrowth算法求关联规则

    使用mahout fpgrowth 首先,这篇文章的内容大部分取自国外一篇博客Finding association rules with Mahout Frequent Pattern Mining ...