codeforces GYM 100781A【树的直径】
先求出每棵树的直径,排个序,要想图的直径最小的话需要每棵树的直径中点像直径最大的树的直径中点连边,这样直径有三种情况:是直径最大的树的直径: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【树的直径】的更多相关文章
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...
- Gym - 100781A Adjoin the Networks (树的直径)
题意: n个点,m条边,m <= n <= 100000,边的长度都为1. 点从 0 ~ n-1 编号.开始时图是不连通的,并且没有环. 通过加入一些边后,可以使图连通.要求加入的边不能多 ...
- Codeforces 592D - Super M - [树的直径][DFS]
Time limit 2000 ms Memory limit 262144 kB Source Codeforces Round #328 (Div. 2) Ari the monster is n ...
- 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 ...
- Codeforces 455C Civilization:树的直径 + 并查集【合并树后直径最小】
题目链接:http://codeforces.com/problemset/problem/455/C 题意: 给你一个森林,n个点,m条边. 然后有t个操作.共有两种操作: (1)1 x: 输出节点 ...
- Codeforces 337D Book of Evil:树的直径【结论】
题目链接:http://codeforces.com/problemset/problem/337/D 题意: 给你一棵树,n个节点. 如果一个节点处放着“罪恶之书”,那么它会影响周围距离不超过d的所 ...
- CodeForces - 592D: Super M(虚树+树的直径)
Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ s ...
- 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 ...
随机推荐
- java生成6位随机数字
//生成6位随机数字 System.out.println((int)((Math.random()*9+1)*100000)); //生成5位随机数字 System.out.println((int ...
- CSUOJ 1542 线段树解决括号反向问题
题目大意: 根据初始给定的合法的小括号排序,每次进行一个操作,将第a位的括号反向,找到一个尽可能靠前的括号反向后是整个括号排列合法 数据量十分大,不断进行查询,要用线段树进行logn的复杂度的查询 首 ...
- [luoguP2146] 软件包管理器(树链剖分)
传送门 看着很吓人,其实就是个树链剖分模板. 可支持操作: 1.将节点 x 到 根 的路径上的值都变成 1 2.将以节点 x 为根的子树的值都变成 0 1A爽~ ——代码 #include <c ...
- redis可视化界面的操作【二十一】
1.安装 2.linux服务器中开启linux服务 root@qiaozhi:~# cd /usr/local/redis root@qiaozhi:/usr/local/redis# ./bin/ ...
- 自定义日志工具LogUtil
package com.pingyijinren.test; import android.util.Log; /** * Created by Administrator on 2016/5/20 ...
- 远程调试 Android 设备使用入门(谷歌翻译版)
移动前端调试方案(Android + Chrome 实现远程调试) 目录 要求 第 1 步:发现您的 Android 设备 第 2 步:从您的开发计算机调试 Android 设备上的内容. 更多操作: ...
- cogs——8. 备用交换机
8. 备用交换机 ★★ 输入文件:gd.in 输出文件:gd.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] n个城市之间有通讯网络,每个城市都有通讯交换机,直 ...
- Ubuntu 16.04安装VirtualBox 5.1实现无缝模式
个人电脑版的虚拟机推荐使用VirtualBox,因为其免费,比起VMware到处要找破解强得多,且最重要的一点是无缝模式,让其感觉不出再用两个操作系统. 下载: wget http://downloa ...
- Ubuntu 16.04安装WebStorm
前提:必须正确安装JDK. 下载: http://confluence.jetbrains.com/display/WI/WebStorm+EAP 或者下载历史版本:https://www.jetbr ...
- mysql 排序order by可以根据权重,进行表达式计算。再排序
1.select * from tbl_actor order by (follower_count+Recommend_weight)*weight_ratio desc limit 3; 2.or ...