POJ1703 简单的种类并查集
2015-08-18 20:50
477 查看
#include<stdio.h> #include<string.h> #include<math.h> #include<queue> #include<vector> #include<iostream> #include<string> #include<set> #include<map> #include<algorithm> using namespace std; #pragma comment(linker, "/STACK:1024000000,1024000000") #define nn 100010 #define LL long long #define ULL unsiged long long #define mod 0x7fffffff #define inf oxfffffffffff #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 int f[nn]; bool sex[nn];//标记该节点和父亲节点的关系。 void init(int n) { for(int i=0;i<=n;i++) { f[i]=i; sex[i]=true; } } int find(int x,bool &se) { se=true; int rt=x; while(x!=f[x]) { if(sex[x]==false) se=!se; x=f[x]; } f[rt]=x; sex[rt]=se; return x; } int main() { int n,m,t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); init(n); while(m--) { char ch[10]; int a,b; scanf("%s%d%d",ch,&a,&b); bool la=false,lb=false; int fa=find(a,la); int fb=find(b,lb); if(ch[0]=='D') { f[fa]=fb; sex[fa]=la^lb;//异或 } else { if(fa==fb) { if(la==lb) printf("In the same gang.\n"); else printf("In different gangs.\n"); } else printf("Not sure yet.\n"); } } } return 0; }
相关文章推荐
- 转:理解C++11的模板类型推导
- 白酒香型
- [HDU 1281] 棋盘游戏 最大匹配
- 2015 Multi-University Training Contest 9(区间dp)
- StringGrid右击选中表格(发消息给句柄模拟点击,右键点击也是MouseDown)
- ZKW费用流修正
- 织梦DedeCms Runphp 标签中调用其他变量的方法
- linux curl 命令
- 编程技巧 - 3
- Redis介绍
- autolayout的使用原理及代码实现
- CODEVS 1132 瑞士轮
- .NET破解之PDFdo转换器
- HDOJ 1754 I Hate It(单点更新+区间最值)
- hdu5402
- Android 随手势滑动销毁(finish)Activity
- 苹果手机微信记录恢复最简单的恢复方法
- 树状数组模板
- 用Eclipse编写Android程序的代码智能提示功能
- js 触摸事件 touch