---
id: 619
title: "「Codeforces 280C」Game on Tree 题解"
slug: cf280c-game-on-tree
lang: zh
date: 2019-02-02T10:06:42
date_gmt: 2019-02-02T02:06:42
modified: 2023-11-11T14:42:57
status: publish
password_protected: false
category: { name: Olympiad in Informatics, slug: oi }
tags: [OI, 概率期望]
original_url: https://kalorona.com/oi/cf280cgame-on-tree-题解/
archived: 2026-11-27
editor: classic
media: []
archive_notes:
  - "Body headings were <h1> in the original; demoted to level 2 so the page title is the only h1."
comments: []
---

## 主要思路

我们考虑每一个点被删掉的情景，对于点$i$，它只能被深度小于等于点$i$的点操作后删除，概率为$\frac{1}{dep[i]}$，每删掉一个节点的代价是$1$，所以答案为$\sum_{i=1}^{n} \frac{1}{dep[i]}$

## 代码

```cpp
// CF280C.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 100;
int head[MAX_N], current, n, tmpx, tmpy;
double ans = 0;
struct edge
{
	int to, nxt;
} edges[MAX_N << 1];
void addpath(int u, int v)
{
	edges[current].to = v, edges[current].nxt = head[u];
	head[u] = current++;
}
void dfs(int u, int fa, int dep)
{
	ans += 1.0 / dep;
	for(int i = head[u]; i != -1; i = edges[i].nxt)
		if(edges[i].to != fa)
			dfs(edges[i].to, u, dep + 1);
}
int main()
{
	memset(head, -1, sizeof(head));
	scanf("%d", &n);
	for(int i = 1; i <= n - 1; i++)
		scanf("%d%d", &tmpx, &tmpy), addpath(tmpx, tmpy), addpath(tmpy, tmpx);
	dfs(1, 0, 1);
	printf("%.20lf", ans);
	return 0;
}
```
