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 ;
}

Brain Network (medium)(DFS)的更多相关文章

  1. Brain Network (medium)

    Brain Network (medium) Further research on zombie thought processes yielded interesting results. As ...

  2. codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)

    题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...

  3. CodeForces 690C2 Brain Network (medium)(树上DP)

    题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离. 析:就是一个树上DP,用两次BFS或都一次DFS就可以搞定.但两次的时间是一样的. 代码如下: #include<bits/std ...

  4. Brain Network (easy)

    Brain Network (easy) One particularly well-known fact about zombies is that they move and think terr ...

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

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

  6. codeforces 690C3 C3. Brain Network (hard)(lca)

    题目链接: C3. Brain Network (hard) time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  7. Brain Network (easy)(并查集水题)

    G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  8. codeforces 690C1 C1. Brain Network (easy)(水题)

    题目链接: C1. Brain Network (easy) time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  9. Codeforces 690 C3. Brain Network (hard) LCA

    C3. Brain Network (hard)   Breaking news from zombie neurology! It turns out that – contrary to prev ...

随机推荐

  1. ECSHOP站内页面跳转,避免死链

    2.x版本域名重定向: # For ISAPI_Rewrite 2.x RewriteCond Host: ^steveluo\.name$ RewriteRule (.*) http\://www\ ...

  2. 2017.5.1 java动态代理总结

    参考来自:http://www.cnblogs.com/jqyp/archive/2010/08/20/1805041.html 1.代理模式 代理类和委托类有相同接口. 代理类负责为委托类:预处理消 ...

  3. DevExpress控件之popupMenu

    一.首次创建 1.可直接从工具栏拉一个PopupMenu出来, 2.右键Customize,Yes(提示是否自动创建BarManager,并为popupmenu绑定这个BarManager): 3.编 ...

  4. EffectiveJava(1) 构造器和静态工厂方法

    构造器和静态工厂方法 **构造器是大家创建类时的构造方法,即使不显式声明,它也会在类内部隐式声明,使我们可以通过类名New一个实例. 静态方法是构造器的另一种表现形式** 主题要点:何时以及如何创建对 ...

  5. 页面加载后累加,自加1&&判断数字是否为两位数

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. JAVA Eclipse中的Android程序如何使用线程

    我们先单独定义一个java类,名字可以任意取(比如叫做ClientHeartBeat类,我当前在做一个socket通信的客户端,我们假定需要一个可以测试心跳的程序),注意他要继承Thread,然后重载 ...

  7. Laravel之中间件

    一.中间件的作用 HTTP 中间件提供了一个便利的机制来过滤进入应用的 HTTP 请求.例如,Laravel 包含了一个中间件来验证用户是否经过授权,如果用户没有经过授权,中间件会将用户重定向到登录页 ...

  8. src-resolve: 无法将名称 'extension' 解析为 'element declaration' 组件。

    activiti流程部署时,出现“src-resolve: 无法将名称 'extension' 解析为 'element declaration' 组件.”错误. 出错原因:项目所在路径中有中文.

  9. MySQL 数据库备份种类以及经常使用备份工具汇总

    mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec) mysql> show master st ...

  10. nginx根据目录反向代理到后端服务器

    nginx根据目录反向代理到后端不同的服务器 server {         listen 80;         server_name demo.domain.com;         #通过访 ...