【题目链接】
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.\n"); else if (get_root(x+n) == get_root(y)) printf("In different gangs.\n"); else printf("Not sure yet.\n"); } else { scanf("%d%d",&x,&y); fa[get_root(x+n)] = get_root(y); fa[get_root(y+n)] = get_root(x); } } } return 0; }
原文地址:https://www.cnblogs.com/evenbao/p/9275492.html
时间: 2024-11-10 08:27:33