• [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;
    }
  • 相关阅读:
    php xml解析方法
    phpmail 发送邮件失败
    善用Eclipse的代码模板功能
    php5.3 namespace
    MyEclipse6.5配置反编译插件
    程序bug致损失400亿,判程序员坐牢? 搞笑我们是认真的
    ios学习笔记(二)第一个应用程序--Hello World
    ios学习笔记(一)Windows7上使用VMWare搭建iPhone开发环境
    GJB150-2009军用装备实验室环境试验方法新版标准
    AXI总线简介
  • 原文地址:https://www.cnblogs.com/evenbao/p/9319918.html
Copyright © 2020-2023  润新知