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.
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).
Print one number – the brain latency.
4 3
1 2
1 3
1 4
2
5 4
1 2
2 3
3 4
3 5
3
分析:树中最长路,两次bfs;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e5+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,vis[maxn],ma,now;
vi a[maxn];
int bfs(int x)
{
memset(vis,,sizeof(vis));ma=;
queue<int>p;p.push(x);vis[x]=;
while(!p.empty())
{
int u=p.front();p.pop();
for(int y:a[u])
{
if(!vis[y])
{
vis[y]=vis[u]+;
p.push(y);
if(ma<vis[y])ma=vis[y],now=y;
}
}
}
return now;
}
int main()
{
int i,j,k,t;
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d",&j,&k);
a[j].pb(k),a[k].pb(j);
}
j=bfs(bfs());
printf("%d\n",ma-);
//system ("pause");
return ;
}
Brain Network (medium)的更多相关文章
- Brain Network (medium)(DFS)
H - Brain Network (medium) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d &am ...
- codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)
题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...
- CodeForces 690C2 Brain Network (medium)(树上DP)
题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离. 析:就是一个树上DP,用两次BFS或都一次DFS就可以搞定.但两次的时间是一样的. 代码如下: #include<bits/std ...
- Brain Network (easy)
Brain Network (easy) One particularly well-known fact about zombies is that they move and think terr ...
- CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)
题目描述 Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一 ...
- Brain Network (easy)(并查集水题)
G - Brain Network (easy) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- codeforces 690C3 C3. Brain Network (hard)(lca)
题目链接: C3. Brain Network (hard) time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- codeforces 690C1 C1. Brain Network (easy)(水题)
题目链接: C1. Brain Network (easy) time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- Codeforces 690 C3. Brain Network (hard) LCA
C3. Brain Network (hard) Breaking news from zombie neurology! It turns out that – contrary to prev ...
随机推荐
- Linux下安装MySQL-5.7
写在前面:此博客是针对MySQL5.7安装教程,其他版本可能略有不同,仅供参考. 第一步:下载mysql 在Linux终端使用wget命令下载网络资源: wget http://mirrors.soh ...
- Spring 框架理论基础
一. IOC 控制反转 概念解释:当我需要一个资源时,容器已经帮我准备好,我只需要接受就可以. // 加载 IOC 容器 ApplicationContext ac = new ClassPathXm ...
- 如何更改mysql可执行路径及更改mysql数据库文件路径
一.如何更改服务中MySQL的可执行文件路径: 解决方法:到注册表里HKEY_LOCAL_MECHINE---SYSTEM ---CurrentControlSet 更改查找一. MySQL项值,然后 ...
- VMWare桥接、NAT和only-host三种模式
如果你想利用VMWare安装虚拟机,或想创建一个与网内其他机器相隔离的虚拟系统,进行特殊的调试工作.此时,对虚拟系统网络连接模式的选择就非常重要了.如果你选择的工作模式不正确,就无法实现上述目的,也就 ...
- hadoop三个配置文件的参数含义说明core-site.xml,hdfs-site.xml,mapred-site.xml
配置hadoop,主要是配置core-site.xml,hdfs-site.xml,mapred-site.xml三个配置文件,默认下来,这些配置文件都是空的,所以很难知道这些配置文件有哪些配置可以生 ...
- 第10章 接口、继承与多态----Object类
在Java中,所有的类都直接或间接继承了java.lang.Object类.Object类是比较特殊的类,它是所有类的父类,是Java类层中的最高层类.当创建一个类时,总是在继承,除非某个类已经指定要 ...
- js1中call和apply的用法
js1中call和apply的用法 е辊顷 饼蹭瑭 岚辗疥 碜坪命 笛攮鼠 鲳篝等 ざ遛膜 镀鞭冢蒯 晕 册薷濑 就不是抓了而是人拳啪啪两声两个人都被拳头打在了腿骨 许郾犍 国 ...
- Eclipse下配置和使用Hibernate Tools
本文转自:http://radiumwong.iteye.com/blog/358585 Hibernate Tools可以通过访问已经建立好的数据库以反向工程方式生成POJO文件. 今天就来说一下如 ...
- Python 入门基础学习
Python入门学习 我们使用python3.0版本 首选来入门 带你进入Hello World >>> print ("Hello World")Hell ...
- include和 merge
include和merge标记的作用主要是为了解决layout的重用问题. 比如我们有三四个Activity但是他们都要用到同一个样式的标题栏,虽然我们把一样的代码copy个三四遍也没关系,但实在是太 ...