ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

codeforces NCPC2015 GYM 100781A Adjoin the Networks 圖的直徑

2021-03-28 21:02:47  阅读:192  来源: 互联网

标签:NCPC2015 number cnt int GYM codeforces 直徑 ans cables


博客迁移计划16

$ \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
*/

标签:NCPC2015,number,cnt,int,GYM,codeforces,直徑,ans,cables
来源: https://www.cnblogs.com/Potrem/p/CF_GYM100781A.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有