CodeForces 690C2 Brain Network (树上最大距离)
2016-07-28 15:27
639 查看
题目链接
题目描述
C2. Brain Network (medium)
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
题意:
给了一棵树,求树上最长距离(树上两任意结点距离最大值)
题解
用两次dfs,先dfs一遍,找出一条最长路径的最末端节点(从任一结点p开始dfs找出离这个结点距离最长的结点s),再从这个点s开始再dfs一次找到与s距离最长的点t,s和t的距离就是树上最长路径。
题目描述
C2. Brain Network (medium)
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
题意:
给了一棵树,求树上最长距离(树上两任意结点距离最大值)
题解
用两次dfs,先dfs一遍,找出一条最长路径的最末端节点(从任一结点p开始dfs找出离这个结点距离最长的结点s),再从这个点s开始再dfs一次找到与s距离最长的点t,s和t的距离就是树上最长路径。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<vector> using namespace std; const int maxn=100000+5; vector<int> G[maxn]; int _max,flag; void dfs(int u,int fa,int dep) { for(int i=0;i<G[u].size();i++) { int v=G[u][i]; if(v==fa) continue; if(dep+1>_max) { _max=dep+1; flag=v; } dfs(v,u,dep+1); } } void init() { for(int i=0;i<maxn;i++) G[i].clear(); _max=0,flag=-1; } int main() { //freopen("in.txt","r",stdin); int n,m; while(~scanf("%d%d",&n,&m)) { init(); for(int i=0;i<m;i++) { int p,q; scanf("%d%d",&p,&q); G[p].push_back(q); G[q].push_back(p); } dfs(1,-1,0); _max=0; dfs(flag,-1,0); printf("%d\n",_max); } }
相关文章推荐
- 再MAC 下用IntelliJ 开发web项目遇到 Server is not connected. Deploy is not available. 1099的问题
- Blockchian区块链:IBM HyperLedger fabric 简述
- inline static varaible
- zabbix启动报错:Connection to database 'xxx' failed解决方法
- support.SerializationFailedException: Failed to deserialize payload.
- UVA 514 - Rails
- git出现Agent admitted failure to sign using the key错误
- 92.You executed the following commands in an RMAN session for your database instance that has failur
- FZU2109 Mountain Number (数位DP)
- 【HDU5745 2016 Multi-University Training Contest 2M】【二维空间维护 讨论题?排序简化!】Memento Mori 空间范围满足一定排列关系点阵数
- 【HDU5745 2016 Multi-University Training Contest 2L】【bitset做法or暴力】La Vie en rose 目标串多少子串可以被原始串做相邻交换得到
- 【HDU5744 2016 Multi-University Training Contest 2K】【水题 贪心】Keep On Movin 给定字符使得最短回文串最长
- 【HDU5743 2016 Multi-University Training Contest 2J】【dfs展开式DP 前缀和思想】Join The Future 40个数已知区间和为奇或偶输出方案
- 【HDU5742 2016 Multi-University Training Contest 2I】【简单讨论】It's All In The Mind 给定除法式尽量大
- 【HDU5741 2016 Multi-University Training Contest 2H】【线段树 扫描线】Helter Skelter 超级01串是否有恰好x个0y个1的子串
- 【HDU5740 2016 Multi-University Training Contest 2G】【二分图染色+费用流+路径输出】Glorious Brilliance 最少交换次数使得相邻点颜色
- 解决Visual Studio 2010新建工程时出现『1>LINK : fatal error LNK1123: failure during conversion to COFF: file in
- MainWindow
- 2014Shanghai Online
- lvm[12446]: Another thread is handling an event. Waiting