HDU4219--Randomization?(树形概率DP)

题意:https://blog.csdn.net/Umbrella__/article/details/78382710

思路:

首先dp【i】【j】保存的是,i节点到其叶子节点最大距离是j的合法概率。

一开始,转移的时候我想:只保证两颗子树从根到叶子最大值相加合法是不是有错(因为一个子树内部可能还有更长的),其实这个不需要考虑,因为你要合并的这两个子树也是刚才合法得合并出来的。

转移要用前缀优化一下max的那个东西,不然会TLE。

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr strcat
 13 #include <string>
 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 #include <cassert>
 21 #include <iomanip>
 22 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 23 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 24 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 25 //******************
 26 clock_t __START,__END;
 27 double __TOTALTIME;
 28 void _MS(){__START=clock();}
 29 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
 30 //***********************
 31 #define rint register int
 32 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 33 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 34 #define mem(a,b) memset(a,b,sizeof(a))
 35 #define pr printf
 36 #define sc scanf
 37 #define ls rt<<1
 38 #define rs rt<<1|1
 39 typedef pair<int,int> PII;
 40 typedef vector<int> VI;
 41 typedef unsigned long long ull;
 42 typedef long long ll;
 43 typedef double db;
 44 const db E=2.718281828;
 45 const db PI=acos(-1.0);
 46 const ll INF=(1LL<<60);
 47 const int inf=(1<<30);
 48 const db ESP=1e-9;
 49 const int mod=(int)1e9+7;
 50 const int N=(int)1e6+10;
 51 
 52 int n,L,S;
 53 db p;
 54 //==========================================================================================
 55 db dp[70][550];
 56 vector<vector<int> >G(100);
 57 int maxx(int a,int b)
 58 {
 59     return a>b?a:b;
 60 }
 61 
 62 void dfs(int u,int fa)
 63 {
 64     int sz=G[u].size();
 65     for(int i=0;i<sz;++i)
 66     {
 67         int to=G[u][i];
 68         if(to==fa)continue;
 69         dfs(to,u);
 70         db temp[550];
 71         mem(temp,0);
 72         db ko[550];
 73         mem(ko,0);
 74         for(int j=0;j<=520;++j)
 75             for(int o=0;o<=L;++o)
 76                 ko[j+o]+=p*dp[to][j];
 77         db kopre[550];kopre[0]=ko[0];
 78         for(int j=1;j<=520;++j)kopre[j]=kopre[j-1]+ko[j];
 79         db jpre[550];jpre[0]=dp[u][0];
 80         for(int j=1;j<=520;++j)jpre[j]=jpre[j-1]+dp[u][j];
 81         for(int j=0;j<=S;++j)
 82         {
 83             int min_=min(j,S-j);
 84             temp[j]+=dp[u][j]*kopre[min_];
 85             temp[j]+=jpre[min_]*ko[j];
 86             if(j*2<=S)temp[j]-=dp[u][j]*ko[j];//减去j*j这个重复的,但是j*2>S肯定不会出现j*j这个
 87         }
 88 /*
 89         for(int j=0;j<=520;++j)
 90             for(int k=0;k<=520;++k)
 91                 for(int o=0;o<=L;++o)
 92                     if(j+k+o<=S)
 93                         temp[maxx(j,k+o)]+=p*dp[u][j]*dp[to][k];
 94 */
 95         for(int j=0;j<=520;++j)
 96             dp[u][j]=temp[j];
 97     }
 98 }
 99 int tot=0;
100 void solve()
101 {
102     mem(dp,0);
103     sc("%d%d%d",&n,&L,&S);p=1.0/(L+1.0);
104     for(int i=1;i<=n;++i)dp[i][0]=1,G[i].clear();
105     for(int i=1;i<n;++i)
106     {
107         int u,v;
108         sc("%d%d",&u,&v);
109         G[u].push_back(v);
110         G[v].push_back(u);
111     }
112     dfs(1,0);
113     db ans=0;
114     for(int i=0;i<550;++i)ans+=dp[1][i];
115     pr("Case %d: %.6lf\n",++tot,ans);
116 }
117 
118 int main()
119 {
120     int T;
121     sc("%d",&T);
122     while(T--)solve();
123     return 0;
124 }
125 
126 /**************************************************************************************/

 

posted @ 2020-02-23 16:09  ZMWLxh  阅读(275)  评论(0)    收藏  举报