Codeforces 690C2 Brain Network (medium) (树的直径)
2016-07-11 09:40
375 查看
Brain Network (medium)
time limit per test2 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 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 a b 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
注: 题目一中的满足条件:1 图中不存在环 2 所有点都在图中;
思路: 题目得知该图为一棵树,所求为为树最长路径即树的直径;首先取任意点bfs求出具该点最远的点即树的直径的一段再对这个点进行bfs()所求的最远距离即为树的直径。
下面ac代码
#include<iostream> #include<queue> #include<cstdio> #include<stack> #include<string> #include<cmath> #include<cstring> #include<algorithm> #include<map> #include<set> using namespace std; struct node { int n; int far; }tmp; int m,n; int stu[100005]; bool vis[100005]; queue<node>Q; vector<int>num[100005]; void bfs(int n) { tmp.n = n; tmp.far = 0; Q.push(tmp); vis = true; stu = 0; while(!Q.empty()) { node now = Q.front(); Q.pop(); vector<int>::iterator it; for(it = num[now.n].begin() ; it != num[now.n].end() ; ++it) { if(!vis[*it]) { stu[*it] = now.far + 1; tmp.n = *it; tmp.far = stu[*it]; Q.push(tmp); vis[*it] = true; } } } } int main() { while(cin>>n>>m) { memset(vis , 0 ,sizeof(vis)); memset(stu , 0 ,sizeof(stu)); for(int i = 1 ; i <= n ; ++i) { num[i].clear(); } for(int i = 1 ; i <= m ; ++i) { int st , en; cin>>st>>en; num[st].push_back(en); num[en].push_back(st); } bfs(1); int lenmax = 0; int weizhi = 1; for(int i = 1 ; i <= n ; ++i) { if(stu[i] > lenmax) { lenmax = stu[i]; weizhi = i; } } memset(vis, 0 ,sizeof(vis)); bfs(weizhi); lenmax = 0; weizhi = 1; for(int i = 1 ; i <= n ; ++i) { if(stu[i] > lenmax) { lenmax = stu[i]; weizhi = i; } } cout<<lenmax<<endl; } return 0; }
相关文章推荐
- C#实现获取系统目录并以Tree树叉显示的方法
- C语言实现输入一颗二元查找树并将该树转换为它的镜像
- 纯jsp打造无限层次的树代码
- 一波C语言二元查找树算法题目解答实例汇总
- php遍历树的常用方法汇总
- PHP树的深度编历生成迷宫及A*自动寻路算法实例分析
- PHP生成树的方法
- PHP树-不需要递归的实现方法
- Java Swing中的表格(JTable)和树(JTree)组件使用实例
- 浅析AST抽象语法树及Python代码实现
- python数据结构树和二叉树简介
- 详解字典树Trie结构及其Python代码实现
- 详解字典树Trie结构及其Python代码实现
- PHP树-不需要递归的实现方法
- B+树到MySQL之innoDB
- ExtJS 4 树
- 数据库表TreeView树的快速生成
- Oracle 查询所有的父节点和子节点
- 多层级节点树实现
- 树的双亲存储: