2019-06-13  2024-09-15    721 字  2 分钟

POJ-1703 Find them, Catch them

并查集

数组建两倍于n的长度,分为两段,两段对应位置属于同一个人

D 时:

将第一段的p和第二段的q相互连接,同时第一段的q和第二段的p相互连接,代表 pq 属于不同帮派。

A 时:

先判断在第一段内 pq 是否相互连接,如果连接则一定属于同一帮派,因为 pq 必定以某个敌对帮派的人物为桥梁才建立的连接。

如果不相连接则再判断 pq + n 是否相连接,如果连接则一定属于不同帮派

如果上述都不满足则 pq 关系不确定

Code
/**
 *    author: Akvicor
 *    created: 2019-06-13 21-00-16
**/

#include <cstdio>
#include <cstring>
#include <iomanip>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <complex>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>

using namespace std;

#ifdef DEBUG
string to_string(string s) {
	return '"' + s + '"';
}

string to_string(const char* s) {
	return to_string((string) s);
}

string to_string(bool b) {
return (b ? "true" : "false");
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
	return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A>
string to_string(A v) {
	bool first = true;
	string res = "{";
	for (const auto &x : v) {
		if (!first) {
			res += ", ";
		}
		first = false;
		res += to_string(x);
	}
	res += "}";
	return res;
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cerr << " " << to_string(H);
	debug_out(T...);
}
#endif

#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 17
#endif

#ifdef DEBUG
#define FAST_IO 17
#define cout cout << "-->"
#else
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl '\n'
#endif

#define LL long long
#define ULL unsigned long long
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define reep(i, n) for(int i = 0; i <= (n); ++i)
#define lop(i, a, n) for(int i = a; i < (n); ++i)
#define loop(i, a, n) for(int i = a; i <= (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define VI vector<int>
#define PII pair<int,int>
#define FI first
#define SE second
#define SZ(x) ((int)(x).size())

const double EPS = 1e-6;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const LL LINF = 0x7f7f7f7f7f7f7f7f;
const int MAXN = (int)1e6 + 10;
const int MOD = (int)1e9 + 7;

class UnionFind{
	private:
		int * parent;
		int * rank;
		int count;
	public:
		UnionFind(int n){
			n *= 2;
			n += 10;
			parent = new int[n];
			rank = new int[n];
			this->count = n;
			for(int i = 0; i < n; ++i){
				parent[i] = i;
				rank[i] = 1;
			}
		}
		~UnionFind(){
			delete[] parent;
			delete[] rank;
		}

		int find(int p){
			while(p != parent[p]){
				p = parent[p];
			}
			return p;
		}

		bool isConnected(int p, int q){
			return find(p) == find(q);
		}

		void unionElements(int p, int q){
			int pRoot = find(p);
			int qRoot = find(q);

			if(pRoot == qRoot) return;

			if(rank[pRoot] < rank[qRoot]){
				parent[pRoot] = qRoot;
			}else if(rank[qRoot] < rank[pRoot]){
				parent[qRoot] = pRoot;
			}else{
				parent[pRoot] = qRoot;
				rank[qRoot] += 1;
			}
		}

};

int t, n, m, p, q;
char c;

int main(){
	//FAST_IO;

	//cin >> t;
	scanf("%d\n", &t);
	while(t--){
		//cin >> n >> m;
		scanf("%d %d\n", &n, &m);
		UnionFind uf = UnionFind(n);
		while(m--){
			//cin >> c >> p >> q;
			scanf("%c %d %d\n", &c, &p, &q);
			if(c == 'D'){
				uf.unionElements(p, n+q);
				uf.unionElements(q, n+p);
			}else{
				if(uf.isConnected(p, q))
					cout << "In the same gang." << endl;
				else if(uf.isConnected(p, n+q))
					cout << "In different gangs." << endl;
				else cout << "Not sure yet." << endl;
			}

		}
	}

	return 0;
}