POJ - 1703 Find them, Catch them

题意:

警察需要将犯人分类(只有两派),D[a][b]表示a,b犯人是不同一派的,A[a][b]表示输入a,b犯人的关系,关
系有三种:1.无法确定 2.同一派 3.不同派

分析:

种类并查集,只要输入D,就将a,b两个合并,并将改他们的关系(g[px]=g[x]^g[y]^1),输入A的时候判断
a,b是否合并过,没有输出无法确定,合并过再判断是不是同一派。(代码里有注释)

code:

#define debug
#include<stdio.h>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<functional>
#include<iomanip>
#include<map>
#include<set>
#define pb push_back
#define dbg(x) cout<<#x<<" = "<<(x)<<endl;
#define lson l,m,rt<<1
#define cmm(x) cout<<"("<<(x)<<")";
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long ll;
const int maxn=1e5;
const ll INF=0x3f3f3f3f;
const ll inf=0x7fffff;
const int mod=1e9+7;
const int MOD=10007;
//----
//define
int f[maxn+10];
int g[maxn+10];
//findset
int findset(int x) {
	if(f[x]!=x) {
		int p=f[x];
		f[x]=findset(f[x]);
		g[x]^=g[p];
	}
	return f[x];
}
//unite
void unite(int x,int y) {
	int px=findset(x),py=findset(y);
	if(px==py)return;
	f[px]=py;
	g[px]=g[y]^g[x]^1;//更新x的父节点跟y(y的父节点)的关系
}
void init(int n) {
	for(int i=1; i<=n; i++) {
		f[i]=i;
		g[i]=0;//0表示同派 
	}
}
//solve
void solve() {
	int T;
	scanf("%d",&T);
	while(T--) {
		int n,m,x,y;
		char mark;
		scanf("%d%d",&n,&m);
		getchar();//一定要加 
		init(n);
		for(int i=0; i<m; i++) {
			scanf("%c%d%d",&mark,&x,&y);
			getchar();//一定要加 
			if(mark=='D') {
				unite(x,y);
			} else {
				if(findset(x)==findset(y))
					puts(g[x]==g[y]?"In the same gang.":"In different gangs.");
				else puts("Not sure yet.");
			}
		}
	}
}

int main() {
//	ios_base::sync_with_stdio(0);
#ifdef debug
	freopen("in.txt","r",stdin);
//	freopen("out.txt","w",stdout);
#endif
//	cin.tie(0);
//	cout.tie(0);
	solve();
	return 0;
}

猜你喜欢

转载自www.cnblogs.com/visualVK/p/9147449.html
今日推荐