codeforces GYM 100781A【树的直径】

先求出每棵树的直径,排个序,要想图的直径最小的话需要每棵树的直径中点像直径最大的树的直径中点连边,这样直径有三种情况:是直径最大的树的直径:a[tot];是直径最大的树和直径第二大的树的半径拼起来+1:(a[tot]+1)/2+(a[tot-1]+1)/2+1);是直径第二大的树和直径第三大的树的半径拼起来+2:(a[tot-1]+1)/2+(a[tot-2]+1)/2+2
取max即可

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=100005;
int n,m,h[N],cnt,s,a[N],tot,mx;
bool v[N];
struct qwe
{
	int ne,to;
}e[N<<1];
int read()
{
	int r=0,f=1;
	char p=getchar();
	while(p>'9'||p<'0')
	{
		if(p=='-')
			f=-1;
		p=getchar();
	}
	while(p>='0'&&p<='9')
	{
		r=r*10+p-48;
		p=getchar();
	}
	return r*f;
}
void add(int u,int v)
{
	cnt++;
	e[cnt].ne=h[u];
	e[cnt].to=v;
	h[u]=cnt;
}
int dfs(int u)
{
    v[u]=1;
    int m1=0,m2=0;
    for(int i=h[u];i;i=e[i].ne) 
        if(!v[e[i].to])
		{
			int nw=dfs(e[i].to)+1;
			if(nw>m1)
				m2=m1,m1=nw;
			else if(nw>m2)
				m2=nw;
		}
    if(m1+m2>mx) 
		mx=m1+m2;
    return m1;
}
int main()
{
	n=read(),m=read();
	for(int i=1;i<=m;i++)
	{
		int x=read()+1,y=read()+1;
		add(x,y),add(y,x);
	}
	for(int i=1;i<=n;i++)
		if(!v[i])
		{
			mx=0;
			dfs(i);
			a[++tot]=mx;
		}
	sort(a+1,a+1+tot);
	int ans=a[tot];
	if(tot>1)
		ans=max(ans,(a[tot]+1)/2+(a[tot-1]+1)/2+1);
	if(tot>2)
		ans=max(ans,(a[tot-1]+1)/2+(a[tot-2]+1)/2+2);
	printf("%d
",ans);
	return 0;
}
原文地址:https://www.cnblogs.com/lokiii/p/9282182.html