zoukankan      html  css  js  c++  java
  • [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;
    }
  • 相关阅读:
    Add函数求两个数的和,不能使用+号或其它算术运算符
    小刘同学的第八十六篇博文
    小刘同学的第八十五篇博文
    小刘同学的第八十四篇博文
    小刘同学的第八十三篇博文
    小刘同学的第八十二篇博文
    小刘同学的第八十一篇博文
    小刘同学的第八十篇博文
    小刘同学的第七十九篇博文
    小刘同学的第七十八篇博文
  • 原文地址:https://www.cnblogs.com/evenbao/p/9319918.html
Copyright © 2011-2022 走看看