题目大意:
和食物链一样,该题询问的是是否在一个帮派中,
A a b : 询问ab是否在一个帮派中;
B a b : 告知你a b不是一个帮派的;
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <bitset>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <iostream>
#include <algorithm>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define maxn 1010
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(x,y) memset(x,y,sizeof(x))
#define rep(i,n) for(int i=0;i<(n);i++)
#define repf(i,a,b) for(int i=(a);i<=(b);i++)
#define PI pair<int,int>
//#define mp make_pair
#define FI first
#define SE second
#define IT iterator
#define PB push_back
#define Times 10
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int ,int > P;
//#define N 100
const double eps = 1e-10;
const double pi = acos(-1.0);
const ll mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const ll INF = (ll)1e18+300;
const int maxd = 501000 + 10;
//int num_a,num_b;
//int A_fir[maxd] ;
//int A_sec[maxd] ,B_fir[maxd] ,B_sec[maxd];
//int res_A = 0;
//int res_B = 0;
map<string, bool> mp;
//map<string, int> mmp;
//int ene_num[maxd];
int mapp[maxd][maxd];
//string fir, last;
int n, m;
int fa[maxd];
int depth[maxd];
void init() {
for (int i = 0; i < maxd ; i++) {
fa[i] = i;
depth[i] = 1;
}
}
int find(int x) {
if(x != fa[x]) {
fa[x] = find(fa[x]);
}
return fa[x];
}
bool same(int a, int b) {
if(find(a) == find(b)) {
return true;
}
else {
return false;
}
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if(x == y) {
return;
}
if(depth[x] < depth[y]) {
fa[x] = y;
}
else {
fa[y] = x;
if(depth[x] == depth[y]) {
depth[x] ++;
}
}
}
int main() {
//ios::sync_with_stdio(false);
int t;
cin >> t;
while(t --) {
init();
scanf("%d%d", &n, &m);
getchar();
for (int i = 0; i < m; i++) {
char c;
int a, b;
//cin >> c >> a >> b;
scanf("%c%d%d",&c, &a, &b);
getchar();
//cout << c << a << b << endl;
if(c == 'A') {
//cout << find(a) << " " << find(a + n) << " " << find(b) << " " << find(b + n) << endl;
if(find(a) == find(b + n)) {
cout << "In different gangs." << endl;
}
else if( find(a) == find(b) ) {
cout << "In the same gang." << endl;
}
else {
cout << "Not sure yet." << endl;
}
}
else if(c == 'D') {
unite(a, b + n);
unite(a+n, b);
}
}
}
}