DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord account linking.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Recoloring Two Vertices

Back to problem

Editorial

Use a disjoint-set union structure to merge vertices connected by edges whose endpoints initially have the same color. For each resulting component qqq, let wqw_qwq​ be its size and dqd_qdq​ 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 vvv, let RvR_vRv​ be the set of initial components containing vvv or a neighbor of vvv. Remove duplicate component IDs. Define

Fv(x)=∑q∈Rv, dq=xwq.F_v(x)=\sum_{q\in R_v,\ d_q=x}w_q.Fv​(x)=q∈Rv

If both endpoints of edge (u,v)(u,v)(u,v) are changed to color xxx, the size of the component containing them is

2+∑q∈Ru∪Rv, dq=xwq−[Cu=x]−[Cv=x].2+\sum_{q\in R_u\cup R_v,\ d_q=x}w_q-[C_u=x]-[C_v=x].2+q∈Ru​∪R

Here [P][P][P] equals 111 if PPP is true and 000 otherwise. The leading 222 already counts the endpoints, so an endpoint originally colored xxx must be subtracted once if it is also counted in the sum.

Orient every edge as u→vu\to vu→v so that ∣Ru∣≤∣Rv∣|R_u|\leq|R_v|∣Ru​∣≤∣Rv​. 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 u→vu\to vu→v, perform the following steps.

  • Consider the three distinct colors with the largest Fv(x)F_v(x)Fv​(x) and update the answer with 2+Fv(x)−[Cu=x]−[Cv=x]2+F_v(x)-[C_u=x]-[C_v=x]2+F. If there are fewer than three colors, consider all of them.

An absent color has value 000 in FvF_vFv​ and EEE. Compress colors and use timestamp arrays so that changing edges does not require clearing an entire array.

Sorting in preprocessing takes O((N+M)log⁡N)O((N+M)\log N)O((N+M)logN) time. Processing edge (u,v)(u,v)(u,v) takes O(min⁡(deg⁡(u),deg⁡(v))+1) time. The total time complexity is therefore , and the space complexity is .

Proof

First, consider a largest component HHH after an optimal operation, and let its color be xxx. If HHH 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 xxx as well. This removes no vertex from HHH 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

​
,
 
dq​
=
x
∑
​
wq​
.
v​
,
 
dq​
=
x
∑
​
wq​
−
[Cu​=
x]−
[Cv​=
x].
∣
vvv
RvR_vRv​
FvF_vFv​
vvv
v
​
(
x
)
−
[Cu​=
x]−
[Cv​=
x]
  • Scan RuR_uRu​. For components absent from RvR_vRv​, add their sizes to a sum E(x)E(x)E(x) for their color and update the answer with 2+Fv(x)+E(x)−[Cu=x]−[Cv=x]2+F_v(x)+E(x)-[C_u=x]-[C_v=x]2+Fv​(x)+E(x)−[Cu​=x]−.
  • O(\min(\deg(u),\deg(v))+1)
    O(min(deg(u),deg(v))+1)
    O((N+M)log⁡N+MM)O((N+M)\log N+M\sqrt M)O((N+M)logN+MM​)
    O(N+M)O(N+M)O(N+M)
    xxx
    xxx
    xxx
    xxx
    xxx
    Ru∪RvR_u\cup R_vRu​∪Rv​

    The components in RvR_vRv​ 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 FvF_vFv​, at least one is unpenalized; its FvF_vFv​ 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 xxx has no component in Ru∖RvR_u\setminus R_vRu​∖Rv​, 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 2M2M2M. Orient an edge from an endpoint of smaller degree to one of larger degree. If the smaller degree is at most 2M\sqrt{2M}2M​, its contribution is at most this threshold. Otherwise, both endpoints belong to the fewer than 2M\sqrt{2M}2M​ vertices of higher degree. Each vertex is then charged its degree only O(M)O(\sqrt M)O(M​) times. Hence the sum of the smaller endpoint degrees over all edges is O(MM)O(M\sqrt M)O(MM​). Since ∣Rv∣≤deg⁡(v)+1|R_v|\leq\deg(v)+1∣Rv​∣≤deg(v)+1, this bound applies to the algorithm as well.

    [
    Cv​
    =
    x]