[POJ 2912] Rochambeau

Posted evenbao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POJ 2912] Rochambeau相关的知识,希望对你有一定的参考价值。

[题目链接]

        http://poj.org/problem?id=2912

[算法]

          并查集

[代码] 

        

#include <algorithm>  
#include <bitset>  
#include <cctype>  
#include <cerrno>  
#include <clocale>  
#include <cmath>  
#include <complex>  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
#include <ctime>  
#include <deque>  
#include <exception>  
#include <fstream>  
#include <functional>  
#include <limits>  
#include <list>  
#include <map>  
#include <iomanip>  
#include <ios>  
#include <iosfwd>  
#include <iostream>  
#include <istream>  
#include <ostream>  
#include <queue>  
#include <set>  
#include <sstream>  
#include <stdexcept>  
#include <streambuf>  
#include <string>  
#include <utility>  
#include <vector>  
#include <cwchar>  
#include <cwctype>  
#include <stack>  
#include <limits.h> 
using namespace std;
#define MAXN 510
#define MAXM 2010

int i,n,m,tot,ans,pos;
int fa[MAXN*3],a[MAXM],c[MAXM];
char b[MAXM];
char ch;

inline int get_root(int x)
{
    if (fa[x] == x) return x;
    return fa[x] = get_root(fa[x]);
}
inline bool ok(int id)
{
    int i;
    for (i = 0; i <= 3 * n; i++) fa[i] = i;
    for (i = 1; i <= m; i++)
    {
        if (a[i] == id || c[i] == id) continue;
        if (b[i] == =) 
        {
            if (get_root(a[i]) == get_root(c[i]+n))     
            {
                ans = max(ans,i);
                return false;
            }
            if (get_root(a[i]+n) == get_root(c[i])) 
            {
                ans = max(ans,i);
                return false;
            }
            fa[get_root(a[i])] = get_root(c[i]);
            fa[get_root(a[i]+n)] = get_root(c[i]+n);
            fa[get_root(a[i]+2*n)] = get_root(c[i]+2*n);
        } else if (b[i] == <)
        {
            if (get_root(a[i]) == get_root(c[i])) 
            {
                ans = max(ans,i);
                return false;
            }
            if (get_root(a[i]+n) == get_root(c[i]))
            {
                ans = max(ans,i);
                return false;
            }
            fa[get_root(a[i])] = get_root(c[i]+n);
            fa[get_root(c[i])] = get_root(a[i]+2*n);
            fa[get_root(a[i]+n)] = get_root(c[i]+2*n);
        } else if (b[i] == >)
        {
            if (get_root(a[i]) == get_root(c[i]))
            {
                ans = max(ans,i);
                return false;
            }
            if (get_root(a[i]) == get_root(c[i]+n))
            {
                ans = max(ans,i);
                return false;
            }
            fa[get_root(a[i]+n)] = get_root(c[i]);
            fa[get_root(c[i]+2*n)] = get_root(a[i]);
            fa[get_root(a[i]+2*n)] = get_root(c[i]+n); 
        }
    }      
    return true;
}

int main()
{
    
    while (scanf("%d%d",&n,&m) != EOF)
    {
        ans = 0;
        for (i = 1; i <= m; i++) 
        {
            scanf("%d",&a[i]);
            while ((ch = getchar()) ==  );
            b[i] = ch;
            scanf("%d",&c[i]);
        }
         tot = 0;
        for (i = 0; i < n; i++)
        {
            if (ok(i)) 
            {
                tot++;
                pos = i;
            }
        }
        if (tot > 1) printf("Can not determine
");
        else if (tot == 1) printf("Player %d can be determined to be the judge after %d lines
",pos,ans);
        else printf("Impossible
");
    }

    return 0;
}

 

以上是关于[POJ 2912] Rochambeau的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2912 - Rochambeau - [暴力枚举+带权并查集]

POJ 2912 Rochambeau

POJ2912 Rochambeau [扩展域并查集]

poj 2912 Rochambeau(枚举+带权并查集)

poj2912(带权并查集+枚举)

poj2912(种类并查集+枚举)