POJ 2912 Rochambeau 并查集+枚举

11 篇文章 0 订阅
4 篇文章 0 订阅
该博客介绍了一种利用并查集和枚举方法来解决POJ 2912题目的策略。通过不参与矛盾判断的裁判枚举,当没有矛盾的组数为0时标记为Inpossible,大于1表示无法确定,等于1则可确定裁判。最大矛盾轮数决定了结果。
摘要由CSDN通过智能技术生成

和食物链那题很像,只要把裁判枚举一遍即可,当前枚举的裁判不参与矛盾判断,没有矛盾的组数为0时,即Inpossible,大于1时就是Can not determine,等于1时是可以确定裁判的,裁判即没有出现矛盾的那一组枚举的人,判断轮数即所有枚举情况中的出现矛盾的最大轮数。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<set>
#include<bitset>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#define INF 0x3f3f3f3f
#define inf 2*0x3f3f3f3f
#define llinf 1000000000000000000
#define pi acos(-1.0)
#define mod 1000000007
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define lb(x) (x&-x)
#define gi(x) scanf("%d",&x)
#define gi2(x,y) scanf("%d%d",&x,&y)
#define gll(x) scanf("%lld",&x)
#define gll2(x,y) scanf("%lld%lld",&x,&y)
#define gc(x) scanf("%c",&x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>P;
/***********************************************/
int par[505*3],ranks[505*3],n,m,x,y;
struct node
{
    int x,y;
    char c;
}qu[2005];
void init(int n)
{
    rep(i,0,n)par[i]=i,ranks[i]=0;
}
int finds(int x)
{
    if(par[x]==x)return x;
    return par[x]=finds(par[x]);
}
void unite(int x,int y)
{
    x=finds(x);y=finds(y);
    if(ranks[x]>ranks[y])par[y]=x;
    else par[x]=y;
    if(ranks[x]==ranks[y])ranks[y]++;
}
bool same(int x,int y)
{
    return finds(x)==finds(y);
}
int main()
{
    while(gi2(n,m)!=EOF)
    {
        rep(i,0,m)
        {
            gi(qu[i].x);
            gc(qu[i].c);
            gi(qu[i].y);
        }
        int flag=0,num=0,round=0;
        rep(i,0,n)
        {
            int j=0;init(3*n);
            for(;j<m;j++)
            {
                if(qu[j].x==i||qu[j].y==i)continue;
                int x=qu[j].x,y=qu[j].y;
                if(qu[j].c=='<')
                {
                    if(same(x+n,y)||same(x,y)){round=max(round,j+1);break;}
                    else unite(x,y+n),unite(x+n,y+2*n),unite(x+2*n,y);
                }
                else if(qu[j].c=='>')
                {
                    if(same(y+n,x)||same(x,y)){round=max(round,j+1);break;}
                    else unite(y,x+n),unite(y+n,x+2*n),unite(y+2*n,x);
                }
                else
                {
                    if(same(x,y+n)||same(x+n,y)){round=max(round,j+1);break;}
                    else unite(x,y),unite(x+n,y+n),unite(x+2*n,y+2*n);
                }
            }
            if(j==m)flag++,num=i;
        }
        if(flag==0)printf("Impossible\n");
        else if(flag==1)printf("Player %d can be determined to be the judge after %d lines\n",num,round);
        else printf("Can not determine\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值