题目链接:这里
题意:给你一棵树,让你选择两条不相交的路径,使得长度的乘积最大。
解法:枚举删除哪条边,然后再跑树形dp就好了。(维护最长和次长就可以了)
//CF 14D
#include <bits/stdc++.h>
using namespace std;
const int maxn = 206;
vector <int> G[maxn];
int n, maxlen;
int dfs(int x, int fa){
int len1 = 0, len2 = 0, len = 0;
for(int i = 0; i < G[x].size(); i++){
int v = G[x][i];
if(v == fa) continue;
len = max(dfs(v, x), len);
if(maxlen > len1){
len2 = len1;
len1 = maxlen;
}
else{
len2 = max(len2, maxlen);
}
}
len = max(len, len1+len2);
maxlen = len1 + 1;
return len;
}
int main()
{
scanf("%d", &n);
for(int i = 1; i < n; i++){
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
int ans = 0;
for(int i = 1; i <= n; i++){
for(int j = 0; j < G[i].size(); j++){
int a = dfs(G[i][j], i);
int b = dfs(i, G[i][j]);
ans = max(ans, a*b);
}
}
cout << ans << endl;
return 0;
}