#include <bits/stdc++.h>
//qwerty47924692
using namespace std;
using ll = long long;
const ll N=5005;
const string br="617283";
#define sz(a)(ll)a.size()
#define f first
#define s second
ll n,tin[N],tout[N],timer,pr[20][N],h[N];
vector<ll>g[N],ch;
void dfs(ll v){
tin[v]=++timer;
for(ll i=1;i<=19;i++){
pr[i][v]=pr[i-1][pr[i-1][v]];
}
for(ll to:g[v]){
if(to==pr[0][v])continue;
pr[0][to]=v;
h[to]=h[v]+1;
dfs(to);
}
tout[v]=timer;
if(tin[v]==tout[v]){
ch.push_back(v);
}
}
bool is_parent(ll x,ll y){
return (tin[x]<=tin[y]&&tout[y]<=tout[x]);
}
ll get(ll x,ll y){
for(ll i=19;i>=0;i--){
if(!is_parent(pr[i][x],y))x=pr[i][x];
}
if(!is_parent(x,y))return pr[0][x];
return x;
}
ll dist(ll v,ll u){
ll x=get(v,u);
return h[v]+h[u]-h[x]-h[x];
}
bool in(ll v,ll u,ll x){
return (is_parent(v,x)&&is_parent(x,u))||(is_parent(u,x)&&is_parent(x,v))||(is_parent(x,v)&&is_parent(v,u))||(is_parent(x,u)&&is_parent(u,v));
}
ll calc(ll x,ll v,ll u){
ll lca=get(v,u);
ll ans=0;
if(!is_parent(lca,x)){
return dist(lca,x);
}
for(ll i=19;i>=0;i--){
if(!in(lca,v,pr[i][x])&&!in(lca,u,pr[i][x]))ans+=dist(x,pr[i][x]),x=pr[i][x];
}
if(!in(lca,v,x)&&!in(lca,u,x)){
ans+=dist(x,pr[0][x]);
x=pr[0][x];
}
return ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n;
for(ll i=1;i<n;i++){
ll v,u;
cin>>v>>u;
g[v].push_back(u);
g[u].push_back(v);
}
pr[0][1]=1;
ch.push_back(1);
dfs(1);
ll mx=0,ans=0;
for(auto i:ch){
for(ll j:ch){
if(i==j)continue;
ll cur=0;
for(ll k=1;k<=n;k++){
cur=max(cur,calc(k,i,j));
}
cur*=dist(i,j);
if(mx==cur){
ans++;
}
if(mx<cur){
mx=cur;
ans=1;
}
}
}
cout<<mx<<' '<<ans/2;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |