Solution for Codeforces Round 1073 (Div. 2), F
Preface
这是一篇英文题解。This is an English solution.
Solution
Consider the necessary and sufficient condition for :
- Edge exists in ;
- Let be the root of . Then, is in the subtree of .
This is because every vertex is removed before .
If and are in the same tree, when is both an ancestor of (including itself) and a child of , all ways are valid. Otherwise, the answer is .
If and are in different connected components, calculate answer for each node . When and are not connected by an edge, just add the edge . After that, we could only consider the second constraint. Here’s the critical part: now the answer is , where is the size of the subtree of , and is the size of the whole tree containing , since all valid edge additions are equivalent by symmetry (that is, the probability of randomly choosing a way to add the edges, in which the subtree containing is connected to the side of is ). If we added the edge manually, the formula should be applied to the new graph, that is , where is the connected component containing .
