[关闭]
@WangYixu 2018-06-15T11:38:06.000000Z 字数 695 阅读 538

[CF208C]Game on Tree

OI 题解 期望 数学

前置知识

考虑每个点,他因为选到自己而变黑的概率为,而某个点变黑的花费期望为选中自己选到祖先.

所以总期望为每个点加起来。

代码

我竟然因为边表开小了WA了3次

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<iomanip>
  4. using namespace std;
  5. const int N = 100000 + 10;
  6. int head[N], to[N * 2], next[N * 2], cnt;
  7. long double ans;
  8. int dep[N];
  9. inline void adde(int x, int y) {
  10. ++cnt;
  11. to[cnt] = y;
  12. next[cnt] = head[x];
  13. head[x] = cnt;
  14. }
  15. void dfs(int x, int fa) {
  16. dep[x] = dep[fa] + 1;
  17. ans += 1.0 / (dep[x] * 1.0);
  18. for (register int i = head[x]; i; i = next[i]) {
  19. if (to[i] != fa) {
  20. dfs(to[i], x);
  21. }
  22. }
  23. }
  24. int main() {
  25. int n;
  26. scanf("%d", &n);
  27. for (register int i = 1, x, y; i < n; ++i) {
  28. scanf("%d %d", &x, &y);
  29. adde(x, y);
  30. adde(y, x);
  31. }
  32. dfs(1, 0);
  33. // printf("%.20llf\n", ans);
  34. cout << setprecision(20) << fixed << ans << endl;
  35. return 0;
  36. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注