Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- class DiameterOfRandomTree{
- public:
- vector<int> adj[55];
- long double dp[55][110][110];
- long double h[110][110];
- int mx[55];
- int n;
- void dfs(int nd,int p){
- dp[nd][0][0]=1.0;
- for(int t=0;t<adj[nd].size();t++){
- int ch=adj[nd][t];
- if(ch==p)continue;
- dfs(ch,nd);
- for(int i=0;i<110;i++){
- for(int j=0;j<110;j++){
- h[i][j]=0;
- }
- }
- mx[nd]=max(mx[nd],mx[ch]+2);
- int dia,ln;
- for(int i=0;i<=mx[nd];i++){
- for(int j=0;j<=mx[nd]*2;j++){
- for(int k=0;k<=mx[nd];k++){
- for(int l=0;l<=mx[nd]*2;l++){
- dia=max(i+k+1,max(j,l));
- ln=max(i,k+1);
- h[ln][dia] += dp[nd][i][j] * dp[ch][k][l] / 2;
- dia=max(i+k+2,max(j,l));
- ln=max(i,k+2);
- h[ln][dia] += dp[nd][i][j] * dp[ch][k][l] / 2;
- }
- }
- }
- }
- for(int i=0;i<110;i++){
- for(int j=0;j<110;j++){
- dp[nd][i][j]=h[i][j];
- }
- }
- }
- }
- double getExpectation(vector <int> a, vector <int> b){
- n=a.size()+1;
- for(int i=0;i<n-1;i++){
- adj[a[i]].push_back(b[i]);
- adj[b[i]].push_back(a[i]);
- }
- dfs(0,0);
- long double sol=0;
- for(int i=0;i<110;i++){
- for(int j=0;j<110;j++){
- sol+= dp[0][i][j] * j;
- }
- }
- return sol;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement