POJ 1703 Find them, Catch them【并查集】
来源:互联网 发布:软件界面的英文 编辑:程序博客网 时间:2024/06/07 19:07
点击打开链接
题意: 一个城市有两个帮派q次操作,
每次操作要么告诉你那两个人是敌对关系, 要么查询那两个人是敌对。
并查集, 查询与父节点的关系。
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <queue>using namespace std;const int inf=0xffffff;const int maxn=1e5+100;int fa[maxn];int sum[maxn];int n,m;int Find(int x){ if(fa[x]==x) return x; int t=fa[x]; fa[x]=Find(fa[x]); sum[x]=(sum[x]+sum[t])%2; return fa[x];}void Merge(int x,int y){ int xx=Find(x); int yy=Find(y); if(xx!=yy){ fa[xx]=yy; sum[xx]=(sum[y]+sum[x]+1)%2; }}int main(){ int T,a,b; char s[3]; scanf("%d",&T); while(T--){ memset(sum,0,sizeof(sum)); scanf("%d %d",&n,&m); for(int i=1;i<=n;++i) fa[i]=i; while(m--){ scanf("%s %d %d",s,&a,&b); if(s[0]=='A'){ int x=Find(a),y=Find(b); if(n==2) printf("In different gangs.\n"); else if(x==y){ if(sum[a]!=sum[b]) printf("In different gangs.\n"); else printf("In the same gang.\n"); } else printf("Not sure yet.\n"); }else{ Merge(a,b); }// for(int i=1;i<=n;++i) printf("%d ",fa[i]); puts("");// for(int i=1;i<=n;++i) printf("%d ",sum[i]); puts(""); } } return 0;}
0 0
- POJ 1703 Find them, Catch them(并查集)
- POJ 1703 Find them, Catch them //并查集
- poj 1703 Find them, Catch them 并查集
- poj 1703 Find them, Catch them(并查集)
- poj 1703 Find them, Catch them //并查集
- POJ 1703 Find them, Catch them 并查集
- poj 1703 Find them, Catch them (并查集)
- poj 1703 Find them, Catch them(种类并查集)
- poj 1703 Find them, Catch them(种类并查集)
- poj 1703 -- Find them, Catch them(并查集)
- poj 1703 Find them, Catch them(并查集应用)
- POJ 1703 Find them, Catch them (并查集)
- poj 1703 - Find them, Catch them(并查集)
- poj 1703 Find them, Catch them(关系并查集)
- POJ 1703 Find them, Catch them 种类并查集
- POJ 1703 Find them, Catch them (关系并查集)
- POJ-1703 Find them, Catch them 经典并查集
- poj 1703 Find them,Catch them 并查集
- 发送一个消息到邮箱中,OSMboxPost()
- C++模板实现二叉树(五 树的平衡之dsw算法)
- 全栈必备 JavaScript基础
- eclipse 使用recycleView基础配置和使用
- Leetcode-199. Binary Tree Right Side View
- POJ 1703 Find them, Catch them【并查集】
- 203. Remove Linked List Elements 没做出来
- CCF-CSP-2017-3-1 分蛋糕
- phpExcel导入不同excel版本遇到的问题
- 【bzoj1030】[JSOI2007]文本生成器
- 复数的运算
- 集合框架
- SDN控制器Floodlight源码学习(二)--控制器(1)
- 道教的认识