题意:

n个点,m条边,m <= n <= 100000,边的长度都为1。

点从 0 ~ n-1 编号。开始时图是不连通的,并且没有环。

通过加入一些边后,可以使图连通。要求加入的边不能多余(即生成的图是一棵树)。

问连通后的图,任意两点之间的距离的最大值,最小可以是多少?

既然刚开始图不连通也无环,那么就是一些树(特殊情况是点)。

于是题目就变成了,如何把很多棵树连起来,使最后生成的树直径最小。

可以想到,如果把两棵直径为 a 和 b 的树加一条边连成一棵,那么直径最小的新树直径为 (a+1)/2 + (b+1)/2 + 1 (两棵树的直径 / 2,向上取整,的和再加 1)

选择其中一个直径最大的子树,遍历所有其他的子树,然后将其他的子树加到这个子树上面。

求树的直径可以用DP。

这道题场上很快想出来了做法。然后一直T在 test 22。

原因是不会写树的直径DP求法,以及,memset超时。

每次找到新的联通块(新的一棵树)求树的直径就要memset一遍是不需要的。因为那些点肯定是不冲突的。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map> using namespace std;
const int maxn = + ; int fa[maxn], dis[maxn], tot = , v[maxn];
bool vis[maxn]; struct Node
{
int v, next, last, z;
}a[maxn]; void build(int x, int y)
{
tot++;
a[tot].v = y;
a[tot].next = a[x].last;
a[x].last = tot;
} void dp(int x, int &ans)
{
v[x] = ;
for (int tmp = a[x].last; tmp; tmp = a[tmp].next)
{
int y = a[tmp].v;
if (v[y]) continue;
dp(y, ans);
ans = max(ans, dis[x] + dis[y] + );
dis[x] = max(dis[x], dis[y] + );
}
} void DFS(int x)
{
vis[x] = true;
for (int tmp = a[x].last; tmp; tmp = a[tmp].next)
{
int y = a[tmp].v;
if (!vis[y]) DFS(y);
}
} int main()
{
int n, m;
scanf("%d%d", &n, &m); int x, y;
for (int i = ; i <= m; i++)
{
scanf("%d%d", &x, &y);
build(x, y);
build(y, x);
} memset(vis, false, sizeof(vis)); memset(v, , sizeof(v));
memset(dis, , sizeof(dis));
int q[maxn], tt = ;
for (int i = ; i < n; i++)
if (!vis[i])
{
int t = ;
dp(i, t);
q[++tt] = t;
DFS(i);
} int ans = , flag = ;
for (int i = ; i <= tt; i++)
if (q[i] > ans)
{
ans = q[i];
flag = i;
} for (int i = ; i <= tt; i++)
if (i != flag)
ans = max(ans, (q[i]+)/+(ans+)/ + );
printf("%d\n", ans); return ;
}

Gym - 100781A Adjoin the Networks (树的直径)的更多相关文章

  1. 【DFS】Gym - 100781A - Adjoin the Networks

    给你一个森林,让你把它连接成一颗树,使得直径最小. 就求出每颗树的重心以后,全都往直径最大的那个的重心上连,一般情况是最大/2+次大/2+1,次大/2+第三大/2+2 中取较大者. 还有些特殊情况要特 ...

  2. codeforces GYM 100781A【树的直径】

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

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

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

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

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

  5. Gym - 100676H Capital City(边强连通分量 + 树的直径)

    H. Capital City[ Color: Black ]Bahosain has become the president of Byteland, he is doing his best t ...

  6. Gym - 100676H H. Capital City (边双连通分量缩点+树的直径)

    https://vjudge.net/problem/Gym-100676H 题意: 给出一个n个城市,城市之间有距离为w的边,现在要选一个中心城市,使得该城市到其余城市的最大距离最短.如果有一些城市 ...

  7. CF GYM 100781A(菊花图+直径)

    题目大意 给出若干颗树用最少的边把它们连成一个无向连通图,同时使图的直径最小.输出最小直径. 题解 我们定义树的半径为(树的直径+1)/2.符合题意的连接方式为.所有树的“中点”连在直径最长的树的中点 ...

  8. hiho 1050 树的直径

    #1050 : 树中的最长路 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中, ...

  9. 牡丹江.2014B(图论,树的直径)

    B - Building Fire Stations Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%lld & ...

随机推荐

  1. High waits on control file sequential read

    High waits on control file sequential read (文档 ID 2277867.1) In case we run into an issue where cont ...

  2. spring assert 用法

    spring在提供一个强大的应用开发框架的同时也提供了很多优秀的开发工具类,合理的运用这些工具,将有助于提高开发效率.增强代码质量.下面就最常用的Assert工具类,简要介绍一下它的用法.Assert ...

  3. Spring Cloud 熔断器

    目录 Spring Cloud 熔断器 Hystrix ribbon中使用hystrix feign中使用hystrix Spring Cloud 熔断器 在微服务架构中,根据业务来拆分成一个个的服务 ...

  4. c#基础 base和this的区别,在继承上面

    base public Person(string name, int age, char gender) { this.Name = name; this.Age = age; this.Gende ...

  5. Ext2文件系统的特点

    1.文件更新策略的谨慎实现将系统崩溃的影响减到最少.我们只举一个例子来体现这个优点:例如,当给文件创建一个硬链接时,首先增加磁盘索引节点中 的硬链接计数器,然后把这个新的名字加到合适的目录中.在这种方 ...

  6. ajax请求拿到多条数据拼接显示在页面中

    首先我们拿到的了一坨Json数据 如下 然后通过ajax请求拿到数据 在ajax的success方法中处理和使用数据: 其中包括: 用eval处理这种数据 var outStr = eval('('+ ...

  7. Windows使用MySQL数据库管理系统中文乱码问题

    声明:本文关于MySQL中文乱码问题的解决方案均基于Windows 10操作系统,如果是Linux系统会有较多不适用之处,请谨慎参考. 一.MySQL中文乱码情况 1. sqlDevelper远程登陆 ...

  8. windows系统下Eclipse启动界面更改

    前段日子看到有人修改了linux系统下Eclipse的启动界面,因此自己试着修改了一下windows平台的启动界面.本文总结一下修改Eclipse 4.5(代号Mars)启动界面的方法. 方法一:修改 ...

  9. ActiveX、OLE和COM/DCOM

    ActiveX:开放的集成平台 为开发人员. 用户和 Web生产商提供了一个快速而简便的在 Internet 和 Intranet 创建程序集成和内容的方法(就是提供了一个方法). 使用 Active ...

  10. (四)SpringMVC之使用cookie实现记住密码的功能

    注意:因为实现记住密码的功能需要用到json,所以需要加上这条语句: <script type="text/javascript" src="scripts/jqu ...