Description
Tree is a connected acyclic graph. Suppose you are given a tree consisting of nn vertices. The vertex of this tree is called centroid if the size of each connected component that appears if this vertex is removed from the tree doesn't exceed $frac{n}{2}$.
You are given a tree of size $n$ and can perform no more than one edge replacement. Edge replacement is the operation of removing one edge from the tree (without deleting incident vertices) and inserting one new edge (without adding new vertices) in such a way that the graph remains a tree. For each vertex you have to determine if it's possible to make it centroid by performing no more than one edge replacement.
给定一颗树,你有一次将树改造的机会,改造的意思是删去一条边,再加入一条边,保证改造后还是一棵树。
请问有多少点可以通过改造,成为这颗树的重心?(如果以某个点为根,每个子树的大小都不大于$frac{n}{2}$,则称某个点为重心)
Solution
当以一棵树的根为其重心时,其所有点的子树大小都$le frac{n}{2}$,因为根节点的所有子树大小都$le frac{n}{2}$
以树的重心为根,对于点$u$,树中$n-siz_{u}$的部分若可以取出一个最大的大小$le frac{n}{2}$的部分,设其大小为$f_u$,则当$n-siz_u-f_u le frac{n}{2}$时,点$u$可以被改造为重心
具体改造方法为将取下来的部分接到$u$上
$f_u$的值可能为:
- $n-siz_u$
- 其兄弟节点中$siz$值最大的
- $f_{fa_u}$
三遍DFS,第一遍找原树重心,第二遍维护$siz$和$siz$的最大值和次大值,第三遍求$f$