Brain Network (medium)

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 uand 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
  1. 4 3
    1 2
    1 3
    1 4
output
  1. 2
input
  1. 5 4
    1 2
    2 3
    3 4
    3 5
output
  1. 3
    分析:树中最长路,两次bfs
    代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <cstring>
  8. #include <string>
  9. #include <set>
  10. #include <map>
  11. #include <queue>
  12. #include <stack>
  13. #include <vector>
  14. #include <list>
  15. #include <ext/rope>
  16. #define rep(i,m,n) for(i=m;i<=n;i++)
  17. #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
  18. #define vi vector<int>
  19. #define pii pair<int,int>
  20. #define mod 1000000007
  21. #define inf 0x3f3f3f3f
  22. #define pb push_back
  23. #define mp make_pair
  24. #define fi first
  25. #define se second
  26. #define ll long long
  27. #define pi acos(-1.0)
  28. const int maxn=1e5+;
  29. const int dis[][]={{,},{-,},{,-},{,}};
  30. using namespace std;
  31. using namespace __gnu_cxx;
  32. ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
  33. ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
  34. int n,m,vis[maxn],ma,now;
  35. vi a[maxn];
  36. int bfs(int x)
  37. {
  38. memset(vis,,sizeof(vis));ma=;
  39. queue<int>p;p.push(x);vis[x]=;
  40. while(!p.empty())
  41. {
  42. int u=p.front();p.pop();
  43. for(int y:a[u])
  44. {
  45. if(!vis[y])
  46. {
  47. vis[y]=vis[u]+;
  48. p.push(y);
  49. if(ma<vis[y])ma=vis[y],now=y;
  50. }
  51. }
  52. }
  53. return now;
  54. }
  55. int main()
  56. {
  57. int i,j,k,t;
  58. scanf("%d%d",&n,&m);
  59. while(m--)
  60. {
  61. scanf("%d%d",&j,&k);
  62. a[j].pb(k),a[k].pb(j);
  63. }
  64. j=bfs(bfs());
  65. printf("%d\n",ma-);
  66. //system ("pause");
  67. return ;
  68. }

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

  1. Brain Network (medium)(DFS)

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

  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. Brain Network (easy)(并查集水题)

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

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

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

  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. Redis简介一

    Redis是一个开源的,使用C语言编写,面向“键/值”对类型数据的分布式NoSQL数据库系统,特点是高性能,持久存储,适应高并发的应用场景.Redis纯粹为应用而产生,它是一个高性能的key-valu ...

  2. linex下部署发布web项目

    准备工作:通过Xmanager远程连接linux系统,下载对应的jdk和tomcat. 1.通过Xmanager远程连接linux系统,下载地址请自己网上搜索吧. 2.下载jdk (这里为jdk-7u ...

  3. web app 基础界面框架搭建

    一.问题的产生 基础框架界面如图1所示,目前app流行布局 图1 布局关注点: 1.Title和Footer部分为固定布局position:fixed,存在的争议是android2.x和IOS4一下说 ...

  4. Explain of Interaction Operators in UML?

    来源于:EA 中的 Interaction Operators Enterprise Architect User Guide Operator Action alt Divide up intera ...

  5. Windows环境下google protobuf入门

    我使用的是最新版本的protobuf(protobuf-2.6.1),编程工具使用VS2010.简单介绍下google protobuf: google protobuf 主要用于通讯,是google ...

  6. c语言库函数

    #include <stdio.h> #define LENGTH 100 main() { FILE *fd; char str[LENGTH]; fd = fopen("he ...

  7. 初学.net 网页打开过程

    一个网页打开的过程 1.进入控制器里的方法里  控制器的命名必须以Controll结尾前面的名字要和view层的命名一致 2.控制器完了以后 就进入view层对应的视图里 3.视图里调用model   ...

  8. OpenGL—Android 开机动画源码分析二

    引自http://blog.csdn.net/luoshengyang/article/details/7691321/ BootAnimation类的成员函数的实现比较长,我们分段来阅读: 第三个开 ...

  9. MySQL操作失误导致mysql数据库查看不了

    使用  show databases; +--------------------+| Database           |+--------------------+| information_ ...

  10. ubuntu下安装nagios

    第一步安装apache root@root01-virtual-machine:/etc/apache2/conf-available# vi charset.conf 可修改apache服务器的编码 ...