题目链接:

C2. Brain Network (medium)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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
  2.  
  3. 题意:
  4.  
  5. 给一棵树,求树的直径;
  6.  
  7. 思路:
  8.  
  9. 两次bfs找树的直径,水题;
  10.  
  11. AC代码;
  1. #include <bits/stdc++.h>
  2. /*
  3. #include <vector>
  4. #include <iostream>
  5. #include <queue>
  6. #include <cmath>
  7. #include <map>
  8. #include <cstring>
  9. #include <algorithm>
  10. #include <cstdio>
  11. */
  12. using namespace std;
  13. #define For(i,j,n) for(int i=j;i<=n;i++)
  14. #define Riep(n) for(int i=1;i<=n;i++)
  15. #define Riop(n) for(int i=0;i<n;i++)
  16. #define Rjep(n) for(int j=1;j<=n;j++)
  17. #define Rjop(n) for(int j=0;j<n;j++)
  18. #define mst(ss,b) memset(ss,b,sizeof(ss));
  19. typedef long long LL;
  20. template<class T> void read(T&num) {
  21. char CH; bool F=false;
  22. for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
  23. for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
  24. F && (num=-num);
  25. }
  26. int stk[], tp;
  27. template<class T> inline void print(T p) {
  28. if(!p) { puts(""); return; }
  29. while(p) stk[++ tp] = p%, p/=;
  30. while(tp) putchar(stk[tp--] + '');
  31. putchar('\n');
  32. }
  33.  
  34. const LL mod=1e9+;
  35. const double PI=acos(-1.0);
  36. const LL inf=1e18;
  37. const int N=1e5+;
  38. const int maxn=;
  39. const double eps=1e-;
  40.  
  41. int n,m,vis[N],dis[N];
  42. vector<int>ve[N];
  43. queue<int>qu;
  44.  
  45. void bfs(int x)
  46. {
  47. mst(dis,);
  48. mst(vis,);
  49. qu.push(x);
  50. vis[x]=;
  51. while(!qu.empty())
  52. {
  53. int fr=qu.front();
  54. qu.pop();
  55. int len=ve[fr].size();
  56. for(int i=;i<len;i++)
  57. {
  58. int y=ve[fr][i];
  59. if(!vis[y])
  60. {
  61. dis[y]=dis[fr]+;
  62. vis[y]=;
  63. qu.push(y);
  64. }
  65. }
  66. }
  67. }
  68.  
  69. int main()
  70. {
  71. int u,v;
  72. read(n);read(m);
  73. For(i,,m)
  74. {
  75. read(u);read(v);
  76. ve[u].push_back(v);
  77. ve[v].push_back(u);
  78. }
  79. bfs();
  80. int s=,ans=;
  81. For(i,,n)
  82. if(dis[i]>dis[s])s=i;
  83. bfs(s);
  84. For(i,,n)ans=max(ans,dis[i]);
  85. cout<<ans<<"\n";
  86.  
  87. return ;
  88. }

codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)的更多相关文章

  1. Brain Network (medium)(DFS)

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

  2. Brain Network (medium)

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

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

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

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

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

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

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

  6. poj 1383 Labyrinth【迷宫bfs+树的直径】

    Labyrinth Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 4004   Accepted: 1504 Descrip ...

  7. codeforce 337D Book of Evil ----树形DP&bfs&树的直径

    比较经典的老题 题目意思:给你一颗节点数为n的树,然后其中m个特殊点,再给你一个值d,问你在树中有多少个点到这m个点的距离都不大于d. 这题的写法有点像树的直径求法,先随便选择一个点(姑且设为点1)来 ...

  8. Codeforces 734E Anton and Tree(缩点+树的直径)

    题目链接: Anton and Tree 题意:给出一棵树由0和1构成,一次操作可以将树上一块相同的数字转换为另一个(0->1 , 1->0),求最少几次操作可以把这棵数转化为只有一个数字 ...

  9. E - We Need More Bosses CodeForces - 1000E (tarjan缩点,树的直径)

    E - We Need More Bosses CodeForces - 1000E Your friend is developing a computer game. He has already ...

随机推荐

  1. 网上找的一篇博文,原文搞错了,应该是\r\n,本文已改正!——回车CR和换行line feed

    "回车"(carriage return)和"换行"(line feed)与 ASCII表 关于“回车”(carriage return)和“换行”(line  ...

  2. Python入门--13--递归

    什么是递归: 有调用函数自身的行为 有一个正确的返回条件 设置递归的深度: import sys sys.setrecursionlimit(10000) #可以递归一万次 用普通的方法也就是非递归版 ...

  3. Ajax 实现文件的下载

    JQuery的ajax函数的返回类型只有xml.text.json.html等类型,没有“流”类型,所以我们要实现ajax下载,不能够使用相应的ajax函数进行文件下载.但可以用js生成一个form, ...

  4. eopkg命令

    #命令: add-repo (ar)  ---添加存储库 blame (bl)  ---包所有者和发布信息 build (bi)  ---建立eopkg包 check  ---验证安装 clean  ...

  5. 邁向IT專家成功之路的三十則鐵律 鐵律一:IT人生存之道-柔

    老子在道德經裡頭曾提到:「天下之至柔,馳聘天下之至堅」,又說:「堅強者死之徒,柔弱者生之徒」.其實人在面對世間的萬事萬物都是一樣的,只是當我們學習將這個至理套用在IT的工作職場時,將可以讓我們在這條崎 ...

  6. C++字符串转数字,数字转字符串

    1. 字符串转数字 如将"32"转为32,将"3.1415"转为3.1415,将"567283"转为567283.使用: //Convert ...

  7. ubutu强制结束进程 kill -9 ProcessID

    强制终止进程 kill -9 2128 表示强制结束进程号 2128 对应的进程.

  8. Java中的反射机制,利用反射访问私有

    利用反射,首先是Class对象的获取,之后是Method和Field对象的获取. 以Method为例,从文档中可以看到: getMethod()方法返回的是public的Method对象, 而getD ...

  9. leetcode笔记:Contains Duplicate

    一. 题目描写叙述 Given an array of integers, find if the array contains any duplicates. Your function shoul ...

  10. 天津政府应急系统之GIS一张图(arcgis api for flex)解说(二)鹰眼模块

    解说GIS功能模块实现之前,先大概说一下flexviewer的核心配置文件config.xml,系统额GIS功能widget菜单布局.系统的样式.地图资源等等都是在这里配置的,这里对flexviewe ...