计蒜客:Adjoin the Networks

15 篇文章 0 订阅
8 篇文章 0 订阅

计蒜客:ACM

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 Format

On the first line, you are given two positive integers, the number 1 \le c \le 10^51≤c≤105 of computers and the number 0 \le l \le c - 10≤l≤c−1 of existing cables. Then follow ll lines, each line consisting of two integers aa and bb, the two computers the cables connect. You may assume that every computer has a unique name between 00 and n - 1n−1.

Output Format

The maximum number of hops in the resulting network.

样例输入1

6 4
0 1
0 2
3 4
3 5

样例输出1

3

样例输入2

11 9
0 1
0 3
0 4
1 2
5 4
6 4
7 8
7 9
7 10

样例输出2

4

 

 

#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int maxn=100000+1000;
int a[maxn],vis[maxn],vis1[maxn],minv,head[maxn],tot,T;
struct Edge{
    int to,next;
}e[2*maxn];
void init(int n){
    tot=0,T=0;
    mem1(head),mem0(a),mem0(vis1),mem0(vis);
}
void addedge(int u,int v){
    e[tot].to=v,e[tot].next=head[u],head[u]=tot++,e[tot].to=u,e[tot].next=head[v],head[v]=tot++;
}
bool cmp(int u,int v){
    return u>v;
}
struct node{
    int u,dep;
};
node bfs(int u,int dep){
    queue<node>q;
    q.push((node){u,dep});
    vis[u]=T;
    int end1=u,count1=0;
    while(!q.empty()){
        node ans=q.front();
        q.pop();
        int u=ans.u,dep=ans.dep;
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].to;
            if(vis[v]!=T){
                q.push((node){v,dep+1});
                vis[v]=T,end1=v,count1=dep+1;
            }
        }
    }
    node b={end1,count1};
    return b;
}
int main(){
    int n,m,u,v;
    while(scanf("%d%d",&n,&m)!=EOF){
        init(n);
        for(int i=0;i<m;i++){
            scanf("%d%d",&u,&v);
            u++,v++,vis1[u]=1,vis1[v]=1;
            addedge(u,v);
        }
        int cnt=0,minv=0,flag=0;
        for(int i=1;i<=n;i++){
            if(vis[i]==0&&vis1[i]!=0){
                ++T;    
                node b=bfs(i,0);
                ++T;
                node c=bfs(b.u,0);
                minv=max(minv,c.dep);
                a[cnt++]=(c.dep+1)/2;
                flag=1;
            }
        }
        sort(a,a+cnt,cmp);
        if(n<=2)
            printf("%d\n",n-1>=0?n-1:0);
        else if(flag==0)       
            printf("2\n");
        else if(cnt>=3&&(a[0]==a[1])&&(a[1]==a[2]))
            printf("%d\n",max(minv,a[0]+a[1]+2));
        else
            printf("%d\n",max(minv,a[0]+a[1]+1));
    }
    return 0;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aurora_U

谢谢你的鼓励,我会继续努力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值