bzoj3522 [Poi2014]Hotel

题目链接

在黄学长博客上居然分类树形DP?

暴力,以每个点为根计算子树内答案,推推式子就维护两个S就可以啦

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<string>
 7 #include<cmath>
 8 #include<ctime>
 9 #include<queue>
10 #include<stack>
11 #include<map>
12 #include<set>
13 #define rre(i,r,l) for(int i=(r);i>=(l);i--)
14 #define re(i,l,r) for(int i=(l);i<=(r);i++)
15 #define Clear(a,b) memset(a,b,sizeof(a))
16 #define inout(x) printf("%d",(x))
17 #define douin(x) scanf("%lf",&x)
18 #define strin(x) scanf("%s",(x))
19 #define LLin(x) scanf("%lld",&x)
20 #define op operator
21 #define CSC main
22 typedef unsigned long long ULL;
23 typedef const int cint;
24 typedef long long LL;
25 using namespace std;
26 void inin(int &ret)
27 {
28     ret=0;int f=0;char ch=getchar();
29     while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
30     while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar();
31     ret=f?-ret:ret;
32 }
33 int n,head[5050],next[10010],zhi[10010],ed,shen[5050],sum[5050],Max;
34 LL s1[5050],s2[5050],ans;
35 void add(int a,int b)
36 {
37     next[++ed]=head[a],head[a]=ed,zhi[ed]=b;
38     next[++ed]=head[b],head[b]=ed,zhi[ed]=a;
39 }
40 void dfs(int x,int fa)
41 {
42     sum[shen[x]]++;Max=max(Max,shen[x]);
43     for(int i=head[x];i;i=next[i])if(zhi[i]!=fa)
44         shen[zhi[i]]=shen[x]+1,dfs(zhi[i],x);
45 }
46 int main()
47 {
48     inin(n);
49     re(i,2,n)
50     {
51         int q,w;
52         inin(q),inin(w);
53         add(q,w);
54     }
55     re(i,1,n)
56     {
57         Clear(s1,0),Clear(s2,0);Max=0;
58         for(int j=head[i];j;j=next[j])
59         {
60             Clear(sum,0);
61             shen[zhi[j]]=1;
62             dfs(zhi[j],i);
63             re(k,1,Max)
64             {
65                 ans+=(LL)sum[k]*s1[k];
66                 s1[k]+=sum[k]*s2[k];
67                 s2[k]+=sum[k];
68             }
69         }
70     }
71     printf("%lld",ans);
72      return 0;
73 }

 

posted @ 2016-03-15 08:04  HugeGun  阅读(144)  评论(0编辑  收藏  举报