博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[并查集] POJ 1703 Find them, Catch them
阅读量:6905 次
发布时间:2019-06-27

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

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

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.

Source

原题大意:对于T组数据,每组数据有两个整数n,m代表有n个点,m次询问。
              接下来m行,每一行第一个字符为A,则回答1与2是否为一个集合,回答是,不是,或是不知道。
              第一个字符为D,则表示1与2不是一个集合。
解题思路:此题唯一与裸并查集不同的就是D表示的是不同。
              明显的是1+0为1,(1+1)%2==0,明显的是组成了一个循环结构,可以用向量的方式来解。
#include
int father[150500],relation[150050];void init(int n) { int i; for(i=1;i<=n;++i) father[i]=i; for(i=1;i<=n;++i) relation[i]=0; }int find(int x) { int t; if(x==father[x]) return (x); t=father[x]; father[x]=find(t); relation[x]=(relation[x]+relation[t])%2; return (father[x]); }void merge(int x,int y) { int find_x=find(x); int find_y=find(y); if(find_x!=find_y) { relation[find_x]=(relation[y]+1-relation[x]+2)%2; father[find_x]=find_y; } }int main() { int T,n,m,num=0;char c;int x,y; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); init(n); while(m--) { getchar(); scanf("%c%d%d",&c,&x,&y); if(c=='D') { merge(x,y); }else if(c=='A') { if(find(x)!=find(y)) { printf("Not sure yet.\n"); continue; }else if(relation[x]==relation[y]) { printf("In the same gang.\n"); continue; }else if(relation[x]!=relation[y]) printf("In different gangs.\n"); } } } return 0; }

  

           

转载于:https://www.cnblogs.com/fuermowei-sw/p/6193533.html

你可能感兴趣的文章
怎么把顶部的Django administration去掉!!
查看>>
知晓设计模式,框架,去提高开发效率,使代码简洁
查看>>
07-OpenLDAP密码审计
查看>>
使用Flex 和 Red5开发简单视频直播功能
查看>>
233
查看>>
第二十一章 任务、线程和同步
查看>>
HtmlDecode 解码 &nbsp;
查看>>
文件共享windows server 2008 服务器
查看>>
软考:两个通用思想
查看>>
初入koa2 -起步
查看>>
java 开发体系参考学习
查看>>
【转】如何阅读android源码
查看>>
Azure系列2.1.4 —— BlobInputStream
查看>>
关于面向对象的理解和类、对象,Java的三大特性
查看>>
1004 成绩排名
查看>>
【转载】【springmvc+mybatis项目实战】杰信商贸-1.项目背景
查看>>
(转)GMap.Net开发之自定义Marker使用方法
查看>>
P1501 [国家集训队]Tree II
查看>>
用ReactNative搭建一个安卓APP
查看>>
rocketmq生产者代码分析
查看>>