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. 【ORIGINATE】详解

    originate 用法如下: originate <call url> <exten> |&<application_name>(<app_args ...

  2. codecs and formats of digital media

    A codec is a device or software that is used to compress or decompress a digital media file, such as ...

  3. Laravel - Union + Paginate at the same time? and another problem----1222 The used SELECT statements have a different number of columns (SQL: (select count(*) as aggregate from

    ### 这是这几天,碰到的一个比较头疼的问题 使用union all联合查询,同时laravel 生成分页,但发生报错? QueryException : SQLSTATE The used from ...

  4. oracle 新增一条数据时设置id自增

    CREATE SEQUENCE test_seq    --创建一个test_seq序列INCREMENT BY 1   --每次加1START WITH 1     --从1开始NOMAXVALUE ...

  5. Python下划线的详解

    本文将讨论Python中下划线(_)字符的使用方法.我们将会看到,正如Python中的很多事情,下划线的不同用法大多数(并非所有)只是常用惯例而已. 单下划线(_) 通常情况下,会在以下3种场景中使用 ...

  6. hive学习02-累加

    求出当月的访问次数,截至当月前的每个月最大访问次数.截至当月前每个用户总的访问次数. 数据表如下 A,-, A,-, B,-, A,-, B,-, A,-, A,-, A,-, B,-, B,-, A ...

  7. python 爬虫简化树状图

  8. nginx官方模块之http_sub_status_module

    作用 显示nginx的连接状态,nginx客户端状态 配置语法 配置

  9. ios集成极光推送:Undefined symbols for architecture arm64: "_dns_parse_resource_record", referenced from:?

    添加libresolv.tbd库,即可解决问题 Undefined symbols for architecture arm64: "_dns_parse_resource_record&q ...

  10. django----多对多数据添加

    class UserInfo(models.Model): """ 用户表:既有班主任也有老师 """ username = models. ...