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的更多相关文章

  1. Codeforces 797C - Minimal string

    C. Minimal string 题目链接:http://codeforces.com/problemset/problem/797/C time limit per test 1 second m ...

  2. 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 ...

  3. codeforces 501C. Misha and Forest 解题报告

    题目链接:http://codeforces.com/problemset/problem/501/C 题目意思:有 n 个点,编号为 0 - n-1.给出 n 个点的度数(即有多少个点跟它有边相连) ...

  4. codeforces 755C. PolandBall and Forest

    C. PolandBall and Forest time limit per test 1 second memory limit per test 256 megabytes input stan ...

  5. CodeForces 797C Minimal string:贪心+模拟

    题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...

  6. Codeforces 840D Expected diameter of a tree 分块思想

    Expected diameter of a tree 我们先两次dfs计算出每个点能到达最远点的距离. 暴力计算两棵树x, y连边直径的期望很好求, 我们假设SZ(x) < SZ(y) 我们枚 ...

  7. Codeforces 825E Minimal Labels - 拓扑排序 - 贪心

    You are given a directed acyclic graph with n vertices and m edges. There are no self-loops or multi ...

  8. Codeforces 804D Expected diameter of a tree(树形DP+期望)

    [题目链接] http://codeforces.com/contest/804/problem/D [题目大意] 给你一个森林,每次询问给出u,v, 从u所在连通块中随机选出一个点与v所在连通块中随 ...

  9. Codeforces 825E - Minimal Labels

    825E - Minimal Labels 题意 给出 m 条有向边,组成有向无环图,输出一个 1 到 n 组成的排列,每个数只能出现一次,表示每个点的标号.如果有边 \((u, v)\) 那么 \( ...

随机推荐

  1. Zabbix3.2监控Windows的内存使用百分比并在内存使用率超过85%的时候触发报警

    内存使用率key:vm.memory.size[pused]

  2. nginx 403 forbidden

    2018年3月9日14:11:59 总结一下: 1. 查看目录或者文件是否是可读可执行 2. 查看nginx配置的server -> location -> index指令, 看其列出的入 ...

  3. event & signals & threads

    The Event Systemhttp://doc.qt.io/qt-4.8/eventsandfilters.html Each thread can have its own event loo ...

  4. CF 313B

    题意: 给你一个字符串, 然后m次区间查询,求出区间有多少组 str[i] == str[i+1] 就是一个水DP了 有则Dp[i] = Dp[i-1] 无则 Dp[i] = Dp[i-1]: (刚开 ...

  5. awk-for循环简单用法

    文本: [root@VM_0_84_centos ~]# cat sshd.txt 1 2 3 4 5 6 7 8 9 循环打印上述文本 for 循环的固定格式   i=1设置i的初始变量  i< ...

  6. 高级UI特效—用SVG码造一个精美的中国地图

    前言 来继续学习SVG,要想深入了解还是要多动手进行实战.关于svg基础可以去看一下我的上一篇文章<SVG前戏—让你的View多姿多彩>,今天就用SVG打造一个精美的UI效果. 正文 先上 ...

  7. 2018谷歌I/O开发者大会8大看点汇总 新品有哪些

    2018谷歌I/O开发者大会8大看点汇总 新品有哪些美国科技媒体The Verge近日撰文,列举了在即将召开的2018年谷歌I/O开发者大会上的8大看点,包括Android P.人工智能等等. 以下为 ...

  8. iOS 在 程序内调用手机上安装的地图软件进行导航

    // 需求是需要用户 能从 所在位置 到 附近的健身房的 路线, 然而,就一个需求,不值当的添加一个地图, 就用调用手机上第三方地图软件,  什么高德, 百度, 腾讯, iOS 原生地图都可以, 如果 ...

  9. Windows下Oracle 11g安装以及创建数据库

    安装数据库 事实上Oracle安装 1.安装准备 Oracle的安装包下载以后是两个压缩包,同时选中两个压缩包右击进行解压 2.解压完成如下图所示 3.双击 setup.exe 文件进行安装,会弹出以 ...

  10. Confluence 6 当前使用的数据库状态

    进入  > 基本配置(General Configuration) > 问题检查和支持工具(Troubleshooting and support tools) 你就可以看到当前使用的数据 ...