POJ1703Find them, Catch them 【种类并查集】

题目链接:http://poj.org/problem?id=1703

题目大意:给n个人,m次询问。A代表询问a, b之间的关系,D代表给出a, b属于不同的帮派。

我的想法:

太菜了,上课的时候没想到这种方法。

思路:

1.感觉有点像2-sat的思想,对于D给出的a, b两人属于不同帮派。我们就将a于b的对立点用并查集维护在一个集合中,也将b于a的对立点连通。

2.查询时,若a,b两人pre值相等则属于同一帮派。若a与b的对立点属于同一集合,或b与a的对立点属于同一集合,则说明a与b属于不同帮派。若上述都不成立则说明关系还未知。

3.对立点是虚构的,不能影响到原来的点。所以数据范围翻倍,x + n代表x的对立点。

注意:不能用 cin 输入,会超时。只能用scanf

 1 #include<stdio.h>
 2 const int maxn = 2e5 + 1e4;
 3 
 4 int pre[maxn];
 5 
 6 int find(int x)
 7 {
 8     if(pre[x] == x)
 9         return x;
10     else
11     {
12         int root = find(pre[x]);
13         pre[x] = root;
14         return pre[x];
15     }
16 }
17 
18 int main()
19 {
20     int T;
21     scanf("%d", &T);
22     while(T --)
23     {
24         int n, m;
25         scanf("%d%d", &n, &m);
26         getchar();
27         for(int i = 1; i <= 2 * n; i ++)
28         {
29             pre[i] = i;
30         }
31         for(int i = 1; i <= m; i ++)
32         {
33             char ch;
34             int a, b;
35             scanf("%c%d%d", &ch, &a, &b);
36             if(ch == 'D')
37             {
38                 int x = find(a), y = find(b);
39                 int xx = find(a + n), yy = find(b + n);
40                 if(x != yy)
41                     pre[yy] = x;
42                 if(y != xx)
43                     pre[xx] = y;
44             }
45             if(ch == 'A')
46             {
47                 int x = find(a), y = find(b);
48                 int xx = find(a + n), yy = find(b + n);
49                 if(x == y)
50                     printf("In the same gang.\n");
51                 else if(x == yy || y == xx)
52                     printf("In different gangs.\n");
53                 else
54                     printf("Not sure yet.\n");
55             }
56             getchar();
57         }
58     }
59     return 0;
60 }
POJ1703

 

转载于:https://www.cnblogs.com/yuanweidao/p/10931777.html

!qhyb.rar 当 CAD 缺失对应字体时,图纸文字会显示异常,出现乱码、问号。将下载好的字体文件复制到 AutoCAD 的 Fonts 文件夹中,即可恢复正常显示。 立即下载

相关推荐

政府科技管理部门如何实现科技成果高效转化?.docx

政府科技管理部门如何实现科技成果高效转化?

poj1703 犯罪团伙 并查集

      在讲解这个题目之前, 我不得不狠狠的吐槽cin和cout的效率, 我提交了6遍都是超时, 最后一遍提交时统统把cin和cout改为scanf和printf才过的, 当时心情又高兴又难受.    查看题目点击这里 Find them, Catch them POJ - 1703 吐槽完了, 开始讲题.        第一次遇见这种题目是感觉满头痛的, 咦~, 并查集不是将关系是朋...

looeyWei的博客 850

国有企业如何推动科技创新与产业升级融合?.docx

科易网基于40亿+科创知识图谱数据库,深度探索AI技术在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,研究科技创新领域的AI+数智化解决方案,推动科技创新与产业创新智能化发展。

poj1703 经典并查集

一道非常经典的并查集,建议还可以做下hdu5971,这样我感觉一般的并查集就并没有神马威胁了。。。祭奠我为此付出的好几天时间。。。懒得加注释,真的要想搞懂的话建议花时间自己看懂,毕竟ac代码已经写出来了。但是实在不会的话详询请加qq1933160466。2333333333333333333333333333333#include #include #include using namespace

fanbaobao829的博客 388

POJ1703 Find them, Catch them 并查集 好题 有坑点

Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first n...

weixin_30883271的博客 208

poj1703 Find them, Catch them并查集

每次给的关系表示两个物品不属于同一集合,题目关键在于一共只有两个集合,那就设一个对立关键字好了,表示两个物品对立,每次给一个对立关系u,v,就把(u和v的对立)与(v和u的对立)并起来,最后査就好了。 #include #include #include #include #include #include #include #include #include #include #de

emmmmmm 2209

POJ1703Find them, Catch them】(并查集模板题)

题目地址:POJ1703Find them, Catch them】 题目描述: Tadu市的警察局决定结束混乱,因此要采取行动,根除城市中的两大帮派:龙帮和蛇帮。然而,警方首先需要确定某个罪犯是属于哪个帮派。目前的问题是,给出两个罪犯,他们是属于同一个帮派吗?您要基于不完全的信息给出您的判断,因为歹徒总是在暗中行事。假设在Tadu市现在有N(N≤105)个罪犯,编号从1到N。当然,至少有一个罪犯属于龙帮,也至少有一个罪犯属于蛇帮。给出M(M≤105)条消息组成的序列,消息有下列两种形式: D [a]

Obliviate的博客 741

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

题目地址:POJ 1703 种类并查集水题。 代码如下: #include #include #include #include #include #include #include #include #include #include #include using namespace std; int bin[600000], rank[600000]; int f

scf0920 1273

POJ - 1703 Find them, Catch them种类并查集

Find them, Catch them Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d &amp;amp;amp; %I64u Submit Status Description The police office in Tadu City decides to say ends to the chaos, as laun...

ll1243295518的博客 391

POJ1703 Find Them,Catch Them 种类并查集

1.题目描述: Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43863   Accepted: 13513 Description The police office in Tadu City decid

终有绿洲摇曳在沙漠 278

poj 1703 Find them, Catch them种类并查集

看了半天才差不多有点看明白了关系的更新。 一下摘自博客:点击打开链接  一道标准的关系型并查集题。普通的并查集是给几个同类的元素,而关系型并查集是给不同类的元素,然后求各个元素之间的关系。        题目大意是:在一个城市里有两种不同的犯罪团伙。首先输入T表示有T组测试,然后输入N和M,表示有N个罪犯(编号从1到N)而且接下来有M个操作。操作分为两种:

CillyB的博客 665

POJ 1703 Find them, Catch them 种类并查集

题目链接 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in...

RPG_Zero的博客 290

POJ 1703 Find them, Catch them种类并查集

Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present quest

memcpy0的博客 230

poj 1703 Find them, Catch them 带权并查集OR种类并查集

The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, give

Lionel 806

poj1703 Find them, Catch them种类并查集

题目地址:http://poj.org/problem?id=1703 题目大意:警察抓了n个坏蛋,这些坏蛋分别属于龙帮或蛇帮。输入m个语句,A x y询问x和y的关系(在一个帮派,不在,不能确定),D x y表示x和y不在一个帮派。 思路:种类并查集。维护一个数组rel[x]表示x和根节点的关系(0表示在一个帮派,1表示不在),初始全为0(自己和自己在一个帮派),更新r...

diaoyinquan9001的博客 148

科技中介服务如何实现数字化升级?.docx

科易网基于40亿+科创知识图谱数据库,深度探索AI技术在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,研究科技创新领域的AI+数智化解决方案,推动科技创新与产业创新智能化发展。

上一篇: POJ1041 John's trip 【字典序输出欧拉回路】
下一篇: J-流浪西邮之寻找火石碎片 【经典背包变形】
a6334167
博客等级 码龄10年 1粉丝 0原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值