手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893

URL: http://poj.org/problem?id=1703

题目大意:本题即很经典的“龙帮虎帮”问题。

有n个元素(n<=1e5),分布在两个不同的集合里。

现在有M个语句(m<=1e5),每个语句共两种:(1) 给定某两个元素在不同的集合中。(2) 询问两个元素是否在同一集合中。对于是、否、无法确定的情况,分别输出"In the same gang.", "In different gangs.", "Not sure yet."

思路分析:假如给定的是两个元素在同一集合中,思路就很明确了。

但是现在给定的是两个元素不同集合,而且已知仅有两个集合,于是我们就可以想办法将其转化为两元素同集合问题。

设有元素A,B, 则我们将每个元素分别“克隆”为A', B', 但将她们放入分别与本身不同的另外一个集合中。

同时,保证A和A', B和B', ..., 永远不会同集合。

然后,假如已知A与B不同集合,则需将A和B', B和A'分别放入同一集合(union操作),假如已知A与B同集合,则需把A和B, A'和B'进行union一下即可。

最后,回答询问时,有以下三种情况:

(1) A与B同集合,或A'与B‘同集合:In the same gang.

(2) A与B'同集合,或B与A'同集合:In different gangs.

(3) 其他:Not sure yet.

代码呈现:(Time:532MS ,Memory:960K ,Code:1005B)

#include<cstdio>
using namespace std; const int MAXN = 1e5;
int fa[MAXN*2+2];
int n,m; int find_fa(int u)
{
int i,j,k; i = u;
while(fa[i] != i)
{
i = fa[i];
}
fa[u] = i;
j = u;
while(j != i)
{
k = fa[j];
fa[j] = i;
j = k;
}
return i;
} int main()
{
int i,t,p,q,pp,qq,x,y;
char ch[3]; scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=1; i<=2*n; i++) //Union-Find Sets init
{
fa[i] = i;
}
for(i=1; i<=m; i++) //calculate while reading
{
scanf("%s%d%d",ch,&x,&y);
if(ch[0] == 'A')
{
p = find_fa(x);
q = find_fa(y);
pp = find_fa(x+n);
qq = find_fa(y+n);
if(p == q) printf("In the same gang.\n");
else if(pp == q || p == qq) printf("In different gangs.\n");
else printf("Not sure yet.\n");
}
else if(ch[0] == 'D')
{
p = find_fa(x);
q = find_fa(y);
pp = find_fa(x+n);
qq = find_fa(y+n);
if(pp != q) fa[pp] = q;
if(qq != p) fa[qq] = p;
}
}
} return 0;
}

类似题目:poj 2492 A Bug's Life (几乎一模一样)

poj 1182 && luogu P2024 [NOI 2001]食物链 (一样的思路,元素分成三类)

luogu P1525 [NOIP 2010提高组] 关押罪犯 (个人认为难度较大)

POJ 1703 Find them, Catch them(并查集高级应用)的更多相关文章

  1. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  2. poj.1703.Find them, Catch them(并查集)

    Find them, Catch them Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  3. POJ 1703 Find them, catch them (并查集)

    题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2  D 3 4 D 5 6...这就至少有3个集合了.并且 ...

  4. POJ 1703 Find them, Catch them 并查集的应用

    题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...

  5. POJ 1703 Find them, Catch them 并查集,还是有点不理解

    题目不难理解,A判断2人是否属于同一帮派,D确认两人属于不同帮派.于是需要一个数组r[]来判断父亲节点和子节点的关系.具体思路可参考http://blog.csdn.net/freezhanacmor ...

  6. [并查集] POJ 1703 Find them, Catch them

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43132   Accepted: ...

  7. POJ 1703 Find them, Catch them(种类并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 41463   Accepted: ...

  8. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

  9. POJ 1703 Find them, Catch them (数据结构-并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31102   Accepted: ...

随机推荐

  1. FZU 1894 志愿者选拔【单调队列】【monotone decreasing queue】

     Problem 1894 志愿者选拔 Accept: 1770    Submit: 5523 Time Limit: 1500 mSec    Memory Limit : 32768 KB  P ...

  2. ANDROID窗体管理服务实现机制和架构分析

     一.功能 窗体管理是ANDROID框架一个重要部分,主要包含例如以下功能: )Z-ordered的维护 )窗体的创建.销毁 )窗体的绘制.布局 )Token管理,AppToken )活动窗体管理 ...

  3. iOS 推送证书的制作

    关于iOS推送证书的P12文件,并非直接从KeyChain导出来的证书文件.而是须要经过openSSL工具制作的.(好在Mac OS 默认就有openSSL命令) 针对不同的Server平台,须要的证 ...

  4. Android 线程 Looper.prepare()、Looper.loop() 使用

    优化项目过程中发现了一个非常Low的问题,整理一下.备忘: 说问题之前先看下HandlerThread的定义 一个封装了looper的线程:   Looper用于封装了android线程中的消息循环. ...

  5. hdu 1035(DFS)

    Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  6. B3403 [Usaco2009 Open]Cow Line 直线上的牛 deque

    deque真的秀,queue和stack...没啥用了啊.操作差不多,就是在前面加一个front||back_就行了. 题干: 题目描述 题目描述     约翰的N只奶牛(编为1到N号)正在直线上排队 ...

  7. HP Unix vsftp服务配置

    HP Unix vsftp 服务配置: /opt/ssh/utils/ssh_chroot_setup.sh 运行脚本,会提示输入要建立的vsftp账号和要限制的家目录, 比如要限制的家目录为/Jia ...

  8. thinkphp 上传多张图片

    tp3.23 没有找到同时上传多张图片 手册有讲过:http://www.kancloud.cn/manual/thinkphp/1876 其实可以通过,多张图片多次上传来到达效果 hmlt: < ...

  9. C - Stones on the Table

    Problem description There are n stones on the table in a row, each of them can be red, green or blue ...

  10. Eclipse 每次ctrl-c ctrl-v 就变慢?

    继续闲着,所以继续写 大小: 60.7 KB 查看图片附件