BAPC 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 leves 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≤500002≤N≤50000) and M (1≤M≤2500001≤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,0≤K,L<N). If the symbol is ‘=’, then the game between KK 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.

在这里插入图片描述

题目大意:
有N个数(编号为0~N-1),然后给出M条关系,只有“=”与“>”这两种关系,”A=B“关系,表示A与B是相等的关系,“A>B”的关系是表示A大于B的关系,如果给出的所有关系中不存在冲突就是输出“consistent”,否则就输出“inconsistent”

分析:
一开始我以为是用类似POJ 1703这道题目相似,但是怎么都处理不了,因为POJ那道题目是没有单向关系,而这道题目是单向关系,因此处理不了,需要去建图,然后判断;

仔细想一想,如果所有的相等关系,我们就把其认为是相同的点,如果不是相等的关系,就算作一个新点,如果是一个consistent的输入,那么最后这个图一定是一个有向无环图,反之,若是一个inconsistent样例,那么这个图就是一个有环图了;

那么接下来就是要处理相同的点,相同的点可以用并查集来实现,这样就将所有的点预处理完,然后去建图,判断是否存在环即可。

#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <cstring>
#include <stack>
#include <string>
#include <cstdio>
using namespace std;

typedef long long ll;
const int N = 1e6+199;
const double Pi = acos(-1);
int f[N];
int Findf(int x){
    if(x==f[x])
        return x;
    else return f[x]=Findf(f[x]);
}
void Merge(int x,int y){
    int xx=Findf(x);
    int yy= Findf(y);
    if(xx!=yy) f[yy]=xx;
}
bool Same(int x,int y){
    int xx=Findf(x);
    int yy=Findf(y);
    return xx==yy;
}
struct node{
    int from,to;
};
vector<node>ve;
int flag=0;
int du[N];
vector<int>edge[N];
void Top_sort(int n){//利用拓扑排序实现有向无环图的判断
    queue<int>q;
    for(int i=0;i<n;i++)
        if(du[i]==0){
            q.push(i);
        }

    int cnt=q.size();
    int u;
    while(!q.empty()){
        u=q.front();
        q.pop();

        for(int i=0;i<edge[u].size();i++){
            if(--du[edge[u][i]]==0){
                q.push(edge[u][i]);
                cnt++;
            }
        }
    }
    if(cnt<n) flag=1;
}
int main()
{

//    #ifndef ONLINE_JUDGE
//    freopen("in.txt","r",stdin);
//    #endif // ONLINE_JUDGE
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n+10;i++) f[i]=i;
    char d;
    int x,y;
    int ans=0;
    for(int i=0;i<m;i++){
        scanf("%d %c %d",&x,&d,&y);
        if(d=='=')
            Merge(x,y);//相等关系的直接利用并查集合并
        else
        ve.push_back({x,y});//不相等的关系先存起来,因为需要给点从新编号
    }
    int cnt=0;
    for(int i=0;i<n;i++)
        if(f[i]==i) cnt++;//cnt表示新编号的点的个数

    int from,to;
    for(int i=0;i<ve.size();i++){
        from=Findf(ve[i].from);
        to=Findf(ve[i].to);
        if(Same(from,to)){
            flag=1;
            break;
        }
        edge[from].push_back(to);
        du[to]++;
    }
    Top_sort(cnt);
    if(flag)
        printf("inconsistent\n");
    else
        printf("consistent\n");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值