Transferable Voting

Time limit1sMemory limit128 MB

Summary
Simulate an instant-runoff election with ranked ballots, discard invalid ones, eliminate lowest-vote candidates each round, and report the winner or the tied candidates.
Level

Medium4 of 10

Topics
Simulation, Implementation, Array
Solved
No attempts yet

Problem

The transferable-vote system elects a single winner and requires that the winner hold an absolute majority of the valid ballots, even when there are more than two candidates. To make this possible, every voter ranks all of the candidates in order of preference. If there are cc candidates for the position, each ballot lists cc choices: a first choice, a second choice, and so on down to the cc-th choice.

The candidates are numbered 11 through cc. A ballot is invalid (a "bad ballot") if its cc numbers are not all distinct — the same candidate may not be listed twice — or if any number falls outside the range 11 to cc. Invalid ballots are discarded, but they are counted and reported.

Let VV be the number of valid ballots. A candidate is elected as soon as they hold more than half of the valid ballots, that is at least ⌊V/2⌋+1\lfloor V/2 \rfloor + 1 votes.

The winner is found in rounds:

  1. Give each valid ballot to its highest-ranked candidate who has not yet been eliminated.
  2. If some candidate has at least ⌊V/2⌋+1\lfloor V/2 \rfloor + 1 votes, that candidate is elected.
  3. Otherwise, if every remaining candidate has the same number of votes, they are all tied — report all of them.
  4. Otherwise, eliminate every candidate tied for the fewest votes, then repeat from step 1.

Because each valid ballot ranks all cc candidates, an eliminated candidate's ballots always flow to the next-ranked candidate who is still in the running.

For example, consider a small election with 12 voters and 3 candidates. Their ballots are shown in Table A.

          Table A                Table B
-----------------------------    -------
Voter  First   Second  Third
       Choice  Choice  Choice
  1      1       2       4
  2      1       3       2       1  3  2
  3      3       2       1       3  2  1
  4      3       2       1       3  2  1
  5      1       2       3       1  2  3
  6      2       3       1       3  1
  7      3       2       1       3  2  1
  8      3       1       1
  9      3       2       1       3  2  1
 10      1       2       3       1  2  3
 11      1       3       2       1  3  2
 12      2       3       1       3  1

Voters 1 and 8 cast invalid ballots (voter 1 lists candidate 4, which does not exist; voter 8 lists candidate 1 twice), so they are not counted. That leaves 10 valid ballots, so winning requires at least 6 votes. Candidates 1 and 3 each have 4 first-choice votes and candidate 2 has 2, so no one has a majority. Candidate 2 has the fewest votes and is eliminated. The two ballots that ranked candidate 2 first now advance to their next surviving choice (Table B), giving candidate 3 two more votes for a total of 6 — enough to be elected. (Had voter 12 instead cast "2 1 3", candidates 1 and 3 would each have finished with 5 votes and the election would have been a tie.)

Input

The input contains one or more elections. Each election begins with a line holding two integers, the number of candidates cc and the number of voters nn (1≤c≤51 \le c \le 5; there are at most 100 voters). The next nn lines each describe one ballot as cc whitespace-separated non-negative integers, listing that voter's choices from first to last.

The input ends with a line containing two zeros (0 0), which is not part of any election.

Output

For each election, print the result in the following format, and print the elections one after another with no blank line between them:

  • A line Election #k, where kk is the election's position in the input (the first election is #1).
  • If the election had any invalid ballots, a line b bad ballot(s) (indented by three spaces), where bb is the number of invalid ballots. Omit this line when there are none.
  • The result, indented by three spaces:
    • Candidate w is elected. if candidate ww wins, or
    • The following candidates are tied: followed by the numbers of the tied candidates in increasing order, with a space before each number and a space after the last one.

Examples3

  1. Example 1

    Input
    3 12
    1 2 4
    1 3 2
    3 2 1
    3 2 1
    1 2 3
    2 3 1
    3 2 1
    3 1 1
    3 2 1
    1 2 3
    1 3 2
    2 3 1
    3 12
    1 2 4
    1 3 2
    3 2 1
    3 2 1
    1 2 3
    2 3 1
    3 2 1
    3 1 1
    3 2 1
    1 2 3
    1 3 2
    2 1 3
    4 15
    4 3 1 2
    4 1 2 3
    3 1 4 2
    1 3 2 4
    4 1 2 3
    3 4 2 1
    2 4 3 1
    3 2 1 4
    3 1 4 2
    1 4 2 3
    3 4 1 2
    3 2 1 4
    4 1 3 2
    3 2 1 4
    4 2 1 4
    0 0
    
    Expected output
    Election #1
       2 bad ballot(s)
       Candidate 3 is elected.
    Election #2
       2 bad ballot(s)
       The following candidates are tied: 1 3 
    Election #3
       1 bad ballot(s)
       Candidate 3 is elected.
    
  2. Example 2

    Input
    1 1
    1
    0 0
    
    Expected output
    Election #1
       Candidate 1 is elected.
    
  3. Example 3

    Input
    2 3
    1 2
    1 2
    2 1
    0 0
    
    Expected output
    Election #1
       Candidate 1 is elected.