[POJ2912]Rochambeau

题目

传送门

题解

听机房大佬说我们以前做食物链做过,然而我太弱了只知道建虚点

曾经很早以前做过的食物链是这道题的弱化版 所以这道题就不能建虚点了吧

但是我曾经做食物链用的虚点,所以这道题做不来qwq

后来经机房大佬 \(\text{SXY}\) 指导,这是一道关系并查集的经典题型。

所谓关系并查集,就是用边权表示 \(u\)\(fa[u]\) 的关系。

这里,如果我们使用:

  • \(a<b\)\(fa[a]=b,w[a]=1\)
  • \(a>b\)\(fa[a]=b,w[a]=2\)
  • \(a=b\)\(fa[a]=b,w[a]=0\)

来表示,我们就会发现这种表示方法是自洽的。

其实类似的还有机房大佬 \(\text{JZM}\) 提出的,关于 [NOI Online #1 提高组]序列 的一种新方法,感觉十分巧妙,这里可以去康一康。

还是回到这道题,由于裁判的出拳是随机的,也就是说关于裁判的所有划拳方法都是错误的,我们就不能考虑加入裁判的关于其他点的关系。

我们怎么找到裁判?

考虑数据范围:\(N\le500,M\le 2000\),那么我们可以暴力枚举哪个人是裁判,如果没有冲突,那么这个人就是裁判;如果没有人,那么输出 Impossible;如果有多于 \(1\) 个人,那么输出 Can not determine

现在,假设我们找到裁判了,那么我们又怎么确定在哪一句话时我们能够确定最终的裁判呢?

正难则反,如果我们不能找到裁判是在哪句话确定的,那么我们就找在哪句话及之前,我们能够将其他 \(n-1\) 个人能当裁判的情况排除,这个很好维护,只需要维护一个 ans2 ,每次出现冲突时,取最大值即可。

代码

#include<iostream>
#include<cstdio>
using namespace std;

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#ifdef _GLIBCXX_CSTDIO
#define cg (c=getchar())
template<class T>inline void qread(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T qread(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
#undef cg
#endif
// template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MAXN=500;
const int MAXM=2000;
const int MOD=3;

struct option{
    int x,y;char opt;
}p[MAXM+5];
int n,m;

inline void Init(){
    rep(i,1,m)cin>>p[i].x>>p[i].opt>>p[i].y;
}

int ans1,ans2,tot;

int fa[MAXN+5],w[MAXN+5];
int findSet(const int u){
    if(fa[u]^u){
        int t=fa[u];
        fa[u]=findSet(fa[u]);
        w[u]=(w[u]+w[t])%MOD;
    }return fa[u];
}
inline void makeSet(){
    rep(i,0,n-1)w[i]=0,fa[i]=i;
}

inline int link(const int u,const int v,const int d){
    int x=findSet(u),y=findSet(v);
    if(x==y){
        if((w[u]-w[v]+MOD)%MOD!=d)return 0;
        return 1;
    }
    fa[x]=y;
    w[x]=(-w[u]+w[v]+d+MOD)%MOD;
    return 1;
}

inline void work(const int ignore){
    makeSet();
    rep(i,1,m)if(p[i].x!=ignore && p[i].y!=ignore){
        if(p[i].opt=='<'){
            if(!link(p[i].x,p[i].y,1)){
                ans2=Max(ans2,i);
                return;
            }
        }else if(p[i].opt=='>'){
            if(!link(p[i].x,p[i].y,2)){
                ans2=Max(ans2,i);
                return;
            }
        }else{
            if(!link(p[i].x,p[i].y,0)){
                ans2=Max(ans2,i);
                return;
            }
        }
    }++tot,ans1=ignore;
}

signed main(){
    while(cin>>n>>m){ans1=ans2=tot=0;
        Init();
        rep(i,0,n-1)work(i);
        if(tot==0)puts("Impossible");
        else if(tot>1)puts("Can not determine");
        else printf("Player %d can be determined to be the judge after %d lines\n",ans1,ans2);
    }
	return 0;
}

猜你喜欢

转载自www.cnblogs.com/Arextre/p/12926109.html
POJ