zoukankan      html  css  js  c++  java
  • 【POJ 1703】 Find them,Catch them

    【题目链接】

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

    【算法】

              并查集 + 拆点

    【代码】

              

    #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;
    const int MAXN = 1e5 + 10;
    
    int i,T,x,y,n,m;
    int fa[MAXN<<1];
    char opt[5];
    
    inline int get_root(int x)
    {
            if (fa[x] == x) return x;
            return fa[x] = get_root(fa[x]);
    }
    
    int main() 
    {
            
            scanf("%d",&T);
          while (T--)
          {
                  scanf("%d%d",&n,&m);
                    for (i = 1; i <= 2 * n; i++) fa[i] = i;
                    for (i = 1; i <= m; i++)
                    {
                            scanf("%s",&opt);
                            if (opt[0] == 'A')
                            {
                                    scanf("%d%d",&x,&y);
                                    if (get_root(x) == get_root(y)) printf("In the same gang.
    ");
                                    else if (get_root(x+n) == get_root(y)) printf("In different gangs.
    ");
                                    else printf("Not sure yet.
    ");        
                            }    else
                            {
                                    scanf("%d%d",&x,&y);
                                    fa[get_root(x+n)] = get_root(y);
                                    fa[get_root(y+n)] = get_root(x);
                            }
                    }        
            }
            
            return 0;
        
    }
  • 相关阅读:
    javascript字符串加密解密函数
    javascript实现blob加密视频源地址
    HTML网页实现flv视频播放
    DELL r720远控装系统
    nginx笔记
    Centos7防火墙配置
    CentOS7.x搭建LNMP
    搭建可道云私人云盘系统
    网络设备巡检常用命令-摘自星球成员马磊分享
    部署Windows Server 2012的WSUS补丁服务器
  • 原文地址:https://www.cnblogs.com/evenbao/p/9275492.html
Copyright © 2011-2022 走看看