DOJ logoDOJ
Problems
Recently added problemsSaved problems
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-backed certification.

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

General Graph Maximum Matching Reconstruction

Back to problem

Editorial

In a general graph, odd cycles prevent the direct use of bipartite matching algorithms. We use Edmonds' blossom algorithm.

For the current matching, run BFS to search for an augmenting path. When an edge between two vertices in the same BFS forest creates an odd cycle, contract the entire cycle into one blossom and continue the search in the contracted graph. Once an augmenting path is found, follow the parent pointers and flip the matched and unmatched edges along the path.

The implementation maintains the current mate of each vertex in matchmatchmatch, the BFS parent in ppp, and the representative of the blossom containing each vertex in basebasebase. The least common ancestor of the two sides of a newly found blossom is obtained by alternately following matching edges and BFS-parent edges. During contraction, every representative on the odd cycle is replaced by that least common ancestor, and newly reachable vertices are inserted into the BFS queue.

One augmenting-path search takes O(N2)O(N^2)O(N2) time, and the matching can be augmented at most ⌊N/2⌋\lfloor N/2 \rfloor⌊N/2⌋ times. Therefore, the total time complexity is O(N3+M)O(N^3+M)O(N and the memory complexity is .

For reconstruction, print each pair only once by selecting vertices vvv such that match[v]match[v]match[v] exists and v<match[v]v<match[v]v<match[v].

Solution written by GPT5.6

3
+
M)
O(N+M)O(N+M)O(N+M)