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 & ...
随机推荐
- 4. 把一幅彩色图像的R、G、B分量单独显示。
#include <cv.h> #include <highgui.h> int main(void) { IplImage* oo = cvLoadImage(); IplI ...
- columns分栏与flex弹性盒模型
columns 分栏 值:column-width:设置每列的宽度 column-count:设置列数 例:columns{200px 3} 列数和宽度固定 co ...
- Kendo UI 单页面应用(二) Router 类
Kendo UI 单页面应用(二) Router 类 Route 类负责跟踪应用的当前状态和支持在应用的不同状态之间切换.Route 通过 Url 的片段功能(#url)和流量器的浏览历史功能融合在一 ...
- ABAP日期和时间运算
"日期运算是以天为单位,时间运算以秒为单位.DATA:date1 TYPE d. "服务器当前日期date1 = sy-datum.WRITE: / date1 . "2 ...
- php 判断字符串中是否包含另一个字符串 strpos
strpos (PHP 4, PHP 5, PHP 7) strpos — 查找字符串首次出现的位置 说明 strpos ( string $haystack , $needle [, int $o ...
- 设置Cookie最大存活时间
Cookie和Session都是由Tomcat自动创建的对象,Cookie的默认最大存活时间是 -1 ,即当浏览器关闭时Cookie就消失了:Session的默认最大存活时间是30分钟. 使用Sess ...
- 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:2.技术简介之MinaFilter(1)
欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 Filter filter:过滤器?(不知道是不是这么翻译,算了知道意思就好了╮(╯▽╰)╭),这种东西在很多语言中 ...
- [LoadRunner]录制启动时报“The JVM could not be started……”错误解决方案
在LR准备点击录制java over http协议时,程序报如下错误: 报错提示是设置的JVM值设置问题,导致不能启动. 解决方案一 点击F4快捷按钮,会弹出以下界面,在选中的位置选择对应的java路 ...
- 解决ajax 遇到session失效后自动跳转的问题
在项目中,经常会遇到session失效后,点击任何链接无反应的情况!这样给客户的体验就不是很好,以为是系统出了故障!所以在项目中我们会处理session失效后的跳转问题(一般给用户提示,并跳转后登录页 ...
- C++string类型转换为C数组
#include <string> #include <iostream> using namespace std; int main(){ string str; str.a ...