先求出每棵树的直径,排个序,要想图的直径最小的话需要每棵树的直径中点像直径最大的树的直径中点连边,这样直径有三种情况:是直径最大的树的直径:a[tot];是直径最大的树和直径第二大的树的半径拼起来+1:(a[tot]+1)/2+(a[tot-1]+1)/2+1);是直径第二大的树和直径第三大的树的半径拼起来+2:(a[tot-1]+1)/2+(a[tot-2]+1)/2+2

取max即可

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=100005;
int n,m,h[N],cnt,s,a[N],tot,mx;
bool v[N];
struct qwe
{
int ne,to;
}e[N<<1];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
h[u]=cnt;
}
int dfs(int u)
{
v[u]=1;
int m1=0,m2=0;
for(int i=h[u];i;i=e[i].ne)
if(!v[e[i].to])
{
int nw=dfs(e[i].to)+1;
if(nw>m1)
m2=m1,m1=nw;
else if(nw>m2)
m2=nw;
}
if(m1+m2>mx)
mx=m1+m2;
return m1;
}
int main()
{
n=read(),m=read();
for(int i=1;i<=m;i++)
{
int x=read()+1,y=read()+1;
add(x,y),add(y,x);
}
for(int i=1;i<=n;i++)
if(!v[i])
{
mx=0;
dfs(i);
a[++tot]=mx;
}
sort(a+1,a+1+tot);
int ans=a[tot];
if(tot>1)
ans=max(ans,(a[tot]+1)/2+(a[tot-1]+1)/2+1);
if(tot>2)
ans=max(ans,(a[tot-1]+1)/2+(a[tot-2]+1)/2+2);
printf("%d\n",ans);
return 0;
}

codeforces GYM 100781A【树的直径】的更多相关文章

  1. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  2. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

  3. Gym - 100781A Adjoin the Networks (树的直径)

    题意: n个点,m条边,m <= n <= 100000,边的长度都为1. 点从 0 ~ n-1 编号.开始时图是不连通的,并且没有环. 通过加入一些边后,可以使图连通.要求加入的边不能多 ...

  4. Codeforces 592D - Super M - [树的直径][DFS]

    Time limit 2000 ms Memory limit 262144 kB Source Codeforces Round #328 (Div. 2) Ari the monster is n ...

  5. Codeforces Beta Round #14 (Div. 2) D. Two Paths 树的直径

    题目链接: http://codeforces.com/contest/14/problem/D D. Two Paths time limit per test2 secondsmemory lim ...

  6. Codeforces 455C Civilization:树的直径 + 并查集【合并树后直径最小】

    题目链接:http://codeforces.com/problemset/problem/455/C 题意: 给你一个森林,n个点,m条边. 然后有t个操作.共有两种操作: (1)1 x: 输出节点 ...

  7. Codeforces 337D Book of Evil:树的直径【结论】

    题目链接:http://codeforces.com/problemset/problem/337/D 题意: 给你一棵树,n个节点. 如果一个节点处放着“罪恶之书”,那么它会影响周围距离不超过d的所 ...

  8. CodeForces - 592D: Super M(虚树+树的直径)

    Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ s ...

  9. E - We Need More Bosses CodeForces - 1000E (tarjan缩点,树的直径)

    E - We Need More Bosses CodeForces - 1000E Your friend is developing a computer game. He has already ...

随机推荐

  1. jQuery调用WCF 说明

    在项目中用过一些WCF的技术这篇文章是对以前用过的一点东西的一个梳理 一,webconfig的配置除了一般的配置外,与WCF相关的配置如下 <system.serviceModel>    ...

  2. hdu - 1627 Krypton Factor (dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1627 给定 n 和 L 找出第n个范围在0-L之内的字符串,字符串要求没有相邻的子串是相同的. 按照格式输出. ...

  3. UVa 12563_Jin Ge Jin Qu hao

    [题意]在KTV唱歌,假设每首歌最长180s,时间结束时如果还有歌正在唱,则将此歌唱完.为使唱歌时间最长,规定最后唱长达678s的<劲歌金曲>[介是个嘛?] 假设你正在唱KTV,在剩余的t ...

  4. mysql 安装与卸载

    mysql用了也好几年了,但每次安装完或者卸载完就忘记了安装步骤以及卸载步骤,因此将关键的步骤记录下来,供以后参考. 1.mysql安装 ①安装类型有typical,complete,custom,一 ...

  5. AtCoder Grand Contest 011 E - Increasing Numbers(灵性乱搞)

    题意: 当一个整数高位数字总不小于低位数字,或者说写成字符串之后单调不下降,称之为上升数.求一个整数最少能表示为多少个上升数的和.(n<=1e500000) 分析: 考虑那些不下降的数字,一定可 ...

  6. 请问这个「 (?<=<(\w+)>).*(?=<\/\1>) 」正则表达式是什么意思呢?

    问题:https://www.zhihu.com/question/26480812  (?<=<(\w+)>).*(?=<\/\1>) ---------------- ...

  7. Vmware worksiation中使用ISO

    Vmware技巧: 用ISO安装系统,需要添加2个CD设备. IDE 1  中选择 autoinst.iso IDE 2  中选择 “要安装的系统”.iso 简单讲:Vmware模拟机上需要模拟两次i ...

  8. UG如何把语言改成中文,UG如何把界面语言改成中文

    1 高级系统设置,高级,新建一个用户变量(变量名为lang,变量值为chs)   2 高级系统设置,高级,环境变量,系统变量中,查看变量名为UGII_LANG的值是否为simpl_chinese,如果 ...

  9. 破碎纪念---记第二次Nexus4换屏

    四太子的屏幕太易碎了.去年九月份在美国买的,十月便碎了,十二月修好,前几天又摔碎了. 本着对此机的喜爱,今天就进行了第二次换屏. 用同事的话说,如今已经是熟练工种了. 先来看看破碎景象: 右下角破碎, ...

  10. jeasyui控件事件和方法的使用方法

    1.事件是在页面加载后就注册绑定: $(function () { ocr(); }); function ocr() { $('#dgTeacher').datagrid({ onClickRow: ...