Adjoin the Networks Gym - 100781A

http://codeforces.com/gym/100781/attachments

In this paper, we give the minimum tree diameter of a connected forest graph

For all connected blocks, if there is only one connected block, then if there are two direct outputs, then compare the maximum diameter and the diameter after the maximum and minimum half connected, and take the minimum value, because connecting two trees together must be the barycenter of the two

If there are more than two barycenters, connect a star graph with all barycenters. In order to make the height of the tree as small as possible, if the number of sides between the barycenters of the next largest diameter and the second largest diameter is 2, the diameter may also be generated here

#include <bits/stdc++.h>
using namespace std;

struct node1
{
    int u;
    int v;
};

struct node2
{
    int v;
    int next;
};

node1 pre[100010];
node2 edge[200010];
int f[100010],first[100010],dis[100010],len[100010];
int n,m,num;

void addedge(int u,int v)
{
    edge[num].v=v;
    edge[num].next=first[u];
    first[u]=num++;
}

int getf(int p)
{
    if(f[p]==p) return p;
    else
    {
        f[p]=getf(f[p]);
        return f[p];
    }
}

void unite(int u,int v)
{
    int fu,fv;
    fu=getf(u),fv=getf(v);
    f[fv]=fu;
}

void dfs(int cur,int fa,int &p)
{
    int i,v;
    for(i=first[cur];i!=-1;i=edge[i].next)
    {
        v=edge[i].v;
        if(v!=fa)
        {
            dis[v]=dis[cur]+1;
            if(dis[p]<dis[v]) p=v;
            dfs(v,cur,p);
        }
    }
}

int main()
{
    int i,p1,p2;
    scanf("%d%d",&n,&m);
    memset(first,-1,sizeof(first));
    num=0;
    for(i=1;i<=n;i++) f[i]=i;
    for(i=1;i<=m;i++)
    {
        scanf("%d%d",&pre[i].u,&pre[i].v);
        pre[i].u++,pre[i].v++;
        addedge(pre[i].u,pre[i].v);
        addedge(pre[i].v,pre[i].u);
        unite(pre[i].u,pre[i].v);
    }
    num=0;
    for(i=1;i<=n;i++)
    {
        if(f[i]==i)
        {
            dis[i]=0,p1=i;
            dfs(i,0,p1);
            dis[p1]=0,p2=i;
            dfs(p1,0,p2);
            len[++num]=dis[p2];
        }
    }
    sort(len+1,len+num+1);
    if(num==1) printf("%d\n",len[1]);
    else if(num==2) printf("%d\n",max(len[num],(len[num-1]+1)/2+(len[num]+1)/2+1));
    else printf("%d\n",max(len[num],max((len[num-1]+1)/2+(len[num]+1)/2+1,(len[num-2]+1)/2+(len[num-1]+1)/2+2)));
    return 0;
}

 

Posted by monkuar on Fri, 03 Jan 2020 11:16:16 -0800