博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ1703 && POJ2942 &&POJ 1182 并查集 这个做法挺巧妙
阅读量:6093 次
发布时间:2019-06-20

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

Find them, Catch them
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 37242   Accepted: 11483

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

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

Sample Output

Not sure yet.In different gangs.In the same gang.

题意是在Tadu这个城市有两个团伙。

给出D i j的意思是说i和j不是一个团伙的。

给出A i j的意思是根据已知的信息,能不能确定i和j是一个团伙的,如果确定是一个团伙的,输出“In different gangs.”。如果确定不是一个团伙的,输出“In the same gang.”。如果不能确定,输出“Not sure yet.”

之前使用并查集都是判断两个人是不是在一个集合里,这次要确定地判断两个人不在集合里,所以想法就是添加无用的N个元素作为桥梁。i k不在一个集合中,k j不在一个集合中,说明i j在一个集合中,那我把i k j都放入集合中,只不过k是我查询永远都不会用到的元素,所以就是添加元素时 i k+N,k+N j添加到集合中。所以当我查询k+N,j 发现他们在一个集合中时,就恰恰说明了k和j在两个团伙里面。

代码:

#include 
#include
#include
#include
#include
#include
#pragma warning(disable:4996) using namespace std;char oper[5];int n,m,num;int pre[200015];int findpre(int x) { while(x!=pre[x]) { x=pre[x]; } return x; } void union_set(int x,int y){ int pre_x=findpre(x); int pre_y=findpre(y); if(pre_x == pre_y) return; else if(pre_x>pre_y) { int temp = pre_x; pre_x = pre_y; pre_y = temp; } pre[pre_y]=pre_x;}bool same(int x,int y){ return findpre(x) == findpre(y); }int main(){ //freopen("i.txt","r",stdin); //freopen("o.txt","w",stdout); int test,i,temp1,temp2; scanf("%d",&test); while(test--) { scanf("%d%d",&n,&m); for(i=1;i<=2*n;i++) { pre[i]=i; } for(i=1;i<=m;i++) { scanf("%s%d%d",oper,&temp1,&temp2); if(oper[0]=='A') { if(same(temp1,temp2)) { printf("In the same gang.\n"); } else if(same(temp1+n,temp2)||same(temp1,temp2+n)) { printf("In different gangs.\n"); } else { printf("Not sure yet.\n"); } } else if(oper[0]=='D') { union_set(temp1,temp2+n); union_set(temp1+n,temp2); } } } //system("pause"); return 0;}
POJ2942与此类似,代码:

#include 
#include
#include
#include
#include
#include
#pragma warning(disable:4996) using namespace std;int n,m;int pre[200015];int findpre(int x) { while(x!=pre[x]) { x=pre[x]; } return x; } void union_set(int x,int y){ int pre_x=findpre(x); int pre_y=findpre(y); if(pre_x == pre_y) return; else if(pre_x>pre_y) { int temp = pre_x; pre_x = pre_y; pre_y = temp; } pre[pre_y]=pre_x;}bool same(int x,int y){ return findpre(x) == findpre(y); }int main(){ //freopen("i.txt","r",stdin); //freopen("o.txt","w",stdout); int test,i,j,temp1,temp2; scanf("%d",&test); for(i=1;i<=test;i++) { printf("Scenario #%d:\n",i); scanf("%d%d",&n,&m); for(j=1;j<=2*n;j++) { pre[j]=j; } bool flag=false; for(j=1;j<=m;j++) { scanf("%d%d",&temp1,&temp2); if(flag)continue; if(same(temp1,temp2)) { flag=true; continue; } union_set(temp1,temp2+n); union_set(temp1+n,temp2); } if(flag) { printf("Suspicious bugs found!\n\n"); } else { printf("No suspicious bugs found!\n\n"); } } //system("pause"); return 0;}

POJ1182是把集合弄成了3个,那与此同理,就是x,x+n,x+2*n的区别,用x,y+n表示A吃B的集合。再对每一个语句进行排除,得到答案。

代码:

#include 
#include
#include
#include
#include
#include
#pragma warning(disable:4996) using namespace std;int n,m,num; int pre[150015]; int findpre(int x) { while(x!=pre[x]) { x=pre[x]; } return x; } void union_set(int x,int y) { int pre_x=findpre(x); int pre_y=findpre(y); if(pre_x == pre_y) return; else if(pre_x>pre_y) { int temp = pre_x; pre_x = pre_y; pre_y = temp; } pre[pre_y]=pre_x; } bool same(int x,int y) { return findpre(x) == findpre(y); } int main(){ //freopen("i.txt","r",stdin); //freopen("o.txt","w",stdout); int oper,i,x,y,ans; scanf("%d%d",&n,&m); ans=0; for(i=1;i<=3*n;i++) { pre[i]=i; } for(i=1;i<=m;i++) { scanf("%d%d%d",&oper,&x,&y); if(x<=0||x>n||y<=0||y>n) { ans++; continue; } if(oper==1) { if(same(x,y+n)||same(x,y+2*n)||same(x+n,y)) { ans++; continue; } union_set(x,y); union_set(x+n,y+n); union_set(x+2*n,y+2*n); } else if(oper==2) { if(x==y||same(x,y)||same(x+n,y)||same(x,y+2*n)) { ans++; continue; } union_set(x,y+n); union_set(x+n,y+2*n); union_set(x+2*n,y); } } printf("%d\n",ans); //system("pause"); return 0;}

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/lightspeedsmallson/p/4899540.html

你可能感兴趣的文章
带空格文件名的处理(find xargs grep ..etc)
查看>>
centos使用docker下安装mysql并配置、nginx
查看>>
需要学的东西
查看>>
Linux 获取文件夹下的所有文件
查看>>
对 Sea.js 进行配置(一) seajs.config
查看>>
第六周
查看>>
解释一下 P/NP/NP-Complete/NP-Hard 等问题
查看>>
javafx for android or ios ?
查看>>
微软职位内部推荐-Senior Software Engineer II-Sharepoint
查看>>
sql 字符串操作
查看>>
【转】Android布局优化之ViewStub
查看>>
网络安全管理技术作业-SNMP实验报告
查看>>
根据Uri获取文件的绝对路径
查看>>
Flutter 插件开发:以微信SDK为例
查看>>
.NET[C#]中NullReferenceException(未将对象引用到实例)是什么问题?如何修复处理?...
查看>>
边缘控制平面Ambassador全解读
查看>>
Windows Phone 7 利用计时器DispatcherTimer创建时钟
查看>>
程序员最喜爱的12个Android应用开发框架二(转)
查看>>
vim学习与理解
查看>>
DIRECTSHOW在VS2005中PVOID64问题和配置问题
查看>>