$ \rightarrow $ 戳我進CF比賽頁面

Problem A Adjoin the Networks Problem ID: adjoin

 

One day your boss explains to you that he has a bunch of computer networks
that are currently unreachable from each other,
and he asks you, the cable expert’s assistant, to adjoin the networks to each other using new cables.
Existing cables in the network cannot be touched.
 
He has asked you to use as few cables as possible,
but the length of the cables used does not matter to him,
since the cables are optical and the connectors are the expensive parts.
Your boss is rather picky on cable usage, so you know that the already existing networks have as few cables as possible.
 
Due to your humongous knowledge of computer networks,
you are of course aware that the latency for an information packet travelling
across the network is proportional to the number of $ hops $ the packet needs,
where a hop is a traversal along a single cable.
And since you believe a good solution to your boss’ problem may earn you that long wanted promotion,
you decide to minimise the maximum number of hops needed between any pair of network nodes.
 

Input

On the first line, you are given two positive integers,
the number $ 1 ≤ c ≤ 10^5 $ of computers and the number $ 0 ≤ l ≤ c−1 $ of existing cables.
Then follow $ l $ lines, each line consisting of two integers $ a $ and $ b $ , the two computers the cables connect.
You may assume that every computer has a unique name between $ 0 $ and $ n−1 $ .
 

Output

The maximum number of hops in the resulting network.
 

Sample Input 1

 6 4 0 1 0 2 3 4 3 5

Sample Output 1

3

 

題目大意

  • 給出若干棵樹,用最少的邊把它們連成一張無向連通圖,同時使圖的直徑最小

  • $ N \le 100000 $

 

題解

  • 求出每棵樹的直徑,其半徑定義爲(直徑+1)/2

  • 把“其他的樹的直徑中點”連到“半徑最大的樹的直徑中點”上即可

  • 根據連接情況計算出圖的直徑

 

代碼

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct edge{ int v,nxt; }e[200010];
int s,len,n,m,cnt,head[100010],tot,ans[100010];
bool vis[100010];
void add(int u,int v){ e[++tot].v=v; e[tot].nxt=head[u]; head[u]=tot; }
void dfs(int u,int fa,int sum){vis[u]=1;if(sum>len){ len=sum; s=u; }for(int i=head[u];i;i=e[i].nxt)if(e[i].v!=fa) dfs(e[i].v,u,sum+1);
}
int main(){scanf("%d %d",&n,&m);for(int u,v,i=1;i<=m;++i){scanf("%d %d",&u,&v);add(u,v); add(v,u);}for(int i=0;i<n;++i)if(!vis[i]){len=0; s=i;dfs(i,-1,0);len=0;dfs(s,-1,0);ans[++cnt]=len;}sort(ans+1,ans+1+cnt);if(cnt>=3)printf("%d",max(ans[cnt],max((ans[cnt]+1)/2+(ans[cnt-1]+1)/2+1,(ans[cnt-1]+1)/2+(ans[cnt-2]+1)/2+2)));else if(cnt==2) printf("%d",max(ans[2],(ans[1]+1)/2+(ans[2]+1)/2+1));else printf("%d",ans[1]);return 0;
}
/*
#        40083758
When     2018-07-08 14:36:33
Who      PotremZ
Problem  A - Adjoin the Networks
Lang     GNU C++
Verdict  Accepted
Time     61 ms
Memory   2500 KB
*/

转载于:https://www.cnblogs.com/PotremZ/p/CF_GYM100781A.html

codeforces NCPC2015 GYM 100781A Adjoin the Networks 圖的直徑相关推荐

  1. codeforces第一次GYM经历

    昨天参加了codeforces的GYM,从21.10--2.10,因为寝室会定时断电,所以我和队友只好在实验室打(同时也 是第一次在实验室住的经历,汗..). a题很快就水过了,发现J题有人过,就去看 ...

  2. [EmguCV|C#]使用CvInvoke自己繪製色彩直方圖-直方圖(Hitsogram)系列(4)

    2014-02-06 10325 0 C# 檢舉文章 過年結束了,雖然還是學生所以其實還有兩個禮拜的假期,不過為了不讓自己發慌,趁著假期多利用充實自己,所以提早回到開工狀態,而這次總算要把一直說的自己 ...

  3. 【Codeforces】Gym 101173B Bipartite Blanket 霍尔定理+状压DP

    题意 给一张$n\times m$二分图,带点权,问有多少完美匹配子集满足权值和大于等于$t$ 这里有一个结论:对于二分图$\mathbb{A}$和$\mathbb{B}$集合,如果子集$A \in ...

  4. Gym101521GHIJKL-----La Salle-Pui Ching Programming Challenge 培正喇沙編程挑戰賽 2016

    La Salle-Pui Ching Programming Challenge 培正喇沙編程挑戰賽 2016 文章目录 La Salle-Pui Ching Programming Challeng ...

  5. 无向图的直径以及树的直径

    转载:http://www.csie.ntnu.edu.tw/~u91029/Path3.html 在一張無向圖上面,給定圖上一點,以最短路徑長度當作距離,找出離此點最遠的一點,這兩點之間的距離就叫做 ...

  6. Codeforces Gym 100513G G. FacePalm Accounting 暴力

    G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...

  7. Codeforces Gym 100269 Dwarf Tower (最短路)

    题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game na ...

  8. Codeforces Gym 100676G Training Camp 状压dp

    http://codeforces.com/gym/100676 题目大意是告诉你要修n门课,每门课有一个权值w[i], 在第k天修该课程讲获得k*w[i]的学习点数,给出了课程与先修课程的关系,要修 ...

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

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

  10. Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset

    Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

最新文章

  1. R语言ggplot2可视化散点图、可视化两个数值变量之间的关系、使用geom_smooth函数基于loess方法拟合数据点之间的趋势关系曲线、自定义数据点的大小、色彩、添加主标题、副标题、题注信息
  2. 计算机能破坏光盘中的系统吗,因为某些程序被破坏需要重装系统,用2000的盘重装可以吗?...
  3. python3 写文件时指定文件名_Python3基础 os listdir 列举指定的所有文件及文件夹的名字...
  4. PHP设计模式——策略模式
  5. 阿里云 MaxCompute 2020-4 月刊
  6. msmq 发送到远程专用队列不存在_不知道消息中间件是什么?RabbitMQ从头到尾详解介绍...
  7. 电商设计师需要的产品广告促销打折标签
  8. graphviz python_工具推荐|我是如何使用Python脚本分析CPU使用情况的?
  9. java项目环境搭建
  10. 论文赏析[EMNLP18]用序列标注来进行成分句法分析
  11. lvgl8.2 分析画面刷新
  12. steam邮箱服务器设置,Steam游戏通用进服教程
  13. 微信小程序开发详解(九)---微信小程序布局基础
  14. 8、Ndk开发小记一
  15. 线性方程组解的数目判定
  16. java工控_用java做工控的大神进
  17. python怎样用填充颜色_python中如何给图形填充颜色
  18. Android中验证姓名、身份证、银行卡、手机号(正则表达式校验)
  19. 中国人是怎样移民到日本,拿到长期居留身份的呢?
  20. 记录一次重装win10系统后,没有1920*1080分辨率的问题

热门文章

  1. Markdown写作工具盘点
  2. Excel如何快速删除指定区域公式保留数值
  3. (转载)神级代码编辑器 Sublime Text 全程指南
  4. matlab设置三维图等高线,MATLAB --三维图形等高线
  5. groovy使用场景
  6. 水比赛系列-HMI串口屏的使用
  7. 产品经理素质能力模型
  8. mysql io瓶颈_服务器IO瓶颈对MySQL性能的影响
  9. 关于DSP的中断操作
  10. 这些关于JAVA基础类的(常用关键字)你都了解了吗?还没搞明白的朋友建议收藏慢慢看~