Brain Network (medium)(DFS)
H - Brain Network (medium)
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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 a b it connects (1 ≤ a, b ≤ n and a ≠ b).
Output
Print one number – the brain latency.
Sample Input
4 3
1 2
1 3
1 4
2
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)的更多相关文章
- Brain Network (medium)
Brain Network (medium) Further research on zombie thought processes yielded interesting results. As ...
- 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) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一 ...
- codeforces 690C3 C3. Brain Network (hard)(lca)
题目链接: C3. Brain Network (hard) time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- Brain Network (easy)(并查集水题)
G - Brain Network (easy) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- 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 ...
随机推荐
- OPENDJ的安装图文说明
一. 说明 介绍: opendj是一个ldap服务器 用于存储openam的配置和用户存储信息 准备工具: OpenDJ-3.0.0.zip 二. 安装步骤 a) Linux安装过程 1. 将zip包 ...
- Shell--变量键盘读取、数组与声明:read,array,declare
1.read read [-pt] variable -P:后面可以接提示信息 -t:后面可以接等待的秒数,时间到后等待结束 read后面不加任何参数,直接加变量名称,那么就会主动出现一个空白行等待你 ...
- account for 与led to和result in的区别
account for sth:be the explanation of sth; explain the cause of sth 作某事物的解释; 解释某事物的原因:His illness ac ...
- zookeeper安装和使用
Zookeeper是Hadoop的一个子项目,它是分布式系统中的协调系统,可提供的服务主要有:配置服务.名字服务.分布式同步.组服务等. 1.下载地址 https://mirrors.cnnic.cn ...
- bitcoin-qt忘记密码
客户端有钱包加密功能,输入密码可以增强安全性. 但是,如果你把钱包文件弄丢了,那即使设了密码,币也会丢. 关闭bitcoin-qt,把钱包文件wallet.dat 剪到其他地方(但不要删除),然后重新 ...
- iOS_新版iOS11 UITbleView适配的一些问题及解决方法
// tableView 偏移20/64适配 if (@available(iOS 11.0, *)) { self.tableView.contentInsetAdjustmentBehavior ...
- C++11之右值引用(二):右值引用与移动语义
上节我们提出了右值引用,可以用来区分右值,那么这有什么用处? 问题来源 我们先看一个C++中被人诟病已久的问题: 我把某文件的内容读取到vector中,用函数如何封装? 大部分人的做法是: v ...
- 自己定义circle
写了一个还不错的自己定义控件.用来展示完毕度或能力值,程序中配置好所占百分比.图中就能够用外面的圆环来显示百分比,效果还是不错的~ watermark/2/text/aHR0cDovL2Jsb2cuY ...
- <转>创建支持eclipse的多模块maven项目
如何使用eclipse创建Maven工程及其子模块 1,首先创建一个父类工程 子模块继承父类工程 并在父类工程的pom.xml文件中定义引入的jar及其版本号 子模块可以引用 2 ...
- STL学习笔记(数值算法)
运用数值算法之前必须先加入头文件<numeric> 加工运算后产生结果 1.对序列进行某种运算 T accumulate(InputIterator beg,InputIterator e ...