P8095 [USACO22JAN] Cereal 2 S Solution
The content below was generated entirely by machine translation. Please verify its accuracy. If anything is unclear, consult the Chinese source version.
Preface: This solution may be rather verbose. I did not solve the problem during the contest, so I wrote the solution mainly to organize my own thoughts. If you already have the idea and only made a mistake in the implementation, I recommend jumping directly to the code section.
update@2022/3/13: Thanks to @小木虫 for the reminder: the current solution is not a correct solution! If USACO’s test data were strong enough, the Hungarian algorithm used here would not pass this problem because its complexity is . If you want to implement my bipartite-matching plus topological approach, you can use Dinic’s algorithm to find the maximum bipartite matching, although it is more troublesome to write. When I have time, I will also try implementing this solution with Dinic and update the solution.
The reading experience is better on the blog
1: Problem Statement
There are cows and types of cereal, with one box of each type. Every cow has a first and a second favorite cereal type, referred to below as its first and second choices. A cow first chooses its favorite cereal; if that cereal is occupied, it chooses its second favorite. Determine:
- The minimum possible number of cows that receive no cereal.
- An ordering of the cows that achieves this minimum.
2: Analysis
2.1 First Question
For the first question, we can see that this is a standard maximum bipartite-matching problem, and it is easy to think of solving it with the Hungarian algorithm (although I did not think of this during the contest). Readers unfamiliar with the Hungarian algorithm and bipartite matching can refer to the solutions for the template problem. This solution will focus mainly on the second question.
2.2 Second Question
For the second question, my initial idea was to output the cows successfully matched to their first choices, then the cows successfully matched to their second choices, and finally the unmatched cows. The resulting submission passed only the sample. With guidance from @lutongyu, I finally understood the problem with this approach.
More specifically, it is fine to output cows successfully matched to their first choices first, and it is also fine to output unmatched cows last. The real problem is the order of the second-choice cows. Consider the following data, shown in the figure below:
1 | 1 (cow) -> [1 (fir), 2 (sec)] |

We can manually simulate these data.
First try the optimal ordering 1 2 3:
- first takes its first choice, cereal 1.
- Because ’s first choice is occupied by , it takes its second choice, cereal 3.
- ’s first choice is occupied by , so it takes its second choice, cereal 4.
In this case, every cow receives cereal.
Now swap the order of and , obtaining the ordering 1 3 2 and the following simulation:
- takes its first choice, cereal 1.
- takes its first choice, cereal 3.
- Both the first and second choices of are occupied—cereals 1 and 3—so it cannot receive cereal.
In this case, cannot receive any cereal.
These data show that directly outputting cows matched to their second choices does not work. Some additional processing is necessary when outputting second-choice cows to ensure that the ordering still attains the maximum matching size.
More specifically, we can use an algorithm similar to topological sorting to resolve conflicts among second-choice cows.
First consider what happens when a cow is successfully matched to its first choice and that cereal is also another cow’s first choice. Using in the preceding figure as an example, it affects the choice of : occupies ’s first choice, forcing it to select its second choice. then affects ’s choice: occupies ’s first choice, forcing it to select its second choice. From this observation, we find that outputting cows along such an “influence chain” guarantees the maximum matching.
3: Algorithm
The beginning of such a chain must be a cow successfully matched to its first choice that forces another cow to choose its second choice; that is, this cow’s first choice is also another cow’s first choice. Enqueue all such cows. Next consider the affected cows. To discover the influence chain, we must also enqueue these affected cows, because they can choose only their second choices, and their second choices may occupy the first choices of other cows, just as does in the figure.
To find which cows might be affected, introduce a dynamic array inv_e[i], implemented as an adjacency list, that contains every cow whose first choice is cereal i. Only a cow whose first choice was taken by another cow can be affected.
For example, in the preceding figure, inv_e[1] = [ , ].
The Hungarian algorithm uses a matched[i] array. Its index denotes a node on the right side, and its value denotes the left-side node matched to that right-side node. In this problem, the index of matched[i] is a cereal number, and its value is the cow matched to that cereal. We can introduce an inv_match[i] array whose index is a cow and whose value is a cereal. Through inv_match, we can determine which cereal each cow is ultimately matched to—its first choice, its second choice, or no match.
Using the preceding figure as an example, inv_match[ ] equals cereal 3, because cereal 3 is its final match.
The following code shows how to find every cow affected by a first-choice cow:
1 | for(int i = 1; i <= n; i++){ // i traverses cows successfully matched to their first choices. |
Next, the second choices of cows already in the queue may occupy other cows’ first choices. We can therefore use a similar method to find the remainder of this influence chain:
1 | while(!q.empty()){ |
4: Code Implementation and Details
Finally, here is the complete code, with comments explaining the details:
1 | /*Date: 22 - 02-03 22 19 |
Finally, I hope this solution can help you. If you still do not understand something or discover a problem in the solution, you can send me a private message or point it out in the comments. I will try my best to answer or resolve it.





![[Stanford CS144] Lab 4 Record](/img/CS144/tcp%E7%8A%B6%E6%80%81%E6%B5%81%E8%BD%AC%E5%9B%BE.jpg)