Feeling not knowing anything about expectations ... (; ′⌒ ') ╮ (╯﹏╰) ╭
have been thinking about how DP, finally read the puzzle--unexpectedly is this??? "shocked" but after looking at the puzzle, I feel really reasonable ...
We can consider the composition of the final answer, and we can calculate the contribution of different points to the answer separately (expecting linearity). We can think of this dyeing process as each node needs to be dyed, but only the first dyed node consumes 1 of the cost. This allows us to analyze the probability of each point contributing to the answer, which is the sum of probabilities. And what is the probability that a point will have an effect on the answer? This is actually related only to the node that it has to the root chain, because it will contribute 1 to the answer as long as it is dyed before its ancestor node is dyed.
So when it and its ancestors are not dyed, it is obvious that the probability of choosing any node is equal, obviously there is a probability of choosing it (\frac{1}{dep[u]}\) (If another node is selected, the node can no longer contribute). So...... The problem is done. Strong Ah! %%%
#include <bits/stdc++.h> using namespace std; #define maxn 200000 #define db double int n, dep[maxn];
db ans; int read()
{ int x = 0, k = 1; char c; c = getchar(); while(c < ‘0‘ || c > ‘9‘) { if(c == ‘-‘) k = -1; c = getchar(); } while(c >= ‘0‘ && c <= ‘9‘) x = x * 10 + c - ‘0‘, c = getchar(); return x * k;
} struct edge
{ int cnp, to[maxn], last[maxn], head[maxn];
edge() { cnp = 2; } void add(int u, int v)
{
to[cnp] = v, last[cnp] = head[u], head[u] = cnp ++;
to[cnp] = u, last[cnp] = head[v], head[v] = cnp ++;
}
}E1; void dfs(int u, int fa)
{ for(int i = E1.head[u]; i; i = E1.last[i])
{ int v = E1.to[i]; if(v == fa) continue;
dep[v] = dep[u] + 1; dfs(v, u);
}
} int main()
{
n = read(); for(int i = 1; i < n; i ++)
{ int u = read(), v = read();
E1.add(u, v);
}
dep[1] = 1; dfs(1, 0); for(int i = 1; i <= n; i ++) ans += (1.0 / (db) dep[i]);
printf("%.10lf\n", ans); return 0;
}
"cf#172" (Div. 1) c.game on Tree