题目链接:Codeforces - Game on Tree
考虑每个点的贡献。
如果这个点会被染色,那么一定父亲以及祖先没有被染色。
所以这个点的贡献为 1.0/dep[x] ,上面的概率乘以操作次数1.
AC代码:
#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=1e5+10;
int n,dep[N]; double res;
vector<int> g[N];
inline void add(int a,int b){g[a].push_back(b),g[b].push_back(a);}
void dfs(int x,int fa){
dep[x]=dep[fa]+1; res+=1.0/dep[x];
for(auto to:g[x]) if(to!=fa) dfs(to,x);
}
signed main(){
cin>>n;
for(int i=1,a,b;i<n;i++) cin>>a>>b,add(a,b);
dfs(1,1);
printf("%.10lf\n",res);
return 0;
}