Codeforces 1092E Minimal Diameter Forest
首先我们找出每个连通块中的特殊点, 特殊点的定义是到各种个连通块中距离的最大值最小的点,
每个连通块肯定通过特殊点连到其他连通块, 我们把有最大值的特殊点当作根, 然后其他点直接接在这个点中, 形成菊花图。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, m, cnt, mx, DIA, belong[N], mn[N], who[N], id[N], dia[N];
vector<int> G[N]; void dfs(int u) {
belong[u] = cnt;
for(auto& v : G[u])
if(!belong[v]) dfs(v);
}
void dfs2(int u, int fa, int depth) {
mx = max(mx, depth);
for(auto& v : G[u])
if(v != fa) dfs2(v, u, depth + );
} bool cmp(const int& x, const int& y) {
return mn[x] > mn[y];
} int main() {
memset(mn, inf, sizeof(mn));
scanf("%d%d", &n, &m);
for(int i = ; i <= m; i++) {
int u, v; scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
for(int i = ; i <= n; i++) {
if(!belong[i]) ++cnt, dfs(i);
}
for(int i = ; i <= n; i++) {
mx = ;
dfs2(i, , );
if(mx < mn[belong[i]]) {
mn[belong[i]] = mx;
who[belong[i]] = i;
}
dia[belong[i]] = max(dia[belong[i]], mx);
}
for(int i = ; i <= cnt; i++) {
id[i] = i;
DIA = max(DIA, dia[i]);
}
sort(id + , id + + cnt, cmp);
if(cnt >= ) DIA = max(DIA, mn[id[]] + mn[id[]] + );
if(cnt >= ) DIA = max(DIA, mn[id[]] + mn[id[]] + );
printf("%d\n", DIA);
for(int i = ; i <= cnt; i++) printf("%d %d\n", who[id[]], who[id[i]]);
return ;
} /*
*/
Codeforces 1092E Minimal Diameter Forest的更多相关文章
- Codeforces 797C - Minimal string
C. Minimal string 题目链接:http://codeforces.com/problemset/problem/797/C time limit per test 1 second m ...
- Codeforces 804D Expected diameter of a tree
D. Expected diameter of a tree time limit per test 3 seconds memory limit per test 256 megabytes inp ...
- codeforces 501C. Misha and Forest 解题报告
题目链接:http://codeforces.com/problemset/problem/501/C 题目意思:有 n 个点,编号为 0 - n-1.给出 n 个点的度数(即有多少个点跟它有边相连) ...
- codeforces 755C. PolandBall and Forest
C. PolandBall and Forest time limit per test 1 second memory limit per test 256 megabytes input stan ...
- CodeForces 797C Minimal string:贪心+模拟
题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...
- Codeforces 840D Expected diameter of a tree 分块思想
Expected diameter of a tree 我们先两次dfs计算出每个点能到达最远点的距离. 暴力计算两棵树x, y连边直径的期望很好求, 我们假设SZ(x) < SZ(y) 我们枚 ...
- Codeforces 825E Minimal Labels - 拓扑排序 - 贪心
You are given a directed acyclic graph with n vertices and m edges. There are no self-loops or multi ...
- Codeforces 804D Expected diameter of a tree(树形DP+期望)
[题目链接] http://codeforces.com/contest/804/problem/D [题目大意] 给你一个森林,每次询问给出u,v, 从u所在连通块中随机选出一个点与v所在连通块中随 ...
- Codeforces 825E - Minimal Labels
825E - Minimal Labels 题意 给出 m 条有向边,组成有向无环图,输出一个 1 到 n 组成的排列,每个数只能出现一次,表示每个点的标号.如果有边 \((u, v)\) 那么 \( ...
随机推荐
- centos7 docker使用https_proxy 代理配置
centos7 docker使用https_proxy 代理配置 背景: 内网的centos主机不能上网,通过同网段的windows设置代理上网,yum.conf配置http代理是可以的,但是dock ...
- mysql alter add 使用记录
alter add命令用来增加表的字段. alter add命令格式:alter table 表名 add字段 类型 其他; 例如,在表MyClass中添加了一个字段passtest,类型为int(4 ...
- Keil和SourceInsight中文乱码解决方法
一.KEIL乱码 到菜单栏Edit--->Configuration-->Encoding ---ChineseSimplied 二.SourceInsight乱码 错误现象:注释乱码,查 ...
- Codeforces 446C DZY Loves Fibonacci Numbers [线段树,数论]
洛谷 Codeforces 思路 这题知道结论就是水题,不知道就是神仙题-- 斐波那契数有这样一个性质:\(f_{n+m}=f_{n+1}f_m+f_{n}f_{m-1}\). 至于怎么证明嘛-- 即 ...
- 我不是机器人:谷歌最新版验证码系统ReCaptcha破解已开源
选自 Github 作者:George Hughey 机器之心编译 每个人都讨厌验证码,这些恼人的图片中包含你必须输入的字符,我们只有正确地填写才能继续访问网站.验证码旨在确认访问者是人还是程序,并防 ...
- tensorflow中文教程
飞机票--->走你~~~ 飞机票 http://blog.csdn.net/lenbow/article/details/52152766
- Swift中 @objc 使用介绍
在swift 中 如果一个按钮添加点击方法 如果定义为Private 或者 定义为 FilePrivate 那么会在Addtaget方法中找不到私有方法 但是又不想把方法暴露出来,避免外界访问 ,那 ...
- ES6之Set与Map加深理解
Set 类似于数组,但是成员的值都是唯一的,没有重复的值,有序. Set函数可以接受一个数组(或者具有 iterable 接口的其他数据结构)作为参数,用来初始化. 用途 数组去重: [...new ...
- lightoj 1282 取对数的操作
/* 前三位 len=log10n^k(乘积的长度) len=klog10n n^k=x*10^(len-1) x=n^k/10^(len-1) log10x = k*log10n - (len-1) ...
- Vue注意事项及用得较多的属性归纳
1.prop是一个对象而不是字符串数组时,它包含验证要求.props: { // 基础类型检测 (`null` 意思是任何类型都可以) propA: Number, // 多种类型 propB: [S ...