题目链接 : http://codeforces.com/gym/100781/attachments

题意 :

有n个编号为0-n-1的点, 给出目前已经有的边(最多n-1条), 问如何添加最少的边, 使得整个图连通, 且其中两点间距离的最大值最小, 一条边距离为1单位

思路 :

两点间距离的情况 : 1. 子图中任意两点间距离   2.两个子图中两点间的距离

无论如何添加边对第一种的距离都没有影响, 对第二种却有影响

考虑添加一条边连通两个子图后, 最长的距离为 子图1中距离添加边的节点的最长的距离 + 子图2中距离添加边的节点的最长的距离 + 添加的1条边的距离

所以选择添加边的节点是 在某个子图中, 使所有节点到它的最长距离最小的点

但是不用算出这个点, 只需要这个距离, 这个距离就是一个子图中距离最长两点距离的一半, 即(max_length + 1) / 2

一个图中最长距离求法是在对某个子图任一节点深搜, 搜到距离最长的点, 再对这个点深搜, 记录最长距离(新技能)

最终取 : 情况1 和 情况2 距离最大值

注意情况二中, 如果存在三个子图最大(max_length + 1) / 2 都相等, 最终答案是要加2条边的距离而不是加1条, 比如0-1    1-2    3-4 这三个子图

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> using namespace std; const int MAXN = 1e5+; vector<int> edge[MAXN];
int depth[MAXN];
bool vis[MAXN];
int maxx, maxx1, mark1;
int length[MAXN]; int Max(int a, int b)
{
return a > b ? a : b;
} void Dfs(int u, int fa)
{
vis[u] = ;
depth[u] = depth[fa] + ;
if(depth[u] > maxx1) {
mark1 = u;
maxx1 = depth[u];
}
int len = edge[u].size();
for(int i = ; i < len; i++) {
int v = edge[u][i];
if(v == fa) continue;
Dfs(v, u);
}
} bool cmp(int a, int b)
{
return a > b;
} void Init(int n)
{
for(int i = ; i <= n; i++) {
edge[i].clear();
vis[i] = ;
}
maxx = ;
} int main()
{
int n, l;
int u, v; while(scanf("%d %d", &n, &l) != EOF) {
Init(n);
for(int i = ; i < l; i++) {
scanf("%d %d", &u, &v);
edge[u].push_back(v);
edge[v].push_back(u);
}
int cnt = ;
for(int i = ; i < n; i++) {
maxx1 = ;
if(vis[i] == ) {
depth[i] = -;
Dfs(i, i);
depth[mark1] = -;
if(maxx1 != ) {
Dfs(mark1, mark1);
}
length[i] = (maxx1 + ) / ;
if(maxx1 > maxx) maxx = maxx1;
cnt++;
}
}
sort(length, length+n, cmp);
if(n == ) printf("0\n");
else if(n == ) printf("1\n");
else if(n == ) printf("2\n");
else if(cnt == ) printf("%d\n", maxx);
else if(length[] == length[] && length[] == length[]) {
printf("%d\n", Max(maxx, length[] + length[] + ));
}
else {
printf("%d\n", Max(maxx, length[] + length[] + ));
}
} return ;
}

NCPC 2015 - Problem A - Adjoin the Networks的更多相关文章

  1. NCPC 2015 October 10, 2015 Problem D

    NCPC 2015Problem DDisastrous DowntimeProblem ID: downtimeClaus Rebler, cc-by-saYou’re investigating ...

  2. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem K. UTF-8 Decoder 模拟题

    Problem K. UTF-8 Decoder 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c702 ...

  3. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem I. Alien Rectangles 数学

    Problem I. Alien Rectangles 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c ...

  4. dp - Google Code jam Qualification Round 2015 --- Problem B. Infinite House of Pancakes

    Problem B. Infinite House of Pancakes Problem's Link:   https://code.google.com/codejam/contest/6224 ...

  5. Google Code jam Qualification Round 2015 --- Problem A. Standing Ovation

    Problem A. Standing Ovation Problem's Link:   https://code.google.com/codejam/contest/6224486/dashbo ...

  6. Google Code Jam Round 1C 2015 Problem A. Brattleship

    Problem You're about to play a simplified "battleship" game with your little brother. The ...

  7. Google Code Jam Round 1A 2015 Problem B. Haircut 二分

    Problem You are waiting in a long line to get a haircut at a trendy barber shop. The shop has B barb ...

  8. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem H. Parallel Worlds 计算几何

    Problem H. Parallel Worlds 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c7 ...

  9. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem F. Turning Grille 暴力

    Problem F. Turning Grille 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c70 ...

随机推荐

  1. Oracle Quality --- Setup Collection Element and Collection Plan

    Responsibility: Quality, Vision Enterprises 第一步: 创建 Collection Elements setup > collection elemen ...

  2. Android-语言设置流程分析

    Android手机语言切换行为,是通过设置-语言和输入法-语言来改变手机的语言,其实这个功能很少被用户使用.     以Android5.1工程源码为基础,从设置app入手来分析和学习语言切换的过程: ...

  3. Spring Remoting by HTTP Invoker Example--reference

    Spring provides its own implementation of remoting service known as HttpInvoker. It can be used for ...

  4. 最全的C#图片处理帮助类ImageHelper

    最全的C#图片处理帮助类ImageHelper.cs 方法介绍: 生成缩略图 图片水印处理方法 图片水印位置处理方法 文字水印处理方法 文字水印位置的方法 调整光暗 反色处理 浮雕处理 拉伸图片 滤色 ...

  5. VS2013默认快捷键

    目录: Global 分析 调试器上下文菜单 体系结构 调试器上下文菜单 生成 诊断中心 类视图上下文菜单 Edit 调试 编辑器上下文菜单 文件 项目和解决方案上下文菜单 帮助 重构 负载测试 解决 ...

  6. 告诉你GetDC()没有释放造成的后果

    最近做的项目中需要显示视频监控窗口,从采集卡中读到图像的数据,需要实时显示出来,而且速度比较快. 由于比较简单,就直接使用了GDI画图,以前复杂的都用openGL啥的工具了,这次这个简单,就直接用GD ...

  7. 关于<%@ include file=" " %>与<jsp:include page=""></jsp:include>中的那些问题?

    今天在使用<%@ include file=" " %>指令时,竟然在页面中不让使用?这是怎么回事:问题如下图: 顿时被这个问题给搞到了!!!突然想到在以前的 JSP ...

  8. C++拾遗(十二)C++代码重用

    “has-a”关系 通常有两种方法实现: 1.被包含,本身是另一个类的对象. 2.私有或者保护继承. 主要讨论第二种方法,在继承时使用private关键字(或者不用任何关键字,默认就是私有的). 使用 ...

  9. static和const关键字的作用

    static关键字至少有下列n个作用: (1)函数体内static变量的作用范围为该函数体,不同于auto变量,该变量的内存只被分配一次,因此其值在下次调用时仍维持上次的值: (2)在模块内的stat ...

  10. android WIFI的一些属性

    package com.example.wifitest; import java.util.List; import android.content.Context; import android. ...