HDU--4705

题目:

Y

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4705

分析:树形dp的思想,枚举中间点。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<vector>
 7 #include<stack>
 8 using namespace std;
 9 #define maxn 100005
10 #define LL __int64
11 #pragma comment(linker, "/STACK:16777216")
12 vector<int>e[maxn];
13 LL ans,sum,n;
14 bool vis[maxn];
15 int dfs(int s)
16 {
17     vis[s]=true;
18     int son,t=0;
19     for(int i=0;i<e[s].size();i++)
20     {
21         int to=e[s][i];
22         if(!vis[to])
23         {
24             son=dfs(to);
25             t+=son;
26             ans+=(LL)(n-t-1)*son;
27         }
28     }
29     return t+1;
30 }
31 int main()
32 {
33     while(scanf("%I64d",&n)!=EOF)
34     {
35         int u,v,m;m=int(n);
36         for(int i=0;i<maxn;i++)e[i].clear();
37         for(int i=0;i<m-1;i++)
38         {
39             scanf("%d%d",&u,&v);
40             e[u].push_back(v);
41             e[v].push_back(u);
42         }
43         memset(vis,false,sizeof(vis));
44         ans=0;
45         LL p=dfs(1);
46         sum=n*(n-1)*(n-2)/6;
47         printf("%I64d\n",sum-ans);
48     }
49     return 0;
50 }
View Code

 

转载于:https://www.cnblogs.com/i-love-acm/p/3323375.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值