cf14d 树的直径,枚举删边

时间:2023-12-13 08:22:08
#include<bits/stdc++.h>
using namespace std;
#define maxn 300
struct Edge{int from,to,nxt,flag;}edge[maxn<<];
int n,head[maxn],tot,a,b,dis[maxn];
void init(){
memset(head,-,sizeof head);
tot=;
}
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=;
} int Max,node;
void dfs(int u,int pre,int dep){
if(dep>Max){node=u,Max=dep;}
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if(edge[i].flag== || v==pre)continue;
dfs(v,u,dep+);
}
}
int getdis(int root){
node=root;
Max=,dfs(root,,);
Max=,dfs(node,,);
return Max;
} int main(){
init();
cin>>n;
for(int i=;i<n;i++){
int u,v;
cin>>u>>v;
addedge(u,v);
addedge(v,u);
}
int ans=-;
for(int i=;i<tot;i+=){
int u=edge[i].from,v=edge[i].to;
edge[i].flag=edge[i^].flag=;
int m1=getdis(u),m2=getdis(v);
ans=max(ans,m1*m2);
edge[i].flag=edge[i^].flag=;
}
printf("%d\n",ans);
}