欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

cf280C. Game on Tree(期望线性性)

程序员文章站 2022-04-04 14:33:27
题意 "题目链接" Sol 开始想的dp,发现根本不能转移(貌似只能做链) 根据期望的线性性,其中$ans = \sum_{1 f(x)}$ $f(x)$表示删除$x$节点的概率,显然$x$节点要被删除,那么它的祖先都不能被删除,因此概率为$\frac{1}{deep[x]}$ cpp includ ......

题意

题目链接

cf280C. Game on Tree(期望线性性)

sol

开始想的dp,发现根本不能转移(貌似只能做链)

根据期望的线性性,其中\(ans = \sum_{1 * f(x)}\)

\(f(x)\)表示删除\(x\)节点的概率,显然\(x\)节点要被删除,那么它的祖先都不能被删除,因此概率为\(\frac{1}{deep[x]}\)

#include<bits/stdc++.h> 
#define pair pair<int, int>
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long 
#define ll long long 
#define ull unsigned long long 
#define fin(x) {freopen(#x".in","r",stdin);}
#define fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int maxn = 1e6 + 10, mod = 1e9 + 7, inf = 1e9 + 10;
const double eps = 1e-9;
template <typename a, typename b> inline bool chmin(a &a, b b){if(a > b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline bool chmax(a &a, b b){if(a < b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline ll add(a x, b y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename a, typename b> inline void add2(a &x, b y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename a, typename b> inline ll mul(a x, b y) {return 1ll * x * y % mod;}
template <typename a, typename b> inline void mul2(a &x, b y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename a> inline void debug(a a){cout << a << '\n';}
template <typename a> inline ll sqr(a x){return 1ll * x * x;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int n, dep[maxn];
vector<int> v[maxn];
void dfs(int x, int fa) {
    dep[x] = dep[fa] + 1;
    for(int i = 0; i < v[x].size(); i++) {
        int to = v[x][i];
        if(to == fa) continue;
        dfs(to, x);
    }
}
signed main() {
    n = read();
    for(int i = 1; i <= n - 1; i++) {
        int x = read(), y = read();
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(1, 0);
    double ans = 0;
    for(int i = 1; i <= n; i++) ans += 1.0 / dep[i];
    printf("%.12lf", ans);
    return 0;
}