poj1703 Find them, Catch them 基础并查集问题
来源:互联网 发布:virtuixomni淘宝 编辑:程序博客网 时间:2024/06/05 11:13
题意:有龙和蛇两类动物,给出若干信息,信息包括若干个两个编号的动物不是一类的,当输入‘A’时,要输出该两个编号的关系信息。
建立2*N并查集,对于‘D’的时候,要把两个编号a和(b+N)连接,b和(a+N)连接,对于每次访问‘A’,当两个编号a,b满足a与b连接的时候为一个类型,a与(b+N)连接为不同类型,都不连接是无法确定。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cstdlib>#include<cmath>#include<set>#include<bitset>#include<map>#include<stack>#include<queue>#include<vector>#include<utility>#define INF 1000000007#define inf 100000000000000000using namespace std;typedef long long ll;int far[200005],ran[200005],n,m,i,j,xx,yy,t;char c;void init(int n){ for(int i=1;i<=n;i++) { far[i]=i;ran[i]=0; }}int findx(int k){ if(far[k]==k)return k; return far[k]=findx(far[k]);}void unite(int x,int y){ x=findx(x);y=findx(y); if(x==y)return ; if(ran[x]<ran[y])far[x]=y; else { far[y]=x; if(ran[x]==ran[y])ran[x]++; }}bool same(int x,int y){ return findx(x)==findx(y);}int main(){ scanf("%d",&t); for(j=0;j<t;j++) { scanf("%d%d",&n,&m); init(2*n); for(i=0;i<m;i++) { scanf("\n%c%d%d",&c,&xx,&yy); if(c=='D') { unite(xx-1,yy-1+n); unite(xx-1+n,yy-1); } else { if(same(xx-1,yy-1))printf("In the same gang.\n"); else if(same(xx-1,yy-1+n))printf("In different gangs.\n"); else printf("Not sure yet.\n"); } } } return 0;}
0 0
- poj1703 Find them, Catch them 基础并查集问题
- poj1703 Find them, Catch them [并查集]
- POJ1703--Find them, Catch them--并查集
- poj1703 Find them, Catch them 并查集
- poj1703 Find them, Catch them 种类并查集
- poj1703 Find them,Catch them 【并查集】
- POJ1703 Find them, Catch them 并查集
- poj1703--Find them, Catch them(并查集应用)
- POJ1703 Find them, Catch them 【并查集】
- [并查集] poj1703 Find them,Catch them
- POJ1703 Find them, Catch them(并查集)
- poj1703 Find them, Catch them(关系并查集)
- poj1703 Find them, Catch them(并查集)
- 【Poj1703】Find them, Catch them(关系并查集)
- POJ1703-Find them, Catch them 并查集构造
- POJ1703 Find Them,Catch Them 种类并查集
- POJ1703 Find them, Catch them (分类并查集)
- Find them, Catch them 并查集
- Saprk数据倾斜七解决方案(一)
- SQL— 创建计算字段(以 PostgreSQL为例)
- Android使用ShareSDK集成QQ、微信、微博等第三方分享
- RecyclerView的行间距&&RecyclerView的Item间距&&
- 盒子模型关于box-sizing属性
- poj1703 Find them, Catch them 基础并查集问题
- 远程通讯____webservice_myeclise开发jax-rs架构的webservice
- 根据先序和中序遍历结果画出二叉树
- 在阿里云上部署生产级别Kubernetes集群
- 获取Servlet初始化参数
- Java入门笔记二——类和对象
- JavaScript中label语句的使用
- learning of zabbix 3.2.3 之nginx
- 我的程序员之路(英语的学习)