DOJ logoDOJ
LoginRegister
DOJ logoDOJ
Call for Tasks
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
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Editorial

Sharing Pepero 2

Back to problem

Editorial

Subtask 1 For KKK empty cells, enumerate all 2K2^K2K assignments and check that every student receives both flavors. This takes O(2KNM)O(2^K NM)O(2KNM) time.

Full solution Create one vertex for each row and column, and an edge between row iii and column jjj for every empty cell (i,j)(i,j)(i,j). A horizontal box directs its edge toward the row; a vertical box directs it toward the column. Call a vertex required if its row or column contains a student. The goal is exactly to give every required vertex positive indegree.

In a tree component there is one fewer edge than vertices. If all vertices are required, the goal is impossible. Otherwise root the tree at a nonrequired vertex and direct every tree edge toward its child. Every other vertex receives an incoming edge.

For a component containing a cycle, choose a spanning tree and a nontree edge (r,c)(r,c)(r,c). Root the tree at rrr, direct tree edges toward children, and direct the chosen extra edge toward rrr. Every vertex now has an incoming edge. All other edges may be directed arbitrarily. An isolated vertex is covered by the same tree rule.

Therefore, impossibility occurs exactly when a tree component has only required vertices. Use DSU to build a spanning forest and collect extra edges, choose roots, and traverse iteratively to construct the assignment. Time complexity is O(NMα(N+M))O(NM\alpha(N+M))O(NMα(N+M)) and space complexity is O(NM+N+M)O(NM+N+M)O(NM+N+.

Solution written by GPT6.1

M
)