题目
题解

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100002;
struct node{
	int to,ne;
}e[N<<1];
int n,i,x,y,tot,dep[N],h[N],son[N];
ll space[N*7],*f[N],*g[N],ans,*now=space+N;
inline char gc(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd(){
	int x=0,fl=1;char ch=gc();
	for (;ch<48||ch>57;ch=gc())if(ch=='-')fl=-1;
	for (;48<=ch&&ch<=57;ch=gc())x=(x<<3)+(x<<1)+(ch^48);
	return x*fl;
}
inline void wri(int a){if(a<0)a=-a,putchar('-');if(a>=10)wri(a/10);putchar(a%10|48);}
inline void wln(int a){wri(a);puts("");}
void add(int x,int y){e[++tot]=(node){y,h[x]},h[x]=tot;}
void create(int u){
	f[u]=now,now+=dep[u]*2+1;
	g[u]=now,now+=dep[u]*2+1;
}
void init(int u,int fa){
	for (int i=h[u],v;i;i=e[i].ne)
		if ((v=e[i].to)!=fa){
			init(v,u);
			if (dep[v]>dep[son[u]]) son[u]=v;
		}
	dep[u]=dep[son[u]]+1;
}
void dfs(int u,int fa){
	f[u][0]=1;
	if (son[u]){
		f[son[u]]=f[u]+1;
		g[son[u]]=g[u]-1;
		dfs(son[u],u);
		ans+=g[u][0];
	}
	for (int i=h[u],v;i;i=e[i].ne)
		if ((v=e[i].to)!=fa && v!=son[u]){
			create(v);
			dfs(v,u);
			for (int j=dep[v];~j;j--){
				if (j) ans+=f[u][j-1]*g[v][j];
				ans+=g[u][j+1]*f[v][j];
				g[u][j+1]+=f[u][j+1]*f[v][j];
			}
			for (int j=0;j<=dep[v];j++){
				if (j) g[u][j-1]+=g[v][j];
				f[u][j+1]+=f[v][j];
			}
		}
}
int main(){
	n=rd();
	for (i=1;i<n;i++) x=rd(),y=rd(),add(x,y),add(y,x);
	init(1,0);
	create(1);
	dfs(1,0);
	printf("%lld",ans);
}