Guest User

Untitled

a guest
Dec 26th, 2015
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. #include <vector>
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <queue>
  6. #include <deque>
  7. #include <stack>
  8. #include <bitset>
  9. #include <algorithm>
  10. #include <functional>
  11. #include <numeric>
  12. #include <utility>
  13. #include <sstream>
  14. #include <iostream>
  15. #include <iomanip>
  16. #include <cstdio>
  17. #include <cmath>
  18. #include <cstdlib>
  19. #include <ctime>
  20.  
  21. using namespace std;
  22.  
  23. const int N = 60;
  24.  
  25. int n;
  26. vector<int> g[N];
  27. double dp[N][2*N][2*N];
  28. int used[N];
  29. int sz[N];
  30. double ndp[2*N][2*N];
  31.  
  32. void dfs(int v)
  33. {
  34. used[v]=1;
  35. vector<int> sons;
  36. sz[v]=1;
  37.  
  38. for (int i=0;i<g[v].size();i++)
  39. {
  40. int to=g[v][i];
  41. if (used[to])
  42. continue;
  43. dfs(to);
  44. sons.push_back(to);
  45. }
  46.  
  47. for (int i=0;i<=sz[v]*2;i++)
  48. for (int j=0;j<=sz[v]*2;j++)
  49. dp[v][i][j]=0;
  50.  
  51. dp[v][0][0]=1;
  52.  
  53. sz[v]=1;
  54.  
  55. for (int ii=0;ii<sons.size();ii++)
  56. {
  57. int to=sons[ii];
  58. for (int i=0;i<=(sz[v]+sz[to])*2;i++)
  59. for (int j=0;j<=(sz[v]+sz[to])*2;j++)
  60. ndp[i][j]=0;
  61.  
  62. for (int i=0;i<=sz[v]*2;i++)
  63. for (int j=0;j<=sz[v]*2;j++)
  64. if (dp[v][i][j]>1e-20)
  65. for (int q=0;q<=sz[to]*2;q++)
  66. for (int w=0;w<=sz[to]*2;w++)
  67. {
  68. ndp[max(i,q+1)][max(j,max(w,q+1+i))]+=dp[v][i][j]*dp[to][q][w]*.5;
  69. ndp[max(i,q+2)][max(j,max(w,q+2+i))]+=dp[v][i][j]*dp[to][q][w]*.5;
  70. }
  71.  
  72. sz[v]+=sz[to];
  73. for (int i=0;i<=sz[v]*2;i++)
  74. for (int j=0;j<=sz[v]*2;j++)
  75. dp[v][i][j]=ndp[i][j];
  76. }
  77. }
  78.  
  79. class DiameterOfRandomTree {
  80. public:
  81. double getExpectation(vector <int> a, vector <int> b) {
  82.  
  83. int n=a.size()+1;
  84. for (int i=0;i<=n;i++)
  85. g[i].clear();
  86.  
  87. for (int i=0;i<=n;i++)
  88. used[i]=0;
  89.  
  90. for (int i=0;i<a.size();i++)
  91. {
  92. g[a[i]].push_back(b[i]);
  93. g[b[i]].push_back(a[i]);
  94. }
  95.  
  96. dfs(0);
  97.  
  98. double ans=0;
  99.  
  100. //cout<<sz[0]<<endl;
  101.  
  102. for (int i=0;i<=2*sz[0];i++)
  103. for (int j=0;j<=2*sz[0];j++)
  104. {
  105. ans+=dp[0][i][j]*max(i,j);
  106. // if (n<6)
  107. // cout<<i<<"%"<<j<<" "<<dp[0][i][j]<<" "<<dp[0][i][j]*max(i,j)<<" "<<ans<<endl;
  108.  
  109. }
  110.  
  111. return ans;
  112. }
  113. };
Add Comment
Please, Sign In to add comment