Main topic
Give a tree, ask to choose three points, so that the distance between the three points equal to the scheme of how many kinds.
Ideas
These three points must not be a chain again, then it is certainly possible to determine a central point, so that the distance between the three points to the center point is equal.
Then we can enumerate the center point and count it for each depth. Although it looks like a O ( n 3 ) , but it's a running flight.
CODE
#define _crt_secure_no_warnings#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 5010using namespace STD;intPointsintHead[max], total;int_next[max <<1], Aim[max <<1];inline voidADD (intXintY) {_next[++total] = head[x]; Aim[total] = y; HEAD[X] = total;}intDeep[max];intNowvoidDFS (intXintLastintd) {++deep[d]; for(inti = head[x]; I i = _next[i]) {if(Aim[i] = = last)Continue; DFS (Aim[i], X, D +1); }}Long LongCnt[max], Ans[max];intMain () {Cin>> points; for(intX, y, i =1; I < points; ++i) {scanf("%d%d", &x, &y); Add (x, y), add (y, x); }Long LongOutput =0; for(intx =1; x <= points; ++X) {memset(CNT,0,sizeof(CNT));memset(ANS,0,sizeof(ans)); for(inti = head[x]; I i = _next[i]) {memset(Deep,0,sizeof(deep)); DFS (Aim[i], X,1); for(intD =1; D <= points; ++D) {output + = ans[d] * Deep[d]; Ans[d] + = cnt[d] * Deep[d]; Cnt[d] + = deep[d]; } } }cout<< output << Endl;return 0;}
Bzoj 3522 POI Hotel tree DP