# Codeforces 280C Game on Tree probability expectation of the random number of deletions in the dp Tree, codeforces280c

Source: Internet
Author: User

Codeforces 280C Game on Tree probability expectation of the random number of deletions in the dp Tree, codeforces280c

A tree with n points

Each operation randomly selects any vertex and deletes the subtree of the vertex.

When all vertices are deleted, the Operation stops.

The expected number of operations.

Question referenced from: Click to open the link

The deletion rule has a very good nature: for arbitrary (u, v), selecting u will cause the deletion of v, the vertex set to be deleted by selecting u must include the vertex set to be deleted after selecting v.

We want to use another method to implement the deletion process:
Generates a random permutation of 1-n p, and tries to delete these vertices from the past to the next. If the current vertex has been deleted, nothing will be done; otherwise, the number of times is increased by 1, delete this vertex and all its descendants.

The actual deletion sequence generated in this way is the same as the probability distribution of the original method of the question. This is obviously and very easy to prove.

If the process of YY is half done and P is arranged, there is a suffix waiting for explanation. The numbers 1 to n In this suffix are actually still in the tree, n + 1 to n + k are actually nonexistent points. So the probability that point 1 is the next selected point is

Because of the nature we found earlier, the above process can also be said as follows:

Generates a random permutation of 1-n p, which is previously deleted from the back. If the current vertex is not deleted, the number of times is + 1, and then the vertex and all its descendants are unconditionally deleted.
(In contrast to the original one, I tried to delete things in YY, and then I tried this method to understand it ..)

How can this be used? Based on the expected linearity, we finally need to calculate E (total number of operations) = sigma {E ([each node u is deleted because of its own choice])}. then the predicate is true. Only when u appears before all its ancestors in the P column we generate, the expectation of this item is the reciprocal of u depth in the tree. Adding the reciprocal of all depths is the answer.

`#include<bits/stdc++.h>template <class T>inline bool rd(T &ret) {    char c; int sgn;    if(c=getchar(),c==EOF) return 0;    while(c!='-'&&(c<'0'||c>'9')) c=getchar();    sgn=(c=='-')?-1:1;    ret=(c=='-')?0:(c-'0');    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');    ret*=sgn;    return 1;}template <class T>inline void pt(T x) {    if (x <0) {        putchar('-');        x = -x;    }    if(x>9) pt(x/10);    putchar(x%10+'0');}using namespace std;const int N = 100005;struct Edge{    int to, nex;}edge[N<<1];int head[N], edgenum;void add(int u, int v){    Edge E = {v, head[u]};    edge[edgenum] = E;    head[u] = edgenum++;}void init(){memset(head ,-1, sizeof head); edgenum = 0;}int dep[N], n;void dfs(int u, int fa, int deep){    dep[u] = deep;    for(int i = head[u]; ~i; i = edge[i].nex){        int v = edge[i].to;        if(v == fa)continue;        dfs(v, u, deep+1);    }}void input(){    init();    for(int i = 1, u, v; i < n; i++)    {        cin>>u>>v;        add(u, v); add(v, u);    }}int main(){    ios::sync_with_stdio(false);    while(cin>>n){        input();        dfs(1,1,1);        double ans = 0;        for(int i = 1; i <= n; i++)        {            ans += 1.0/dep[i];        }        printf("%.10f\n", ans);    }    return 0;}`

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.