Advertisement
Guest User

Untitled

a guest
Dec 26th, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class DiameterOfRandomTree{
  6. public:
  7.     vector<int> adj[55];
  8.     long double dp[55][110][110];
  9.     long double h[110][110];
  10.     int mx[55];
  11.     int n;
  12.  
  13.     void dfs(int nd,int p){
  14.         dp[nd][0][0]=1.0;
  15.         for(int t=0;t<adj[nd].size();t++){
  16.             int ch=adj[nd][t];
  17.             if(ch==p)continue;
  18.             dfs(ch,nd);
  19.             for(int i=0;i<110;i++){
  20.                 for(int j=0;j<110;j++){
  21.                     h[i][j]=0;
  22.                 }
  23.             }
  24.             mx[nd]=max(mx[nd],mx[ch]+2);
  25.             int dia,ln;
  26.             for(int i=0;i<=mx[nd];i++){
  27.                 for(int j=0;j<=mx[nd]*2;j++){
  28.                     for(int k=0;k<=mx[nd];k++){
  29.                         for(int l=0;l<=mx[nd]*2;l++){
  30.                             dia=max(i+k+1,max(j,l));
  31.                             ln=max(i,k+1);
  32.                             h[ln][dia] += dp[nd][i][j] * dp[ch][k][l] / 2;
  33.  
  34.                             dia=max(i+k+2,max(j,l));
  35.                             ln=max(i,k+2);
  36.                             h[ln][dia] += dp[nd][i][j] * dp[ch][k][l] / 2;
  37.                         }
  38.                     }
  39.                 }
  40.             }
  41.             for(int i=0;i<110;i++){
  42.                 for(int j=0;j<110;j++){
  43.                     dp[nd][i][j]=h[i][j];
  44.                 }
  45.             }
  46.         }
  47.     }
  48.     double getExpectation(vector <int> a, vector <int> b){
  49.         n=a.size()+1;
  50.         for(int i=0;i<n-1;i++){
  51.             adj[a[i]].push_back(b[i]);
  52.             adj[b[i]].push_back(a[i]);
  53.         }
  54.         dfs(0,0);
  55.         long double sol=0;
  56.         for(int i=0;i<110;i++){
  57.             for(int j=0;j<110;j++){
  58.                 sol+= dp[0][i][j] * j;
  59.             }
  60.         }
  61.         return sol;
  62.     }
  63. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement