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 ...
随机推荐
- androidmanifest
在unity里面这个androidmanifest.xml 就相当于 buildsetting 里面的playersettings
- EM算法和GMM模型推导
- 拉格朗日对偶与kkt条件
- 2017.6.30 IDEA插件--gsonfomat的安装与使用
参考来自:http://www.cnblogs.com/1024zy/p/6370305.html 1.安装 2.使用 (1)新建一个空类 (2)在空类里按快捷键:alt+s,打开gsonformat ...
- 使用BAT编译,链接,执行汇编代码
大家都知道汇编程序(MASM)的上机过程,先要对源代码进行汇编. 连接,然后再执行,而这中间有很多环节需要输入很多东西,麻烦的很(只有经历过的朋友才懂得).如何使这个过程变的简单呢?在我们搞汇编课程设 ...
- 小计一次linux下渗透方法
本文转自91ri 踩点 目标域名是XX.com 我们的目标是大站,所以主站一般都挺安全的,所以直接寻找二级目录,运气好时能找到一些开源的cms,运气更好点找到个dede啥的,那就…. 我们直接枚举他域 ...
- AI学习笔记
人人都是产品经理,继续设计课程啦啦啦啦 ADOBE: ps, ai, fl, dw, fw, ae, pr, id COREL: painter coreldraw autodesk: 三维: ...
- c多线程学习
http://www.ibm.com/developerworks/cn/linux/thread/posix_thread1/index.html http://blog.chinaunix.net ...
- 在Excel2003中给定区域填充随机数?
1.选中一个单元格,输入“=RAND()*40+60”,点CTRL+ENTER键,即可完成公式填充. 2.鼠标悬停单元格边框右下角,出现“+”符号,左键拉动实现格式复制,从而填充整个区域随机数. ★题 ...
- Spark中经常使用工具类Utils的简明介绍
<深入理解Spark:核心思想与源代码分析>一书前言的内容请看链接<深入理解SPARK:核心思想与源代码分析>一书正式出版上市 <深入理解Spark:核心思想与源代码分析 ...