Condorcet Paradox
Time limit5sMemory limit128 MB
Given b ranked ballots over c candidates, find the candidate who beats every other candidate head to head on more than half the ballots.
- Level
Medium4 of 10
- Topics
- Array, Simulation, Brute force, Implementation
- Solved
- No attempts yet
Problem
A Condorcet winner is a candidate who would beat every other candidate in a one-on-one contest. The Condorcet winner can only be determined from ballots on which each voter writes down all of the candidates in their order of preference; such a ballot is called a preference list.
One candidate X beats another candidate Y one-on-one when X is ranked before Y on a strict majority of the ballots (that is, on more than half of them). Because every ballot ranks all candidates, for any pair exactly one of them is ranked higher on each ballot, so X beats Y precisely when X is preferred to Y on more than half of the ballots.
For example, suppose there are three candidates A, B, C and three voters whose preference lists are ABC, BAC, and CBA. Here B is the Condorcet winner: B is ranked ahead of A on two ballots (the 2nd and 3rd) and ahead of C on two ballots (the 1st and 2nd). Note that under ordinary plurality voting (counting only each voter's first choice) this same example would be a three-way tie.
There can be at most one Condorcet winner, but sometimes there is none. Given the preference lists, determine the Condorcet winner.
Input
The input consists of several test cases. The first line of each test case contains the number of preference lists b and the number of candidates c (1 ≤ b ≤ 500, 1 ≤ c ≤ 2500). The candidates are numbered from 0 to c-1. Each of the next b lines contains one preference list: the candidate numbers listed in that voter's order of preference (from most to least preferred), a permutation of 0 through c-1. The last line of the input is 0 0.
Output
For each test case, output Case k: w, where k is the test-case number (starting at 1) and w is the number of the Condorcet winner. If there is no Condorcet winner, output Case k: No Condorcet winner instead.