[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\n");
        else if (tot == 1) printf("Player %d can be determined to be the judge after %d lines\n",pos,ans);
        else printf("Impossible\n");
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/evenbao/p/9319918.html
POJ