2017 Benelux Algorithm Programming Contest (BAPC 17) K. King of the Waves

题目链接

K King of the Waves
Picture by JD Lasica via Flickr.
You are organising a king of the hill tournament, the Buenos Aires
Paddleboarding Competition (BAPC), with n participants. In a
king of the hill tournament, one person starts as a “king” and is
then challenged by another person, the winning person becomes
the new king. This is repeated until all participants have challenged
exactly once (except for the starting person). In a paddleboarding
match, there are no draws. The person which ends up as
king, wins the tournament. Since you are the organiser, you get to
choose the starting person and the order in which they challenge
the king.
Someone is offering you a substantial amount of money in case
one of the participants, Henk, ends up winning the tournament. You happen to know, for
any two participants x and y, which of the two would win if they were to match during the
tournament. Consequently, you choose to do the unethical: you will try to rig the game. Can
you find a schedule that makes Henk win the tournament?
Input
• The first line contains an integer 1 ≤ n ≤ 1000, the number of participants. The
participants are numbered 0, … , n − 1, where Henk is 0.
• Then n lines follow, where each line has exactly n characters (not counting the newline
character). These lines represent the matrix with the information of who beats who, as
follows. On line i the jth character is (note that 0 ≤ i, j < n):
– ’1’ if person i will win against person j.
– ’0’ if person i will lose against person j.
– ’X’ if i = j.
Output
Print a sequence of participants, such that the first person starts as king and the consequent
participants challenge the king. If there is no way to rig the game such that Henk wins, print
“impossible”.

题目描述:

已知n个人(0 ~ n-1),和一个n行n列矩阵:

  • ‘1’ 代表 i 能赢 j
  • ‘0’ 代表 j 能赢 i
  • ‘X’ 代表平局

安排一个n个人都出场出场顺序,保证最后0赢

Solution:

由于只需要保证0能赢即可,那么对于 i (1 <= i <= n-1) 这个人,必须要保证要么被0打败,要么被0所打败的人打败,所以只需要对0所打败的人和0打败的人所打败的人进行遍历(不需要关心他们是否能赢0,只要保证在遇到0之前会输掉即可),如果所有点都能扫到,就说明0能赢所有人

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <queue>
using namespace std;
typedef long long LL;
const int MaxN = 1e5 + 5;
const int Mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;

vector <int> G[1005];
int n;
int inseq[MaxN], seq[MaxN];
char s[1005];

void solve() {
    int head = 1, tail = 0; 
    seq[++tail] = 0, inseq[0] = 1;
    while(head <= tail) {
        int now = seq[head];
        for(int i = 0; i < G[now].size(); i++) {
            int u = G[now][i];
            if(!inseq[u]) seq[++tail] = u, inseq[u] = 1;
        }
        head++;
    }
    if(tail < n) printf("impossible\n");
    else {
        for(int i = tail; i >= 1; i--) printf("%d ", seq[i]);
        printf("\n");
    }
}
int main() {
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%s", s);
        for(int j = 0; j < n; j++) {
            if(s[j] == '0') G[j].push_back(i);
            if(s[j] == '1') G[i].push_back(j);
        }
    }
    solve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Jasmineaha/article/details/80349001