手动博客搬家:本文发表于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. selenium获取弹窗提示

    1.点击保存给的提示是几秒钟,遮罩显示 2. 其他弹窗处理方法 http://blog.csdn.net/Real_Tino/article/details/59068827

  2. C++ Development Library

    C/C++ 开发库 | C/C++ Development Library 这里收集一些著名的 C/C++ 开发库.SDK.类库.可复用类与结构代码 等信息,列举它们的介绍.参考和网站链接,为各位 C ...

  3. media type

    https://www.sitepoint.com/mime-types-complete-list/ application/base64 https://github.com/dotnet/doc ...

  4. php 关于使用七牛云存储

    1.首先注册七牛云存储账号 http://www.qiniu.com/ 2.获得密钥 3.仔细查看文档 http://developer.qiniu.com/docs/v6/sdk/php-sdk.h ...

  5. windows php文件下载地址

    http://windows.php.net/downloads/releases/archives/

  6. [转]利用 NPOI 變更字體尺寸及樣式

    本文转自:http://blog.cscworm.net/?p=1650 利用 NPOI 變更字體尺寸及樣式: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...

  7. 【CodeForces688A】Opponents

    [思路分析] 比较水的模拟题 具体见代码吧 #include<iostream> #include<cstdio> #include<algorithm> usin ...

  8. 利用windbg获取dump的dll文件

    根据堆栈对应的地址查找其对应的Module ID,然后将对应的Module保存. !IP2MD 命令从托管函数中获取 MethodDesc 结构地址. !dumpmodule 1caa50 下面的命令 ...

  9. js-常见简单的js判断方法(暂不参考正则)

    1: 2: 3: 4: 5: 6: 7:

  10. mysqlslap对mysql进行压力测试

    mysqlslap是从5.1.4版开始的一个MySQL官方提供的压力测试工具.通过模拟多个并发客户端访问MySQL来执行压力测试,并且能很好的对比多个存储引擎在相同环境下的并发压力性能差别. mysq ...