Right side configuration, to start from 0 assignment side, the right side is the initial select a 0, the assignment of each node that contribution to both sides of a strand (chain inclusive) of the product number, the current time assignment Chain one endpoint continued an edge, the edge weight is assigned a value of +1 last time. DFS to find the number of chains on both sides of the junction point of the composition (comprising a chain end), then the enumeration endpoint DP.
1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 vector<long long>edge[3007]; 5 long long cnt[3007][3007]; 6 long long fa[3007][3007]; 7 long long dp[3007][3007]; 8 void dfs(long long now,long longF, Long Long the root) { . 9 CNT [the root] [now] = . 1 ; // start point node number and the link current point of the side opposite to the point (endpoint including chain) 10 FA [the root] [now] = F; // the same start point and the current side-chain parent node F . 11 for (Auto IT: Edge [now]) { 12 is IF (IT == F) 13 is Continue ; 14 DFS (IT, now, the root ); 15 CNT [the root] [now] + = CNT [the root] [IT]; // the results back to the upper node 16 } . 17 } 18 is Long Long Solve ( Long Long L, Long Longr) { // find the answers to the endpoint l and r as the maximum strand . 19 IF (r == l) // start and end points can not be the same as 20 is return 0 ; 21 is IF (DP [l] [r] == 0 ) // L and r are as first strand ends 22 is DP [L] [r] = CNT [L] [r] * CNT [r] [L] + max (Solve (L, FA [L] [ R & lt]), Solve (R & lt, FA [R & lt] [L])); // this is a result both of the chain section of the number of nodes (including dot chain segments) plus the product of the current dot chain remove an endpoint answer ( one of the new addition of two end points, the new value of the right side of the right side of the primary chain Max + 1'd) 23 is return DP [L] [R & lt]; 24 } 25 int main () { 26 is iOS: : sync_with_stdio ( to false ); 27 cin.tie (NULL); 28 cout.tie(NULL); 29 long long n; 30 cin>>n; 31 for(long long i=1;i<n;++i){ 32 long long u,v; 33 cin>>u>>v; 34 edge[u].emplace_back(v); 35 edge[v].emplace_back(u); 36 } 37 for(long long i=1;i<=n;++i)//枚举链的起点 38 dfs(i,-1,i);//深搜 39 Long Long ANS = 0 ; 40 for ( Long Long I = . 1 ; I <= n-; I ++) // starting enumeration chain 41 is for ( Long Long J = . 1 ; J <= n-; J ++) // end enumeration chain 42 is ANS = max (ANS, Solve (I, J)); 43 is COUT << ANS; 44 is return 0 ; 45 }