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 ...
随机推荐
- Mysql_SQL_常用知识点&实践
1.Mysql中类似于nvl()函数的ifnull()函数 ) FROM Table 2.添加某个字段(指定字段column的位置) ----------添加字段zoneId ) NOT NULL A ...
- 质量平台建设之Mock平台
转载:http://blog.csdn.net/spark2008/article/details/51372913 基于目前系统越来越多,在项目开发过程中,减少各模块之间的开发依赖,另保证各个系统模 ...
- Python线程操作
一.全局锁 1.在Python中,Python代码的执行由Python虚拟机来控制,而在Python虚拟机中,同一时刻只有一个线程在执行,就像单CPU的系统中运行多个进程那样,内存中可以存放多个程序, ...
- SoC嵌入式软件架构设计之三:代码分块(Bank)设计原则
上一节讲述了在没有MMU的CPU(如80251.MIPS M控制器系列.ARM cortex m系列)上实现虚拟内存管理的集成硬件设计方法.新设计的内存管理管理单元要实现虚拟内存管理还须要操作系统.代 ...
- PowerShell-将CSV导入SQL Server
$database = 'foxdeploy' $server = '.' $table = 'dbo.powershell_test' Import-CSV .\yourcsv.csv | ForE ...
- smali 语法参考
原文见:http://www.blogjava.net/midea0978/archive/2012/01/04/367847.html Dalvik opcodes Author: Gabor Pa ...
- C# 写日志到文件
C# 写日志到文件 using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms ...
- PHP面试题及答案解析(1)—PHP语法基础
1. strlen( )与 mb_strlen( )的作用分别是什么? strlen和mb_strlen都是用于获取字符串长度.strlen只针对单字节编码字符,也就是说它计算的是字符串的总字节数.如 ...
- 《JavaScript权威指南》学习笔记之二十---XMLHttpRequest和AJAX解决方式
一.AJAX概述 AJAX是Asynchronous JavaScript and XML的缩写.中文译作异步JavaScript和XML.AJAX 不是新的编程语言,而是一种使用现有标准的新方法.在 ...
- Atitit.swift 的新特性 以及与java的对比 改进方向attilax 总结
Atitit.swift 的新特性 以及与java的对比 改进方向attilax 总结 1. defer关键字1 2. try!形式存在的“不失败”机制3 3. Guard 4 4. swift的新语 ...