아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Ranked Choice Spoiling

시간 제한1초메모리 제한1024 MB

요약
두세 명 후보에 대한 유권자 순위가 주어질 때, 새 후보 Z를 모든 순위에 끼워 넣어 A가 당선되도록 만들 수 있는지 판정한다.
난이도

보통10점 중 5점

유형
시뮬레이션, 완전 탐색, 조합론
정답자
아직 제출이 없습니다

문제

Ranked choice voting, a way of determining the winner of an election, proceeds as follows:

  1. Each candidate (identified in this problem as A, B, C, etc.) is placed on the ballot.
  2. Each voter ranks the candidates from most to least favorite (e.g., B first, then A, then C last) and submits that ranking as their vote.
  3. Once all votes have been received, the first place votes for each candidate remaining on the ballot are tallied. If there is a candidate with more than half of the first place votes, that candidate is declared the winner.
  4. If no candidate has more than half of the first place votes, the candidate with the fewest first place votes is eliminated from the ballot, and the process returns to step 3 with the remaining candidates. (If there are multiple such candidates, the loser is the lexicographically last candidate, e.g., C is eliminated before B.)

Spoiling refers to an additional candidate Z entering a race, thereby causing the winner to switch from one existing candidate to another candidate (who is not Z). Proponents of ranked choice voting claim that this type of election is less vulnerable to spoiling than the more common plurality voting (the candidate with the most votes wins).

You are candidate A in an election against one or two other candidates, and you wish to prove them wrong by engineering a candidate Z to enter the election and spoil it in your favor. Suppose you knew every voter's rankings for the existing candidates, and you were capable of engineering a candidate Z such that you had full control over where each voter inserts Z into their rankings. For a given set of such rankings, is it possible to engineer a candidate Z to spoil the election in your favor?

입력

The first line contains two integers nn (1≤n≤1,0001 \leq n \leq 1\\,000) and kk (2≤k≤32 \leq k \leq 3), where nn is the number of voters and kk is the number of existing candidates.

Each of the next nn lines contains kk space-separated capital letters (A, B, or C) indicating the rankings of each voter from first to last place. Each line will list each candidate exactly once (when k=2k = 2, C is not present).

출력

Output a single integer, 11 if it is possible to create a candidate Z who spoils the election in favor of candidate A (or if A would already win the election), 0 otherwise.

예제3

  1. 예제 1

    입력
    5 2
    B A
    A B
    A B
    B A
    B A
    
    예상 출력
    1
    
  2. 예제 2

    입력
    1 3
    A B C
    
    예상 출력
    1
    
  3. 예제 3

    입력
    8 3
    A B C
    B C A
    B C A
    B C A
    C B A
    C B A
    C B A
    C B A
    
    예상 출력
    0