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

  题意:给一颗树,从树上任意选择3个点{A,B,C},要求他们不在一条链上,求总共的数目。

  容易想到枚举每个点,然后从每个点的所有分支中选择3个分支,然后从每个分支中选择1个点。假设点u有k个分支,每个分支的节点个数为a1,a2...an,那么方案数就是这个数列中所有3个数的积的和。直接枚举肯定会TLE的。我们可以维护3个前缀和,f1[i]表示前 i 个数选择一个的方案数,f2[i]表示前 i 个数选择两个的方案数,f3[i]表示前 i 个数选择3个的方案数。那么f1[i]就是前缀和,f2[i]=f2[i-1]+f1[i-1]*c[i],f3[i]=f3[i-1]+f2[i-1]*c[i]。然后DFS搜一遍就可以了。。。

 //STATUS:C++_AC_156MS_10508KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
//#include <map>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Edge{
int u,v;
}e[N<<]; int first[N],next[N<<];
int n,m,mt;
LL d[N],c[N],f1[N],f2[N],f3[N];
LL ans; void adde(int a,int b)
{
e[mt].u=a;e[mt].v=b;
next[mt]=first[a];first[a]=mt++;
e[mt].u=b;e[mt].v=a;
next[mt]=first[b];first[b]=mt++;
} void dfs(int u,int fa)
{
int i,j,v,t,cnt=;
f1[]=;
f2[]=f2[]=;
f3[]=f3[]=f3[]=;
d[u]=;
for(i=first[u];i!=-;i=next[i]){
if((v=e[i].v)==fa)continue;
dfs(v,u);
d[u]+=d[v];
}
for(i=first[u];i!=-;i=next[i]){
if((v=e[i].v)==fa)continue;
c[cnt]=d[v];
f1[cnt]=f1[cnt-]+c[cnt];
f2[cnt]=f2[cnt-]+c[cnt]*f1[cnt-];
cnt++;
}
if(cnt<)return;
if(fa!=-){
c[cnt]=n-d[u];
f1[cnt]=f1[cnt-]+c[cnt];
f2[cnt]=f2[cnt-]+c[cnt]*f1[cnt-];
cnt++;
}
if(cnt<)return;
for(i=;i<cnt;i++){
f3[i]=f3[i-]+c[i]*f2[i-];
}
ans+=f3[cnt-];
} int main() {
// freopen("in.txt", "r", stdin);
int i,j,a,b;
while(~scanf("%d",&n))
{
mem(first,-);mt=;
for(i=;i<n;i++){
scanf("%d%d",&a,&b);
adde(a,b);
} ans=;
dfs(,-); printf("%I64d\n",ans);
}
return ;
}

HDU-4705 Y 树形DP的更多相关文章

  1. HDOJ 4705 Y 树形DP

    DP:求出3点构成链的方案数 .然后总方案数减去它 Y Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K ...

  2. HDU 4705 Y 树形枚举

    树形枚举--搜索 题目描述: 给你一棵树,要在一条简单路径上选3个不同的点构成一个集合,问能构成多少个不同的集合. 解法: 枚举所有结点,假设某个结点有n棵子树,每棵子树的结点个数分别为s1,s2,` ...

  3. HDU 4705 Y (2013多校10,1010题,简单树形DP)

    Y Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submiss ...

  4. hdu 4081 最小生成树+树形dp

    思路:直接先求一下最小生成树,然后用树形dp来求最优值.也就是两遍dfs. #include<iostream> #include<algorithm> #include< ...

  5. HDU 3899 简单树形DP

    题意:一棵树,给出每个点的权值和每条边的长度, 点j到点i的代价为点j的权值乘以连接i和j的边的长度.求点x使得所有点到点x的代价最小,输出 虽然还是不太懂树形DP是什么意思,先把代码贴出来把. 这道 ...

  6. HDU 2196.Computer 树形dp 树的直径

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. hdu4705 Y 树形DP

    给出一颗数,求没有一条路径穿过的节点三元集合个数. 这样的三元集合呈现Y字形,求出反面情况,三点为子节点和两个祖先节点,或一个祖先节点与它子树中非父子关系的节点.可由树形DP求得. #pragma c ...

  8. hdu Anniversary party 树形DP,点带有值。求MAX

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. HDU 2196 Computer 树形DP经典题

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...

  10. hdu 6201 【树形dp||SPFA最长路】

    http://acm.hdu.edu.cn/showproblem.php?pid=6201 n个城市都在卖一种书,该书的价格在i城市为cost[i],商人打算从某个城市出发到另一个城市结束,途中可以 ...

随机推荐

  1. 【leetcode】3Sum Closest(middle)

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  2. Time.deltaTime 含义和应用

    第一種:使用Time.deltaTime 一秒內從第1個Frame到最後一個Frame所花的時間,所以不管電腦是一秒跑60格或者一秒30格.24格,值都會趨近於一. 就結果而言,deltaTime是為 ...

  3. coco2d-js 多屏适配相关API

    setDesignResolutionSize() //设计分辨率大小及模式  setContentScaleFactor() //内容缩放因子 setSearchPaths() //资源搜索路径 g ...

  4. $.cookie 使用不了的问题定位过程

    最近在项目中需要使用到jquery的cookie,按理说在html头中引入jquery-1.7.1.min.js和jquery.cookie.js,然后在js中就可以使用cookie函数了.像这样使用 ...

  5. QT中16进制字符串转汉字

    最经在研究AT指令接受短信,短信是unicode编码,接受后需要根据系统的编码方案进行相关的转码比如接受到了一串字符4F60597D,它是“你好”的unicode编码,一个unicode编码占两个字节 ...

  6. 91. Decode Ways

    题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...

  7. 最短路径算法之四——SPFA算法

    SPAF算法 求单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm,该算法是西南交通大学段凡丁于1994年发表的. 它可以在O(kE)的时间复杂度内求出源点 ...

  8. win8 hyper-v 禁用不必卸载虚拟机

    转载:http://tylzwp.blogbus.com/logs/232938121.html 禁用hyperv的目的是使用之前在用的VMware的虚拟机,不必重新处理一遍. 具体操作: 1确报之前 ...

  9. C3p0/元数据/内省-Bean/自定义查询封装类/查询/分页

    c3p0连接池(C3p0连接池,只有当用户获取连接时,才会包装Connection.) 第一步:导入c3p0 第二步:在classpath目录下,创建一个c3p0-config.xml 第三步:创建工 ...

  10. 【ZOJ】3609 Modular Inverse

    1. 题目描述求乘法逆元. 2. 基本思路利用扩展gcd求逆元,模板题目. 3. 代码 /* 3609 */ #include <iostream> #include <sstrea ...