https://vjudge.net/problem/Gym-100781A#
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1e5+10;
int v[2*N],first[N],nxt[2*N],e;
void insert(int x,int y){
v[++e]=y;
nxt[e]=first[x];
first[x]=e;
}
int dis;
int b[N],be;
bool vis[2][N];
int s;
void dfs(int x,int d,bool f){
vis[f][x]=1;
if(d>dis){
dis=d;
s=x;
}
for(int i=first[x];i;i=nxt[i])
if(!vis[f][v[i]])
dfs(v[i],d+1,f);
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
e=0;
if(n==1){
puts("0");return 0;
}
if(n==2){
puts("1");return 0;
}
int x,y;
for(int i=1;i<=m;i++){
scanf("%d%d",&x,&y);
++x;++y;
insert(x,y);insert(y,x);
}
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
if(!vis[0][i]){
dis=0;
dfs(i,0,0);
dis=0;
dfs(s,0,1);
b[++be]=dis;
}
sort(b+1,b+1+be);
int ans=max(b[be]/2+b[be]%2+b[be-1]/2+b[be-1]%2+(be!=1),b[be]);
if(be>=3){
ans=max(ans,b[be-2]/2+b[be-2]%2+b[be-1]/2+b[be-1]%2+2);
}
printf("%d\n",ans);
}