POJ 1703 Find them,Catch them

Posted evenbao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    
}

 

以上是关于POJ 1703 Find them,Catch them的主要内容,如果未能解决你的问题,请参考以下文章

POJ1703 Find them, Catch them

poj-1703-Find them, Catch them

poj 1703 Find them, Catch them

(POJ 1703)Find them, Catch them

POJ 1703 Find them,Catch them

[并查集] POJ 1703 Find them, Catch them