Use a disjoint-set union structure to merge vertices connected by edges whose endpoints initially have the same color. For each resulting component , let be its size and its color. Initialize the answer to the largest of these sizes, covering the choice of making no operation.
Although the two endpoints may receive different colors, it is sufficient to consider assigning them the same color when maximizing the answer.
For a vertex , let be the set of initial components containing or a neighbor of . Remove duplicate component IDs. Define
If both endpoints of edge are changed to color , the size of the component containing them is
Here equals if is true and otherwise. The leading already counts the endpoints, so an endpoint originally colored must be subtracted once if it is also counted in the sum.
Orient every edge as so that . Process all edges directed into the same vertex together. First mark the component IDs in and compute the sums . All edges directed into share this information.
For an edge , perform the following steps.
An absent color has value in and . Compress colors and use timestamp arrays so that changing edges does not require clearing an entire array.
Sorting in preprocessing takes time. Processing edge takes time. The total time complexity is therefore , and the space complexity is .
Proof
First, consider a largest component after an optimal operation, and let its color be . If contains neither selected endpoint, it is contained in an initial monochromatic component, so it is no larger than the answer obtained without an operation. If it contains both endpoints, their colors already coincide. If it contains exactly one, change the other endpoint to color as well. This removes no vertex from and preserves the edge between the endpoints. Thus an operation assigning equal colors can achieve at least the same size.
Suppose both endpoints become color . Every vertex initially colored keeps its color, so its initial monochromatic component cannot split. All color- components containing or neighboring either endpoint become connected through the two endpoints. No other color- component can join: an edge of color between two initial components would have made them the same initial component. This proves the formula using .
Solution written by GPT6
The components in alone connect to the two endpoints, so each value in the first step is no greater than an achievable component size. Only the at most two original endpoint colors are penalized. Among the three colors with largest , at least one is unpenalized; its is at least that of every color outside the top three. Therefore the largest corrected base value occurs among these three colors.
If a color has no component in , its base value is already exact. Otherwise, the second step computes its exact value after adding the last such component. Intermediate values include only some of the components and cannot exceed the true answer. Thus the maximum over all colors and all edges is considered. Together with the initial components, this yields the global optimum.
Finally, the sum of vertex degrees is . Orient an edge from an endpoint of smaller degree to one of larger degree. If the smaller degree is at most , its contribution is at most this threshold. Otherwise, both endpoints belong to the fewer than vertices of higher degree. Each vertex is then charged its degree only times. Hence the sum of the smaller endpoint degrees over all edges is . Since , this bound applies to the algorithm as well.