cf14d 树的直径,枚举删边

#include<bits/stdc++.h>
using namespace std;
#define maxn 300
struct Edge{int from,to,nxt,flag;}edge[maxn<<1];
int n,head[maxn],tot,a,b,dis[maxn];
void init(){
    memset(head,-1,sizeof head);
    tot=0;
}
void addedge(int u,int v){
    edge[tot].from=u,edge[tot].to=v;edge[tot].nxt=head[u];head[u]=tot++;
    edge[tot].flag=1;
}

int Max,node;
void dfs(int u,int pre,int dep){
    if(dep>Max){node=u,Max=dep;}
    for(int i=head[u];i!=-1;i=edge[i].nxt){
        int v=edge[i].to;
        if(edge[i].flag==0 || v==pre)continue;
        dfs(v,u,dep+1);
    }
}
int getdis(int root){
    node=root;
    Max=0,dfs(root,0,0);
    Max=0,dfs(node,0,0);
    return Max;
}

int main(){
    init();
    cin>>n;
    for(int i=1;i<n;i++){
        int u,v;
        cin>>u>>v;
        addedge(u,v);
        addedge(v,u);
    }
    int ans=-1;
    for(int i=0;i<tot;i+=2){
        int u=edge[i].from,v=edge[i].to;
        edge[i].flag=edge[i^1].flag=0;
        int m1=getdis(u),m2=getdis(v);
        ans=max(ans,m1*m2);
        edge[i].flag=edge[i^1].flag=1;
    }
    printf("%d\n",ans);
}

 

posted on 2019-01-29 11:39  zsben  阅读(188)  评论(0编辑  收藏  举报

导航