#pragma comment(linker, "/STACK:1024000000,1024000000") //加栈
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
#define inf 0x3f3f3f3f
int n;
int num,cnt[maxn];
int head[maxn];
ll ans;
struct Edge
{
int u,v,next,w;
}edge[maxn<<1];
void addEdge(int u,int v,int w)
{
edge[num].u=u;
edge[num].v=v;
edge[num].w=w;
edge[num].next=head[u];
head[u]=num++;
}
void init()
{
num=0;
memset(head,-1,sizeof(head));
}
void dfs(int u,int pre)//dfs求出在同一路径上的点的数量
{
int tmp=0;
cnt[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(v==pre) continue;
dfs(v,u);
ans+=1ll*tmp*cnt[v];
cnt[u]+=cnt[v];
tmp+=cnt[v];
}
ans+=1ll*tmp*(n-cnt[u]);
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
while(scanf("%d",&n)!=EOF)
{
init();
for(int i=1;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
addEdge(x,y,0);
addEdge(y,x,0);
}
memset(cnt,0,sizeof(cnt));
ans=0;
dfs(1,0);
ll res=1ll*n*(n-1)*(n-2)/6;
cout<<res-ans<<endl;
}
return 0;
}
Y HDU - 4705
猜你喜欢
转载自blog.csdn.net/qq_40774175/article/details/81462086
今日推荐
周排行