种类并查集!!!
这里使用的两段区间求法!!两种种类并查集比较好理解,就是和你不同类的两个就是同类!!
表情分界线
AC代码如下:
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> #define inf 2000000000 #define linf 1000000000000000000LL #define dinf 1e200 #define pb push_back #define mp make_pair #define ll long long #define sq(a) ((a)*(a)) #define ff(i,xi,n) for(int i=xi;i<=(int)(n);++i) #define ffd(i,xi,n) for(int i=xi;i>=(int)(n);--i) #define ffl(i,r) for(int i=head[r];i!=-1;i=edge[i].next) #define ms(i,j) memset(i,j,sizeof(i)) #define M 200010 using namespace std; int fa[M]; int findfa(int a) { return a==fa[a]?a:fa[a]=findfa(fa[a]); } int main() { int n,m; int t; char c; int a,b; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m);getchar(); ff(i,0,2*n) fa[i]=i; ff(i,1,m) {scanf("%c%d%d",&c,&a,&b);getchar(); if(c=='A'){ if(findfa(a)==findfa(b)) {printf("In the same gang.\n");continue;} if(findfa(a+n)==findfa(b)||findfa(a)==findfa(b+n)) {printf("In different gangs.\n");continue;} printf("Not sure yet.\n"); } else { fa[findfa(a)]=findfa(b+n);fa[findfa(b)]=findfa(a+n);//把不同类的放到后面区间去。 } } } return 0; }
POJ 1703 Find them, Catch them,布布扣,bubuko.com
POJ 1703 Find them, Catch them
原文:http://blog.csdn.net/hanhai768/article/details/38252315