H - Brain Network (medium)

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

CodeForces 690C2

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.

Sample Input

Input
4 3
1 2
1 3
1 4
Output
2
Input
5 4
1 2
2 3
3 4
3 5

Output

3

题意是 n ,m 是 n 点 m 边,然后 m 行边的描述,问这棵树的两节点最远距离是多少,两两相邻的节点距离算 1

//我还以为并查集能做,想了半天,然后发现实在想得太简单了

//从任一点 DFS 这棵树,到最远节点,然后再从最远节点 DFS 到最远节点,就是树的节点最远距离了

 #include <iostream>
#include <cstdio>
#include <vector>
using namespace std; vector<int> p[];
int ans,far; void Dfs(int cur,int pre,int step)
{
if (step>ans)
{
ans=step;
far=cur;
}
int i,j;
for (i=;i<p[cur].size();i++)
{
int to = p[cur][i];
if (to!=pre)//不回头
{
Dfs(to,cur,step+);
}
}
} int main()
{
int n,m;
while (scanf("%d%d",&n,&m)!=EOF)
{
int i;
for (i=;i<=n;i++)
p[i].clear();
for (i=;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
p[a].push_back(b);
p[b].push_back(a);
}
ans=;
Dfs(,,);
ans=;
Dfs(far,,);
printf("%d\n",ans);
}
return ;
}

最新文章

  1. [SoapUI] 在SoapUI里用Java语言判断Excel单元格为空或者Null时出错
  2. mongodb(Index)
  3. (转) C/C++中const关键字详解
  4. scp 使用
  5. Linux系统自启动脚本
  6. 正确看待HTML5的语法变化
  7. gcc支持c99验证
  8. android自定义状态栏颜色
  9. Ruby类
  10. Storm入门(四)WordCount示例
  11. Codeforces 446A. DZY Loves Sequences (线性DP)
  12. Problem J. Journey with Pigs
  13. SSH与MVC
  14. selenium——表单嵌套
  15. 01python语言程序设计基础——初识python
  16. idea问题总结记录
  17. Python网络编程笔记
  18. 20145216史婧瑶 《网络对抗》 MSF基础应用
  19. 【分形】【洛谷P1498】
  20. golang rpc 简单范例

热门文章

  1. Snapdragon connect to android devices
  2. 深入理解dataset及其用法
  3. SQL Script for read information from a csv file in FTP Server
  4. 2017.3.31 spring mvc教程(四)全局的异常处理
  5. NetBean 远程开发的好文1 --&gt; NetBeans的远程Linux C开发实践
  6. Angular 学习笔记——自定义指令
  7. PHP 在源代码中实现换行使得页面源代码更精致美观
  8. cpu、内存、磁盘
  9. 字典转模型的过程中,空值和id特殊字符的处理
  10. jQuery表单 Ajax向PHP服务端发送文件请求并返回数据