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. T-SQL LIKE子句 模糊查询

    MS SQL Server LIKE子句用于使用通配符运算符将值与类似值进行比较. 有两个通配符与LIKE运算符结合使用: 百分号(%) 下划线(_) 百分号表示零个,一个或多个字符. 下划线表示单个 ...

  2. nginx的location、rewrite玩法详解

      1. location正则写法 一个示例: 1234567891011121314151617181920212223242526272829303132333435363738394041424 ...

  3. oracle 11g完全彻底的卸载

    1.关闭oracle所有的服务.可以在windows的服务管理器中关闭: 2.打开注册表:regedit 打开路径: <找注册表 :开始->运行->regedit>  HKEY ...

  4. Confluence 6 增加和减少你许可证的用户数

    增加你许可证的用户数 如果你使用的用户数超过你许可证允许的用户数的话,你的 Confluence 实例将会变成为只读. 只读的意思是没有用户可以创建和编辑内容,直到你的用户数满足你的许可证需求为止. ...

  5. 关于npm 淘宝镜像 以及package.json里包的更新

    1.淘宝镜像的设置 npm config set registry https://registry.npm.taobao.org npm config set disturl https://npm ...

  6. ionic3 打包Xcode 9 Swift Language Version (SWIFT_VERSION) Ask 报错

    解决方案 选择4.0 然后报错17个,类似以下这样的错误 'AVMediaTypeVideo' has been renamed to 'AVMediaType.video' 根据提示更改 AVMed ...

  7. js中的 !! 和 ! 的区别

    简单描述:看老大提交的代码,发现了一个事情,有一处是连着两个!  然后就把我弄纳闷了,果断找度娘,原来如此.上代码 代码: if(!!s && s.length > 0){ /* ...

  8. 【python】内存相关

    1.  /proc/pid/status 可以查看进程相关的详细信息,当内存异常时可查看 参考:http://blog.csdn.net/beckdon/article/details/4849190 ...

  9. java爬虫笔记

    一.URl解释 1.URl统一资源定位符, Uniform Resource Location 也就是说是Internet上信息资源的字符串,所谓的网页抓取就是把URl地址中指定的网络资源从网络中读取 ...

  10. cf1143E 倍增好题!

    一开始感觉用莫队可以搞一下,但是看了题解才发现这题其实是倍增套路题 把排列转换成nxt数组,然后倍增dp[i][j]表示第i个数后面有(1<<j)个数的最靠左的区间 然后从右往左扫一次即可 ...