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. windows cmd 看服务cpu、内存

    开始菜单-运行-cmd-输入systeminfo-回车 不用命令从以下两个地方都可以看出CPU个数 使用命令看CPU 利用win+r键打开运行,输入cmd回车即会出现 查看cpu信息 通过上图可以看出 ...

  2. bzoj 4501: 旅行 01分数规划+概率期望dp

    题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=4501 题解: 首先我们不考虑可以删除边的情况下,如何计算期望边数. 然后我们发现这是个有 ...

  3. asp.net C#操作存储过程读取存储过程输出参数值

    这段时间在做一个价格平台的项目时候,同事让我写一个存储过程.该存储过程是根据查询条件得出一组新数据,并且返回该组数据的总条数,此处的存储过程我用到了分页,其中主要知识点和难点是之前做项目的时候没有用到 ...

  4. 时空上下文视觉跟踪(STC)算法

    论文原文以及Matlab代码下载 算法概述 而STC跟踪算法基于贝叶斯框架,根据跟踪目标与周围区域形成的的时空关系,在图像低阶特征上(如图像灰度和位置)对目标与附近区域进行了统计关系建模.通过计算置信 ...

  5. C#程序运行计时

    var stp = new System.Diagnostics.Stopwatch(); stp.Start();//计时启动 ..........程序代码........... stp.Stop( ...

  6. c++对象导出到lua

    转自:http://www.cnblogs.com/ringofthec/archive/2010/10/26/luabindobj.html 虽然有tolua++, luabind等等, 不过自己手 ...

  7. 移植完linux-3.4.2内核,启动系统后使用命令ifconfig -a查看网络配置,没有eth0

    问题: / # ifconfig / # ifconfig eth0  ifconfig: eth0: error fetching interface information: Device not ...

  8. nmap 快速扫描所有端口

    nmap -sT -sV -Pn -v xxx.xxx.xxx.xxx nmap -sS -p 1-65535 -v 192.168.1.254参数:-sS    TCP SYN扫描    nmap ...

  9. Java探索之旅(16)——异常处理

    1.异常与异常处理 在<java编程思想>中这样定义 异常:阻止当前方法或作用域继续执行的问题.虽然java中有异常处理机制,但是要明确一点,决不应该用"正常"的态度来 ...

  10. 错误:Tomcat version 7.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 and 6 Web

    在eclipse的workspace里面找到该项目. 依次进入:.settings->org.eclipse.wst.common.project.facet.core.xml. 打开文件后,将 ...