博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ:1703-Find them, Catch them(并查集好题)(种类并查集)
阅读量:4327 次
发布时间:2019-06-06

本文共 3810 字,大约阅读时间需要 12 分钟。

Find them, Catch them

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 49867 Accepted: 15320

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 question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)

Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:

  1. D [a] [b]

    where [a] and [b] are the numbers of two criminals, and they belong to different gangs.

  2. A [a] [b]

    where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.

Input

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message as described above.

Output

For each message “A [a] [b]” in each case, your program should give the judgment based on the information got before. The answers might be one of “In the same gang.”, “In different gangs.” and “Not sure yet.”

Sample Input

1

5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4

Sample Output

Not sure yet.

In different gangs.
In the same gang.


解题心得:

  1. 有两个帮派,龙帮和蛇帮,有n个罪犯,每个罪犯属于其中一个帮派,有m个操作,第一种就是规定罪犯a,b不属于一个帮派,第二种就是询问罪犯a,b的关系。
  2. 刚开始看这个题的时候真的很蒙,写得暴力会TLE,后来看大佬说有两种方法,
    • 第一种,我把它称为创造镜像(a的镜像是a+n),每个罪犯的镜像和自己属于敌对关系,这样就可以把并查集分成两个部分,两个敌对关系的集合,如果给出a和b是敌对关系,那么就可以合并a和b镜像,合并b和a的镜像。
    • 第二种是直接观察关系的转变,在find father和merge时候维护关系,首先使用cm[i]来表示第i个罪犯的帮派(值为0,1),先全部初始化为0,如果a和b是敌对关系,那么cm[b] = (cm[a]+1+cm[b])%2,在find father的时候需要从祖先节点开始从上往下维护,可以发现cm[son] = (cm[son] + cm[father])%2,这里可以仔细想想这个关系,要明白的是我们将所有的帮派初始化都为0。

第一种方法:

#include 
#include
using namespace std;const int maxn = 3e5+100;int father[maxn],n,m,t;int find(int x){ if(father[x] == x) return x; return father[x] = find(father[x]);}void merge(int x,int y) { int fx = find(x); int fy = find(y); father[fy] = fx;}bool _same(int x,int y) { return find(x) == find(y);}void unite(int x,int y) { int fx = find(x); int fy = find(y); father[fy] = fx;}void init() { scanf("%d%d",&n,&m); for(int i=1;i<=n*3;i++) father[i] = i;}void Solve() { for(int i=0;i

第二种方法:

#include 
#include
#include
using namespace std;const int maxn = 1e5+100;int father[maxn],n,m,cm[maxn];void init() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) father[i] = i; memset(cm,0,sizeof(cm));}int find(int x) { if(x == father[x]) return x; int temp = father[x]; father[x] = find(father[x]); cm[x] = (cm[temp]+cm[x])%2; return father[x];}void Unite(int a,int b) { int fa = find(a); int fb = find(b); if(fa == fb) return; father[fb] = fa; cm[fb] = (cm[a] + 1 - cm[b]) % 2;}int main() { int t; scanf("%d",&t); while(t--){ init(); while(m--) { char temp[5]; int a,b; scanf("%s%d%d",temp,&a,&b); if(temp[0] == 'A') { if (find(a) != find(b)) { printf("Not sure yet.\n"); } else if (cm[a] == cm[b]) { printf("In the same gang.\n"); } else printf("In different gangs.\n"); } else { Unite(a,b); } } } return 0;}

转载于:https://www.cnblogs.com/GoldenFingers/p/9107138.html

你可能感兴趣的文章
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-5.PageHelper分页插件使用
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-6.微信扫码登录回调本地域名映射工具Ngrock...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-8.用户模块开发之保存微信用户信息...
查看>>
Linux下Nginx安装
查看>>
LVM扩容之xfs文件系统
查看>>
Hbase记录-client访问zookeeper大量断开以及参数调优分析(转载)
查看>>
代码片段收集
查看>>
vue-cli3创建项目时报错
查看>>
输入1-53周,输出1-53周的开始时间和结束时间
查看>>
实验二
查看>>
shell——按指定列排序
查看>>
crash 收集
查看>>
507 LOJ 「LibreOJ NOI Round #1」接竹竿
查看>>
UI基础--烟花动画
查看>>
2018. 2.4 Java中集合嵌套集合的练习
查看>>
精通ASP.NET Web程序测试
查看>>
vue 根据不同属性 设置背景
查看>>
51Nod1601 完全图的最小生成树计数 Trie Prufer编码
查看>>
Codeforces 1110D. Jongmah 动态规划
查看>>
android驱动在win10系统上安装的心酸历程
查看>>