#P11529. Tree Coloring: Minimizing White Conversions
Tree Coloring: Minimizing White Conversions
Tree Coloring: Minimizing White Conversions
You are given an undirected tree with \( n \) nodes and exactly \( k \) nodes are initially colored black. The remaining nodes are initially colored gray. Before the process begins, you are allowed to choose some gray nodes and paint them white. After that, the following simultaneous operation is performed in rounds until there is no gray node remaining on the tree:
- For every gray node \( u \):
- If none of the nodes directly adjacent to \( u \) are colored black or white (i.e. all are gray), then \( u \) remains gray.
- If at least one node directly adjacent to \( u \) is white, then \( u \) becomes white.
- If (and only if) no adjacent node is white but at least one adjacent node is black, then \( u \) becomes black.
Note that the order in the operations guarantees that if a gray node is simultaneously adjacent to both white and black nodes then it will be colored white. Also, the color updates in each round use the colors from the previous round only; that is, nodes which are updated in the current round do not affect the update of other nodes in the same round.
The final coloring of the tree is obtained when there is no gray node left. Let \( B_f \) be the number of nodes colored black at the end of the process. Your task is to choose the minimum number of gray nodes to paint white initially in order to guarantee that \( B_f \leq k \) (i.e. at most the original number of black nodes remain black).
Observation: Notice that in the first round every gray node that is directly adjacent to an initial black node would become black unless it is already white. Hence, to prevent any new node from turning black (and thereby keeping the final count \( B_f \leq k \)), it is necessary and sufficient to paint every gray node that is adjacent to any black node white in the initial step.
Your goal is to compute the minimum number of gray nodes that should be painted white initially.
Important: The tree is connected and the nodes are numbered from \( 1 \) to \( n \).
inputFormat
The first line contains two integers \( n \) and \( k \) \((1 \leq k \leq n \leq 10^5)\) representing the number of nodes and the number of initially black nodes, respectively.
The second line contains \( k \) distinct integers \( b_1, b_2, \dots, b_k \) \((1 \leq b_i \leq n)\) indicating the indices of the nodes that are initially black.
Each of the next \( n-1 \) lines contains two integers \( u \) and \( v \) \((1 \leq u,v \leq n)\) which denote an edge between nodes \( u \) and \( v \) in the tree.
outputFormat
Output a single integer representing the minimum number of gray nodes that must be painted white initially so that after the process the total number of black nodes is at most \( k \).
sample
3 1
1
1 2
2 3
1