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

博客迁移计划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
*/
上一篇:网络安全与Kali Linux:利用MSF对Win7进行永恒之蓝***演练实战


下一篇:Gym - 101955K Let the Flames Begin(思维)