2018 East Central Regional Contest/ ECNA2018 C.Pear-wise Voting (dfs)

8 篇文章 0 订阅

Problem Description

Bob Roberts is ending his term as president of a local group of fruit aficionados call the Pear-wise Club. Being president is a plum position so many people are running, some of whom Bob thinks are just peachy, while others are just the pits. After some thought, Bob has amended the by-laws of the Pear-wise Club to use sequential pairwise voting to determine his successor.

Sequential pairwise voting works as follows: every voter presents a ballot which ranks the candidates from most to least favorable. Then two of the candidates are selected to hold a one-on-one contest with these ballots, using the relative positions of the candidates on each ballot to determine how each voter would vote in this contest. The winner of this contest then takes part in a second one-on-one contest with a third candidate; the winner of that takes part in a third contest with a fourth candidate, and so on. The winner of the election is the winner of the last one-on-one contest.

The order in which the candidates take part in the one-on-one contests is specified by the agenda, which is a pre-selected ordering of the candidates. The first two candidates on the agenda take part in the first contest; the winner of that contest then faces the third candidate on the agenda, an so on. For example, assume we had the following set of 13 ballots, each ranking five candidates A, B, C, D and E:

(4) (3) (3) (2) (1)
A D C B E
D C A D B
C A D C C
B B B A D
E E E E A

The number above each ballot indicates the number of voters who submitted that ballot. If the agenda is ABCDE, then sequential pairwise voting would proceed as follows: in the first contest between A and B, A wins 10–3; A would then face C (the third candidate on the agenda) and here C wins 9–4; C would then face D and D wins 9–4; and finally D would face E and D would win 12–1, so D would be the overall winner using this agenda. Using the reverse agenda – EDCBA – would result in A winning the election (we’ll let you figure out the details of that one).

Now it wasn’t out of any political science reasons that Bob picked sequential pairwise voting, or the co- incidence of its name. He knows that the results are highly dependent on the agenda that’s used and as current president, Bob gets to set the agenda! Since he’s also aware of everyone’s voting preferences, he’s pretty sure that he can find an agenda so that his preference for who succeeds him will be elected. Just to be sure, he would like you to write a program which, when given a set of ballots as input, determines, for each candidate, whether there is an agenda which will result in his/her winning the election.

Input
Input begins with a line containing two positive integers n m (n ≤ 26, m ≤ 2 000) indicating the number of candidates and the number of unique ballots, respectively. Candidates are labeled with the first n letters of the alphabet, uppercase. Each of the next m lines displays one of the m ballots. Each line starts with a positive integer p (p ≤ 100) indicating the number of voters who submitted the ballot, following by a string of n characters specifying the ballot. This upper-case string is a permutation of the first n characters of the English alphabet, with the first character in the string indicating the most favored candidate in the ballot, the second character the second-most favored, and so on. The total number of votes over all ballots will be odd, so there can’t be a tie in any pairwise vote.

Output
For each candidate display the candidate’s letter followed by a colon, followed by either the phrase can win if an agenda exists where the candidate can win, or the phrase can’t win if no such agenda exists.

Sample Input

5 5
4 ADCBE
3 DCABE
3 CADBE
2 BDCAE
1 EBCDA

Sample Output

A: can win
B: can’t win
C: can win
D: can win
E: can’t win

Sample Input2

3 3
1 ABC
1 BCA
1 CAB

Sample Output2

A: can win
B: can win
C: can win

Solution

 T若能胜利,则T联合T能够打败的对手U,再联合U可以打败的对手V, 再联合…可以打败所有的参赛者

AC Code

#include<bits/stdc++.h>

using namespace std;

int n, m, p, sum;
int strongerThan[30][30];
char vote[30];
bool visited[30];

void dfs(int currId) {
    if (visited[currId])
        return;
    visited[currId] = true;
    for (int i = 0; i < n; ++i)
        if (strongerThan[currId][i])
            dfs(i);
}

bool canWin(int currId) {
    memset(visited, 0, sizeof(visited));
    dfs(currId);
    for (int i = 0; i < n; ++i)
        if (!visited[i])
            return false;
    return true;
}

int main() {
    scanf("%d%d", &n, &m);
    while (m--) {
        scanf("%d", &p);
        scanf("%s", vote);
        for (int i = 0; i < n; ++i)
            for (int j = i; j < n; ++j)
                strongerThan[vote[i] - 'A'][vote[j] - 'A'] += p;
        sum += p;
    }
    sum = (sum + 1) >> 1;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            strongerThan[i][j] = strongerThan[i][j] >= sum ? 1 : 0;
    for (int i = 0; i < n; ++i)
        if (canWin(i))
            printf("%c: can win\n", 'A' + i);
        else printf("%c: can't win\n", 'A' + i);
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值