zoukankan      html  css  js  c++  java
  • [CF280C] Game on Tree

    [CF280C] Game on Tree - 拆贡献,期望

    Description

    给出一棵树,每次随机等概率选择一未染黑的点,将它及其子树染黑。问期望多少次操作可以将树全部染黑。

    Solution

    按惯例拆贡献,转化为每个点被染色次数的期望的和

    一个点会被染色,当且仅当它的所有祖先没有在它之前被染色

    换言之现在有个随机排列,我们问的就是某个元素出现在另外一些元素之前的概率

    因此一个点的贡献就是它深度的倒数

    #include <bits/stdc++.h>
    using namespace std;
    
    #define int long long
    
    const int N = 1000005;
    int n;
    vector<int> g[N];
    int dep[N];
    
    void dfs(int p, int from)
    {
        for (int q : g[p])
        {
            if (q == from)
                continue;
            dep[q] = dep[p] + 1;
            dfs(q, p);
        }
    }
    
    signed main()
    {
        ios::sync_with_stdio(false);
    
        cin >> n;
        for (int i = 1; i < n; i++)
        {
            int t1, t2;
            cin >> t1 >> t2;
            g[t1].push_back(t2);
            g[t2].push_back(t1);
        }
    
        dep[1] = 1;
        dfs(1, 0);
    
        double ans = 0;
        for (int i = 1; i <= n; i++)
            ans += 1.0 / dep[i];
    
        cout << fixed << setprecision(12) << ans << endl;
    }
    
  • 相关阅读:
    git
    界面编程与视图(View)组件
    genymotion的安装
    210中断故障分析
    6410/210按键中断编程
    2440按键中断编程
    中断处理流程深度剖析
    scrapy 写文件进行debug调试
    scrapy 爬虫中间件-offsite和refer中间件
    scrapy文件管道
  • 原文地址:https://www.cnblogs.com/mollnn/p/14638761.html
Copyright © 2011-2022 走看看