HDU-4705 Y 树形DP
题目链接: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的更多相关文章
- HDOJ 4705 Y 树形DP
DP:求出3点构成链的方案数 .然后总方案数减去它 Y Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- HDU 4705 Y 树形枚举
树形枚举--搜索 题目描述: 给你一棵树,要在一条简单路径上选3个不同的点构成一个集合,问能构成多少个不同的集合. 解法: 枚举所有结点,假设某个结点有n棵子树,每棵子树的结点个数分别为s1,s2,` ...
- HDU 4705 Y (2013多校10,1010题,简单树形DP)
Y Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submiss ...
- hdu 4081 最小生成树+树形dp
思路:直接先求一下最小生成树,然后用树形dp来求最优值.也就是两遍dfs. #include<iostream> #include<algorithm> #include< ...
- HDU 3899 简单树形DP
题意:一棵树,给出每个点的权值和每条边的长度, 点j到点i的代价为点j的权值乘以连接i和j的边的长度.求点x使得所有点到点x的代价最小,输出 虽然还是不太懂树形DP是什么意思,先把代码贴出来把. 这道 ...
- HDU 2196.Computer 树形dp 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu4705 Y 树形DP
给出一颗数,求没有一条路径穿过的节点三元集合个数. 这样的三元集合呈现Y字形,求出反面情况,三点为子节点和两个祖先节点,或一个祖先节点与它子树中非父子关系的节点.可由树形DP求得. #pragma c ...
- hdu Anniversary party 树形DP,点带有值。求MAX
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 2196 Computer 树形DP经典题
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...
- hdu 6201 【树形dp||SPFA最长路】
http://acm.hdu.edu.cn/showproblem.php?pid=6201 n个城市都在卖一种书,该书的价格在i城市为cost[i],商人打算从某个城市出发到另一个城市结束,途中可以 ...
随机推荐
- 《head first java 》读书笔记
Updated 2014/03/26 P300-P402 Updated 2014/03/25 P213-300 Updated 2014/03/20 P0-P213 对象本身已知的事物被称为: 实例 ...
- 如何保护 .NET 应用的安全?
自从 Web 应用能给访问者提供丰富的内容之后,黑客们就把目光转向任何他们能够破坏,损毁,欺骗的漏洞.通过网络浏览器提供的应用越来越多,网络罪犯们可以利用的漏洞数量也呈指数增长起来. 大多数企业都依赖 ...
- HDU4607+BFS
/* bfs+求树的直径 关键:if k<=maxs+1 直接输出k-1: else: k肯定的是包括最长路.先从最长路的起点出发,再走分支,最后到达最长路的终点. 因此是2*(k-(maxs+ ...
- 使用头文件cfloat中的符号常量获知浮点类型数据的表数范围---gyy整理
使用头文件cfloat中的符号常量获知浮点类型数据的表数范围 cfloat头文件包含了系统的浮点数的长度限制 头文件climits中的符号常量是获知整型数据的表数范围,并不能获取浮点类型数据的表数范围 ...
- Mac与Linux的一个巨大不同
就是Mac仍处在桌面市场的商业圈里,尽管它的市场很小,但是正版率却很高,而且还有专门的Mac Store提供付费下载. Linux在桌面市场几乎没有份额,也不会有人会去买它的应用软件,基本上只存在于服 ...
- POJ1753——Flip Game
Flip Game Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on ...
- Android数据存储(三)——SQLite
如果需要一个更加健壮的数据存储机制,则需要使用一个关系型数据库,在Android上,则为SQLlite. SQLite的特点:轻量级.嵌入式的.关系型数据库.可移植性好,易使用,小,高效且可靠,与使用 ...
- [ffmpeg 扩展第三方库编译系列] 关于 mingw32 下编译libcaca
在编译前最好先看一下帮助 ./configure --help 开始编译 ./configure --disable-shared --disable-cxx \ --disable-csharp ...
- hdu 4502 吉哥系列故事——临时工计划(dp)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4502 思路:一个简单的dp ,比赛时没做出来呢..... d[i]代表 到第i天时的最大值 #includ ...
- No Hibernate Session bound to thread, and configuration does not allow
今天晚上挺悲催的,遇到了这个问题花费我很长时间,现在总结如下: 到这这种情况的发生有两种情况: 1,没有配置事物只要在Spring配置文件中添加如下代码: <bean id="txMa ...