• 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 << '
    ';}
    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;
    }
    
  • 相关阅读:
    如何复制word的图文到UMEditor中自动上传
    如何复制word的图文到百度编辑器中自动上传
    如何复制word的图文到百度ueditor中自动上传
    java实现大文件上传源码
    java实现大文件上传控件
    java实现大文件上传组件
    java实现大文件上传插件
    html 常用标签及基本用法
    Web最佳实践阅读总结(1)
    基于DevOps理论与人工智能技术的银行业务可用性管控
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/10241189.html
Copyright © 2020-2023  润新知