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

Colors and Matching

Back to problem

Editorial

Let njn_jnj​ be the number of vertices of color jjj. Requiring at most AjA_jAj​ covered vertices of that color is equivalent to requiring at least dj=max⁡(0,nj−Aj)d_j=\max(0,n_j-A_j)dj​=max(0,nj​−A uncovered vertices.

For each color jjj, add djd_jdj​ dummy vertices and connect each of them to every original vertex of color jjj. Keep all original edges. Let D=∑j=1KdjD=\sum_{j=1}^K d_jD= and call the new graph . The answer is , where is the maximum matching size of .

Take any feasible original matching MMM. For every color jjj, choose djd_jdj​ uncovered vertices and match them to the dummy vertices of that color. This gives a matching of size ∣M∣+D|M|+D∣M∣+ in . Writing the original optimum as , we obtain .

Conversely, a maximum matching of G′G'G′ can be modified to cover every dummy. Suppose a dummy xxx of color jjj is uncovered. At most dj−1<njd_j-1<n_jdj original vertices of that color are matched to dummies, so some original vertex of color is not matched to a dummy. If is uncovered, adding contradicts maximality of the matching size. Hence is matched to an original vertex . Replace by . The size is unchanged, and the number of covered dummies increases by one. Repetition covers every dummy. We do not need to assume that an arbitrary maximum matching already covers them all.

Removing its DDD dummy edges leaves ν(G′)−D\nu(G')-Dν(G′)−D original edges. At most nj−dj≤Ajn_j-d_j\le A_jn vertices of color are covered by these edges, so this is feasible. Thus , proving the formula.

For subtask 1, choose an unprocessed vertex and enumerate leaving it uncovered or matching it to any remaining neighbor. Decrease the remaining capacities when selecting an edge; an edge with two endpoints of the same color consumes two units of that color. The number of matchings of a complete graph satisfies F(0)=F(1)=1F(0)=F(1)=1F(0)=F(1)=1 and F(V)=F(V−1)+(V−1)F(V−2)F(V)=F(V-1)+(V-1)F(V-2)F(V). An implementation suffices since .

For subtask 2, apply Edmonds' blossom algorithm to G′G'G′. Odd cycles must be handled, so an ordinary bipartite matching algorithm cannot be applied directly. Since D≤VD\le VD≤V and ∑jdjnj≤V2\sum_j d_jn_j\le V^2∑, there are at most vertices. Time is and space is .

There are at most 600 transformed vertices, 44,850 original edges, 90,000 dummy edges, and 134,850 total edges. The transformed matching size is at most 300 and the original answer is at most 150. Also D≤300D\le300D≤300 and ∑jAj≤90 000\sum_jA_j\le90\,000∑j​Aj​≤. All integer calculations fit signed 64-bit integers. An absent color or a color with has .

j
​
)
∑j=1K​dj​
G′G'G′
ν(G′)−D\nu(G')-Dν(G′)−D
ν(G′)\nu(G')ν(G′)
G′G'G′
D
G′G'G′
OPT\mathrm{OPT}OPT
OPT+D≤ν(G′)\mathrm{OPT}+D\le\nu(G')OPT+D≤ν(G′)
​
−
1<
nj​
vvv
jjj
vvv
(x,v)(x,v)(x,v)
vvv
uuu
(v,u)(v,u)(v,u)
(x,v)(x,v)(x,v)
j
​
−
dj​≤
Aj​
jjj
ν(G′)≤OPT+D\nu(G')\le\mathrm{OPT}+Dν(G′)≤OPT+D
=
F(V−
1)+
(V−
1)F(V−
2)
O(VF(V))O(VF(V))O(VF(V))
F(12)=140 152F(12)=140\,152F(12)=140152
j
​
dj​
nj​
≤
V2
2V2V2V
O((V+D)3)=O(V3)O((V+D)^3)=O(V^3)O((V+D)3)=O(V3)
O(V2)O(V^2)O(V2)
90000
Aj>njA_j>n_jAj​>nj​
dj=0d_j=0dj​=0