计蒜客: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;
}