首页 > 代码库 > HDOJ 4705 Y 树形DP
HDOJ 4705 Y 树形DP
DP:求出3点构成链的方案数 。然后总方案数减去它
Y
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1738 Accepted Submission(s): 493
Problem Description
Sample Input
4 1 2 1 3 1 4
Sample Output
1Hint1. The only set is {2,3,4}. 2. Please use #pragma comment(linker, "/STACK:16777216")
Source
2013 Multi-University Training Contest 10
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #pragma comment(linker, "/STACK:16777216") using namespace std; typedef long long int LL; const int maxn=100100; vector<int> g[maxn]; int n; LL ans,doubi[maxn],son[maxn]; void dfs(int u,int fa) { LL ndoubi=0,nson=0; son[u]=1; for(int i=0,sz=g[u].size();i<sz;i++) { int v=g[u][i]; if(v==fa) continue; dfs(v,u); son[u]+=son[v]; doubi[u]+=doubi[v]+son[v]; ans+=son[v]*nson; ans+=son[v]*ndoubi; ans+=doubi[v]*nson; ans+=doubi[v]; nson+=son[v]; ndoubi+=doubi[v]; } } int main() { while(scanf("%d",&n)!=EOF) { ans=0; memset(doubi,0,sizeof(doubi)); memset(son,0,sizeof(son)); for(int i=0;i<=n+10;i++) g[i].clear(); for(int i=0;i<n-1;i++) { int a,b; scanf("%d%d",&a,&b); g[a].push_back(b); g[b].push_back(a); } dfs(1,1); cout<<1LL*n*(n-1)*(n-2)/6-ans<<endl; } return 0; }
HDOJ 4705 Y 树形DP
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。