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 ...
随机推荐
- servlet页面没有跳转
Boolean b = userService.selectByParams(user);if (b) { req.getSession().setAttribute("loginname& ...
- POJ 2096 找bug 期望dp
题目大意: 一个人受雇于某公司要找出某个软件的bugs和subcomponents,这个软件一共有n个bugs和s个subcomponents,每次他都能同时随机发现1个bug和1个subcompon ...
- 算法(1):查找&排序
算法(Algorithm):一个计算过程,解决问题的方法 程序 = 数据结构+算法 时间复杂度: 当算法过程中出现循环折半的时候,复杂度式子中会出现 O(logn) 时间复杂度小结: 1. 时间复杂度 ...
- Thinkphp5.0 的Model模型
Thinkphp5.0 的Model模型 新建user模型User.php: <?php namespace app\index\model; use think\Model; class Us ...
- ***CSS总结-原创
CSS规则 第一个是:选择符.和jquery的选择器是一个意思大括号是:申明块里面KV是:属性和属性值分号是结束符 类别选择符(器) p.red的意思,就是对p标签中类为red的进行CSS作用 ID选 ...
- iphone原生cookie处理
可以使用NSURLConnection的类来执行HTTP请求,登录该网站,并检索的cookie. 要执行一个请求,只是创建一个NSURLConnection的实例,并分配给它的委托对象. NSURLR ...
- Ubuntu 16.04安装Memcached(单机)
Ubuntu 16.04安装Memcached,不过不仅限与Ubuntu,可以用CentOS等去安装,只不过测试时使用的是Ubuntu机器.Windows下不建议使用,本机调试可以使用,线上环境除了W ...
- rsyslogd系统日志服务总结
简单介绍 syslog系统日志服务协议,标准出来的比较晚 用于记录系统日志或者用户程序产生的日志 采用C/S架构,本地可以通过socket和syslogd守护进程通讯,远程通过TCP/UDP协议通信, ...
- mvn打包源码的方法:maven-source-plugin
maven-javadoc-plugin可以打包 dubbo-demo-provider-2.6.1-javadoc.jar maven-jar-plugin 打包插件 dubbo-demo-prov ...
- Linux学习系列之MySQL备份
MySQL排除表备份 #!/bin/bash #created by 90root #date: 20160809 date_y=$(date +%Y) date_m=$(date +%m) time ...