先求出每棵树的直径,排个序,要想图的直径最小的话需要每棵树的直径中点像直径最大的树的直径中点连边,这样直径有三种情况:是直径最大的树的直径: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. os系统下安装Python2和Python3

    一.下载Xcode工具 1.在App Store 里面下载并安装Xcode 2.安装好Xcode后就打开它,首次进入会有一些LicenceAgreement,点同意就是了,然后就进入到 这个界面: 3 ...

  2. jQuery_计算器实例

    知识点: fadeIn()---计算器界面载入淡入效果 hover()---鼠标移入移出某个元素时触发的事件 click()---鼠标单击事件 css()---对元素样式的操作 val()---获取表 ...

  3. Android BottomSheet:底部弹出Fragment面板(4)

     Android BottomSheet:底部弹出Fragment面板(4) BottomSheet不仅可以弹出轻量级的定制好的面板(见附录文章5,6,7),还可以弹出"重"的 ...

  4. android开发里跳过的坑——listview不显示

    在蓝牙回调接口public void onLeScan(BluetoothDevice device, int arg1, byte[] arg2)里面调用adpter.notifyDataSetCh ...

  5. 【git】Git 提示fatal: remote origin already exists 错误解决办法

    今天使用git 添加远程github仓库的时候提示错误:fatal: remote origin already exists. 最后找到解决办法如下: 1.先删除远程 Git 仓库 $ git re ...

  6. Binary Tree Preorder Traversal (非递归实现)

    具体思路参见:二叉树的非递归遍历(转) 先序遍历(根左右). 即把每一个节点当做根节点来对待. /** * Definition for binary tree * struct TreeNode { ...

  7. select语句中会影响查询效率的因素

    1.没有创建索引,或者没有正确使用索引;2.存在死锁的情况,从而导致select语句挂起; 3.返回不必要的列,如很多人喜欢在程序中使用select * from 这样会查询表或视图中的所有字段,如果 ...

  8. 得到java异常printStackTrace的详细信息

    平时写Java代码时,想看抛出的异常信息,来找出具体的异常点,我们常常会用Exception.toString ()或者 Exception.getMessage()来取得异常信息,再把它print到 ...

  9. LoadRunner脚本编写

    性能測试project师要懂代码么?答案是必须的.好多測试员觉得在loadrunner中编写脚本非常难非常牛X ,主要是大多測试人员并未做过开发工作,大学的那点程序基础也忘记的几乎相同了. 还有非计算 ...

  10. Outlook2010 没有Exchange Server,怎么自动回复邮件?

    步骤 1:创建邮件模板 单击“开始”>“新建邮件”. 在邮件正文中,键入要作为自动答复发送的邮件. 在邮件窗口中,单击“文件”>“另存为”. 在“另存为”对话框中的“保存类型”列表中,单击 ...