해설
Assign weight to each black vertex and weight to each white vertex. Then a black-white component is exactly a connected vertex set whose total weight is .
Root the tree at vertex . Every connected vertex set has a unique vertex closest to the root. Call it the top of .
Let be the generating function of connected vertex sets whose top is . Then,
The answer is the sum of the constant coefficients of over all vertices .
For , merging child polynomials one by one in is not enough. Choose one heavy child and define . Then . Along a heavy path, each vertex is an affine transform . Compose these transforms in a balanced way and use NTT for polynomial multiplication. Store for each interval so that the top DP is and the interval contribution is . This gives an -style solution.