题意:给出一张图,存在多棵树,你要把这些树连起来,形成一棵树,令这棵树的直径最小。

解法:很容易想到贪心一下就行了,找出直径最长的树的根作为连起来后的树的根,其他树的根连到这个根即可。然后求一下树的直径。难点在于怎么规定一个树的根,使得这棵树的深度最小。下面写的是类似于LCA两个端点向上走的方法,有dalao告诉我可以树形dp+dfs找。这都是可以的。但是注意:树的重心并不一定是这个最优的根。

代码如下:

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<stack>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
using namespace std;
typedef pair <int, int> pii;
const int maxn = 1e5 + 5;
int head[maxn], to[maxn << 1], nx[maxn << 1], tot;
bool vst[maxn];
int Min = 0x3f3f3f3f, key, dis[maxn], pre[maxn];
int n, l, mycnt;
int path1[maxn], path2[maxn], path[maxn], cnt1 = 0, cnt2 = 0, cnt = 0;void add_edge(int u, int v) {to[tot] = v, nx[tot] = head[u], head[u] = tot++;swap(u, v);to[tot] = v, nx[tot] = head[u], head[u] = tot++;
}vector <pii> vec;
int Max_dis;void dfs(int u, int par, int _dis) {vst[u] = 1; pre[u] = par;mycnt++;dis[u] = _dis;for(int i = head[u]; ~i; i = nx[i]) {int v = to[i];if(v != par) {dfs(v, u, _dis + 1);}}if(_dis > Max_dis) {key = u;Max_dis = _dis;}
}int cal(int x, int y) {cnt1 = cnt2 = cnt = 0;while (dis[x] > dis[y]) {path1[cnt1++] = x; x = pre[x];}while (dis[y] > dis[x]) {path2[cnt2++] = y; y = pre[y];}while (x != y) {path1[cnt1++] = x; x = pre[x];path2[cnt2++] = y; y = pre[y];}for (int i = 0; i < cnt1; ++i) {  path[cnt++] = path1[i];}path[cnt++] = x;for (int i = cnt2 - 1; i >= 0; --i) {  path[cnt++] = path2[i];}
//  for (int i = 0; i < cnt; ++i) {
//      printf("%d ", path[i]);
//  }
//  printf("\n");return path[cnt / 2];
}int main() {
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);
//  freopen("out.txt", "w", stdout);
#endifmemset(head, -1, sizeof(head));tot = 0;scanf("%d%d", &n, &l);for(int i = 0, u, v; i < l; i++) {scanf("%d%d", &u, &v);add_edge(u, v);}for(int i = 0; i < n; i++) {if(!vst[i]) {Max_dis = -1; int t1, t2, t3;dfs(i, i, 0);Max_dis = -1;mycnt = 0;t1 = key; dfs(key, key, 0); t2 = key; t3 = cal(t1, t2);if(mycnt != 1)vec.push_back(pii(cnt / 2, t3));elsevec.push_back(pii(0, i));}}sort(vec.begin(), vec.end());int root = vec[vec.size() - 1].second;for(int i = vec.size() - 2; i >= 0; i--) {add_edge(root, vec[i].second);}Max_dis = -1;dfs(0, 0, 0);Max_dis = -1;dfs(key, key, 0);printf("%d\n", Max_dis);return 0;
}

CodeFroces gym 100781 A.Adjoin the Networks(贪心)相关推荐

  1. Pytorch Note 快乐星球

    Pytorch Note 什么是快乐星球,让我用简单易懂的代码带你进入pytorch快乐星球 这是我的Pytoch学习笔记,下面会慢慢的更新我的学习笔记 part1: 深度学习基础 PyTorch介绍 ...

  2. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  3. 【贪心】【字典树】Gym - 101466A - Gaby And Addition

    题意:定义一种无进位加法运算,给你n个正整数,问你取出两个数,使得他们加起来和最大/最小是多少. 无进位加法运算,其实是一种位运算,跟最大xor那个套路类似,很容易写出对于每个数字,其对应的最优数字是 ...

  4. Gym - 101471D Money for Nothing(决策单调性+分治+贪心)

    题目链接:点击查看 题目大意:在二维平面中给出 n 个点可以作为矩形左下角的点,再给出 m 个点可以作为矩形右上角的点,现在问最大可以构造出多大面积的矩形,即如何选择,可以使得 ( b[ j ] . ...

  5. Gym - 101291I Mismatched Socks(贪心)

    题目: Fred likes to wear mismatched socks. This sometimes means he has to plan ahead. Suppose his sock ...

  6. 【Gym 102893 L】The Firm Knapsack Problem (贪心)

    题目链接 题目大意 一个 01 背包问题,物品数 n≤105n\le 10^5n≤105 ,容量 W≤1012W\le 10^{12}W≤1012 .将体积上限放宽到 32W\frac{3}{2}W2 ...

  7. codeforce Gym 100685E Epic Fail of a Genie(MaximumProduction 贪心)

    题意:给出一堆元素,求一个子集,使子集的乘积最大,如有多个,应该使子集元素个数尽量小. 题解:贪心,如果有大于1的正数,那么是一定要选的,注意负数也可能凑出大于1的正数,那么将绝对值大于1的负数两两配 ...

  8. Gym - 101911B Glider 贪心

    A plane is flying at a constant height of hh meters above the ground surface. Let's consider that it ...

  9. B - Glider Gym - 101911B (贪心)

    A plane is flying at a constant height of hh meters above the ground surface. Let's consider that it ...

  10. 【2018icpc宁夏邀请赛现场赛】【Gym - 102222H】Fight Against Monsters(贪心排序)

    题干: It is my great honour to introduce myself to you here. My name is Aloysius Benjy Cobweb Dartagna ...

最新文章

  1. 请收下这份NLP热门词汇解读
  2. 【Android 逆向】使用 DB Browser 查看并修改 SQLite 数据库 ( 下载 DB Browser 安装包 | 安装 DB Browser 工具 )
  3. PE文件格式--------------导出表
  4. mysql统计age大于20的数_数据库命令记录
  5. eclipse 安装python开发工具 PyDev
  6. java in array_ArrayList to Array Conversion in Java
  7. VirtualBox中增强工具的安装
  8. mysql的下载及安装(windows)
  9. C语言除法向上、向下取整
  10. 数据库表结构设计方法及原则
  11. 刷题——必备十二大网站
  12. Excel表格打印时不打印标记填充颜色
  13. Handler的理解、用法以及运行机制原理
  14. 上拉电阻的作用原理_电容触摸屏原理以及敦泰TP FT5X06驱动
  15. 自然数因式分解最小和
  16. python读取word文档并做简单的批量文档筛选
  17. 定位器百科:老人、小孩的GPS定位器是如何工作的
  18. 从普朗克黑体辐射定律到真正的黑
  19. Django cms 教程三:创建模板
  20. 熬夜对身体造成多种的损害

热门文章

  1. White Sheet(面积法)
  2. 批量Word转换成PDF,用这方法超简单
  3. Redis的使用场景及其介绍
  4. 海外邮件收发阻碍多?【企业邮箱怎么申请】
  5. 37、T5L迪文屏C51开发之绘制2D形状
  6. 工业级光纤收发器 百兆单模双纤内电/光电转换器/光钎收发 耐高温
  7. 剪辑视频时PR播放卡顿不连贯|如何修复Premiere软件中播放太卡问题
  8. C#反射Activator
  9. 自制简单的诗歌搜索系统
  10. 复习330+天,我总结了一份对大多数人都适用的复习经验