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],商人打算从某个城市出发到另一个城市结束,途中可以 ...
随机推荐
- uva 10791
还算比较水的一个数学题 求因子的最小和 总是用小的数去除 注意特判 是用int不行哦........ #include <cstdio> #include <cmath> ...
- 如何在eclipse里使用git
新版都自带git插件了.在项目上右键,选team,选share project,再选择git就可以了. 如果在本地使用git比较简单.如果要多人共享的使用git,那么需要专门的服务器,并提供ssh,这 ...
- URAL 1353 Milliard Vasya's Function(DP)
题目链接 题意 : 让你找出1到10^9中和为s的数有多少个. 思路 : 自己没想出来,看的题解,学长的题解报告 题解报告 //URAL 1353 #include <iostream> ...
- hdu 4035 Maze 概率DP
题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结点i都有3种可能: 1.被杀死,回到结点1处(概率为ki) ...
- redis info命令结果释疑
redis的性能数据这块用 info 命令就可以获取的比较全面了,下面是对info信息返回值的解释: # 参考:http://redis.io/commands/info # # # Server r ...
- PHP-CGI, FastCGI, PHP-FPM的关系和区别
Web server(apache, nginx) 接受到一个php请求后要解析php文件, 怎么解析呢, web server是C语言写的, 所以需要一个协议, 一个php解释器, 也就是CGI. ...
- R语言学习笔记:列表
R列表时以其他对象为成分的有序集合,列表的成分和向量不同,它们不一定是同一种数据类型,模式或者长度.例: > my.list<-list(stud.id=34453, + stud.nam ...
- ISE综合后得到的RTL图如何与硬件对应起来,怎么知道每个element的功能
2013-06-23 21:34:03 要知道“我写的这段代码会综合成什么样的电路呢”,就要搞清楚RTL图中每个模块的功能,从而将代码与硬件对应,判断综合后的电路是否与预期的一致.如何做到? 之前查了 ...
- IMX51+WINCE6.0平台缩写意义
1.以EPIT为例 EPIT(Enhanced Periodic Interrupt Timer)为增强型周期中断定时器,其中有CR控制寄存器,要设置CR寄存器的SWR位,代码如下: // Asser ...
- JsonPath详解
JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPat ...