Codeforces 280C Game on Tree 树形期望dp
2016-08-16 10:16
579 查看
题目链接
题意:
给出一棵树,每次可以拆掉一颗子树,问拆完整棵树次数的期望。思路:
删掉x点,将x点的贡献记为1,将x的后辈结点贡献记为0。换句话说如果x点是作为根结点删掉的那么贡献为1,否则贡献为0。
现在的问题就是求∑f(i)=∑p(i)∗1+(1−p(i))∗0
其中p(i)=1/depth(i)
#include<cstdio> #include<string> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<iomanip> using namespace std; #define all(x) (x).begin(), (x).end() #define for0(a, n) for (int (a) = 0; (a) < (n); (a)++) #define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++) #define mes(a,x,s) memset(a,x,(s)*sizeof a[0]) #define mem(a,x) memset(a,x,sizeof a) #define ysk(x) (1<<(x)) typedef long long ll; typedef pair<int, int> pii; const int INF =0x3f3f3f3f; const int maxn= 100000 ; int n; vector<int >G[maxn+10]; double ans; void dfs(int x,int fa,int depth) { ans+=1.0/depth; int siz=G[x].size(); for0(i,siz) { int y=G[x][i];if(y==fa) continue; dfs(y,x,depth+1); } } int main() { std::ios::sync_with_stdio(false); while(cin>>n) { for1(i,n) G[i].clear(); int x,y; for1(i,n-1) { cin>>x>>y; G[x].push_back(y); G[y].push_back(x); } ans=0; dfs(1,-1,1); cout<<fixed<<setprecision(20)<<ans<<endl; } return 0; }
相关文章推荐
- Codeforces 280C Game on Tree 概率dp 树上随机删子树 求删完次数的期望
- Codeforces 280 C. Game on Tree (概率与期望)
- codeforces 280C Game on Tree
- CodeForces 280C Game on Tree 概率
- Codeforces 280C - Game on Tree
- codeforces 405 D. Bear and Tree Jumps 树形dp
- CodeForces 771C Bear and Tree Jumps 树形DP
- Codeforces 696B Puzzles 树形期望dp
- Codeforces 839C Journey (树形dp + 概率期望)
- [Ccodeforces 736C] Ostap and Tree - 树形DP
- 【Codeforces 736C】 Ostap and Tree【树形DP】
- codeforces 220 C. Game on Tree
- CodeForces 618 D Hamiltonian Spanning Tree(树形DP)
- Appleman and Tree - CodeForces 461 B 树形dp
- CodeForces 160D - Distance in Tree 树形DP
- codeforces 161D D. Distance in Tree(树形dp)
- HDU 3848 CC On The Tree 树形DP
- codeforces 697D Puzzles (树形dp 期望 推荐)
- CodeForces 482 D.Random Function and Tree(树形DP)
- [BZOJ4424]CodeForces 19# E Fairy(树形dp / Link-Cut-tree)