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搜一遍就可以了。。。

  1 //STATUS:C++_AC_156MS_10508KB
  2 #include <functional>
  3 #include <algorithm>
  4 #include <iostream>
  5 //#include <ext/rope>
  6 #include <fstream>
  7 #include <sstream>
  8 #include <iomanip>
  9 #include <numeric>
 10 #include <cstring>
 11 #include <cassert>
 12 #include <cstdio>
 13 #include <string>
 14 #include <vector>
 15 #include <bitset>
 16 #include <queue>
 17 #include <stack>
 18 #include <cmath>
 19 #include <ctime>
 20 #include <list>
 21 #include <set>
 22 //#include <map>
 23 using namespace std;
 24 #pragma comment(linker,"/STACK:102400000,102400000")
 25 //using namespace __gnu_cxx;
 26 //define
 27 #define pii pair<int,int>
 28 #define mem(a,b) memset(a,b,sizeof(a))
 29 #define lson l,mid,rt<<1
 30 #define rson mid+1,r,rt<<1|1
 31 #define PI acos(-1.0)
 32 //typedef
 33 typedef __int64 LL;
 34 typedef unsigned __int64 ULL;
 35 //const
 36 const int N=100010;
 37 const int INF=0x3f3f3f3f;
 38 const int MOD=1000000007,STA=8000010;
 39 const LL LNF=1LL<<60;
 40 const double EPS=1e-8;
 41 const double OO=1e15;
 42 const int dx[4]={-1,0,1,0};
 43 const int dy[4]={0,1,0,-1};
 44 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
 45 //Daily Use ...
 46 inline int sign(double x){return (x>EPS)-(x<-EPS);}
 47 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
 48 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
 49 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
 50 template<class T> inline T Min(T a,T b){return a<b?a:b;}
 51 template<class T> inline T Max(T a,T b){return a>b?a:b;}
 52 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
 53 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
 54 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
 55 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
 56 //End
 57 
 58 struct Edge{
 59     int u,v;
 60 }e[N<<1];
 61 
 62 int first[N],next[N<<1];
 63 int n,m,mt;
 64 LL d[N],c[N],f1[N],f2[N],f3[N];
 65 LL ans;
 66 
 67 void adde(int a,int b)
 68 {
 69     e[mt].u=a;e[mt].v=b;
 70     next[mt]=first[a];first[a]=mt++;
 71     e[mt].u=b;e[mt].v=a;
 72     next[mt]=first[b];first[b]=mt++;
 73 }
 74 
 75 void dfs(int u,int fa)
 76 {
 77     int i,j,v,t,cnt=1;
 78     f1[0]=0;
 79     f2[0]=f2[1]=0;
 80     f3[0]=f3[1]=f3[2]=0;
 81     d[u]=1;
 82     for(i=first[u];i!=-1;i=next[i]){
 83         if((v=e[i].v)==fa)continue;
 84         dfs(v,u);
 85         d[u]+=d[v];
 86     }
 87     for(i=first[u];i!=-1;i=next[i]){
 88         if((v=e[i].v)==fa)continue;
 89         c[cnt]=d[v];
 90         f1[cnt]=f1[cnt-1]+c[cnt];
 91         f2[cnt]=f2[cnt-1]+c[cnt]*f1[cnt-1];
 92         cnt++;
 93     }
 94     if(cnt<3)return;
 95     if(fa!=-1){
 96         c[cnt]=n-d[u];
 97         f1[cnt]=f1[cnt-1]+c[cnt];
 98         f2[cnt]=f2[cnt-1]+c[cnt]*f1[cnt-1];
 99         cnt++;
100     }
101     if(cnt<4)return;
102     for(i=3;i<cnt;i++){
103         f3[i]=f3[i-1]+c[i]*f2[i-1];
104     }
105     ans+=f3[cnt-1];
106 }
107 
108 int main() {
109  //   freopen("in.txt", "r", stdin);
110     int i,j,a,b;
111     while(~scanf("%d",&n))
112     {
113         mem(first,-1);mt=0;
114         for(i=1;i<n;i++){
115             scanf("%d%d",&a,&b);
116             adde(a,b);
117         }
118 
119         ans=0;
120         dfs(1,-1);
121 
122         printf("%I64d
",ans);
123     }
124     return 0;
125 }
原文地址:https://www.cnblogs.com/zhsl/p/3275898.html