Gym - 100781A Adjoin the Networks (树的直径)
题意:
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 (树的直径)的更多相关文章
- 【DFS】Gym - 100781A - Adjoin the Networks
给你一个森林,让你把它连接成一颗树,使得直径最小. 就求出每颗树的重心以后,全都往直径最大的那个的重心上连,一般情况是最大/2+次大/2+1,次大/2+第三大/2+2 中取较大者. 还有些特殊情况要特 ...
- codeforces GYM 100781A【树的直径】
先求出每棵树的直径,排个序,要想图的直径最小的话需要每棵树的直径中点像直径最大的树的直径中点连边,这样直径有三种情况:是直径最大的树的直径:a[tot]:是直径最大的树和直径第二大的树的半径拼起来+1 ...
- 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 - 100676H Capital City(边强连通分量 + 树的直径)
H. Capital City[ Color: Black ]Bahosain has become the president of Byteland, he is doing his best t ...
- Gym - 100676H H. Capital City (边双连通分量缩点+树的直径)
https://vjudge.net/problem/Gym-100676H 题意: 给出一个n个城市,城市之间有距离为w的边,现在要选一个中心城市,使得该城市到其余城市的最大距离最短.如果有一些城市 ...
- CF GYM 100781A(菊花图+直径)
题目大意 给出若干颗树用最少的边把它们连成一个无向连通图,同时使图的直径最小.输出最小直径. 题解 我们定义树的半径为(树的直径+1)/2.符合题意的连接方式为.所有树的“中点”连在直径最长的树的中点 ...
- hiho 1050 树的直径
#1050 : 树中的最长路 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中, ...
- 牡丹江.2014B(图论,树的直径)
B - Building Fire Stations Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & ...
随机推荐
- ADC中的滤波算法
STM32的AD最大输入时钟不超过14MHZ,最高采样速度1us,可以采用DMA或者内部的基本定时器/高级定时器来触发,利用模拟看门狗监控所选择的的所有通道,如果超过模拟的 阀[fá] 值,将产生中断 ...
- 《java学习三》jvm性能优化-------调优
常见参数配置 -XX:+PrintGC 每次触发GC的时候打印相关日志 -XX:+UseSerialGC 串行回收 -XX:+PrintGCDetails 更详细的GC日志 -X ...
- 单个页面Request编码方式的改变,无需改动Web.config~
搞一个东西,从别人的接口接一段中文,URL传输,怎么都有乱码~~ 得到对方的编码方式是gb2312,于是用HttpUtility.UrlDecode(_smssend_content, System. ...
- 【部分补充】【翻译转载】【官方教程】Asp.Net MVC4入门指南(4):添加一个模型
4. 添加一个模型 · 原文地址:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-mo ...
- 搭建本地SVN資料
基于網上眾多教程,搭建SVN成功:VisualSVN Server + TortoiseSVN Client. 過程比較簡單,就不重複書寫了. 部份參考資料,感謝作者: 什麽是SVN及如何應用 htt ...
- copyout函数
copyout Kernel Service Purpose Copies data between user and kernel memory. Syntax #include <sys ...
- WinForm 公共控件和属性
Button 按钮 布局 AutoSize 内容超出部分是否扩展到适应尺寸大小 Location 位置坐标 Size 控件大小 行为 Enabled 控件是否启用 visible 控件 ...
- JAVA多线程编程——JAVA内存模型
一.何为“内存模型” 内存模型描述了程序中各个变量(实例域.静态域和数组元素)之间的关系,以及在实际计算机系统中将变量存储到内存和从内存中取出变量这样的底层细节,对象最终是存储在内存里面的,但是编译器 ...
- Mandelbrot图像
using System;using System.Collections.Generic;using System.Text; namespace ConsoleApplication3{ ...
- ios常见错误之 Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?
Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the desi ...