Chess Tournament(并查集+拓扑排序)

Chess Tournament

Your friend is an organizer of the International Chess Playing Championship. He is worried that some of the contestants may be cheating, and he has asked you to help out. The chess players are allowed to report matches to the jury themselves, and this is not checked with the reported opponent. So, it is possible for competitors to make up matches and falsely report themselves as the winners.

Since chess is a game of skill, and not of chance, a player will always beat their opponent if their skill level is higher. A game will result in a draw if and only if the two players’ skills are exactly equal.

However, the skill level of the players is not known. He has therefore asked you to write a program that, given a list of reported matches, determines whether this list is consistent or not. The list is inconsistent if we can determine that at least one reported match is falsely reported, otherwise it is consistent.

Input
The first line contains two integers N (2≤N≤50000) and M (1≤M≤250000), to describe a championship with N players and M reported matches.

The following M lines each consist of an integer K, a symbol which is either ‘=’ or ‘>’, and another integer L. The integers K and L each uniquely identify a player (0≤K,L<N). If the symbol is ‘=’, then the game between K and L was a draw. If the symbol is ‘>’, then K beat L in a match.

You may assume that there is at most one reported match between any given pair of players. Also, each player takes part in at least one reported match.

Output
Output a single line containing a single word: “consistent” if the list of recorded matches is consistent, and “inconsistent” if it is not.

Sample Input 1 Sample Output 1
3 3
0 > 1
1 = 2
0 = 2
inconsistent
Sample Input 2 Sample Output 2
5 5
0 = 1
1 = 2
3 = 4
0 > 3
1 > 4
consistent
Sample Input 3 Sample Output 3
6 5
0 > 1
1 > 2
3 = 4
4 = 5
5 > 3

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 4e5 + 10;
int fa[N], flag = 1, n, m, sum;
int a[N], b[N], d[N];
char c[N];
vector<int> ma[N];
int Find(int x) {
    if (x == fa[x]) return x;
    return fa[x] = Find(fa[x]);
}
void Merge(int x, int y) {
    x = Find(x);
    y = Find(y);
    if (x != y) {
        if (x < y) swap(x, y);
        fa[x] = y;
        sum--;
    }
}
void topsort() {
    queue<int> q;
    for (int i = 0; i < n; i++) {
        if (d[i] == 0 && fa[i] == i) q.push(i);
    }
    int num = 0;
    while (q.size()) {
        int x = q.front();
        // cout << x << ' ';
        q.pop();
        num++;
        for (int y : ma[x]) {
            if (--d[y] == 0) q.push(y);
        }
    }
    // cout << '\n' << num << " " << sum << '\n';
    if (num < sum)
        cout << "inconsistent\n";
    else
        cout << "consistent\n";
}
signed main() {
    ios::sync_with_stdio(false);
    cin >> n >> m;
    sum = n;
    for (int i = 0; i < n; i++) fa[i] = i;
    for (int i = 0; i < m; i++) {
        cin >> a[i] >> c[i] >> b[i];
        if (i == 0) continue;
        if (c[i] == '=') {
            Merge(a[i], b[i]);
        }
    }
    for (int i = 0; i < m; i++) {
        if (c[i] == '=') continue;
        int x = fa[a[i]];
        int y = fa[b[i]];
        ma[x].push_back(y);
        d[y]++;
    }
    topsort();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值