您的位置:首页 > 其它

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

2014-08-15 17:08 330 查看
Findthem,Catchthem
TimeLimit:1000MSMemoryLimit:10000K
TotalSubmissions:31532Accepted:9713
DescriptionThepoliceofficeinTaduCitydecidestosayendstothechaos,aslaunchactionstorootuptheTWOgangsinthecity,GangDragonandGangSnake.However,thepolicefirstneedstoidentifywhichgangacriminalbelongsto.Thepresentquestionis,giventwocriminals;dotheybelongtoasameclan?Youmustgiveyourjudgmentbasedonincompleteinformation.(Sincethegangstersarealwaysactingsecretly.)AssumeN(N<=10^5)criminalsarecurrentlyinTaduCity,numberedfrom1toN.Andofcourse,atleastoneofthembelongstoGangDragon,andthesameforGangSnake.YouwillbegivenM(M<=10^5)messagesinsequence,whichareinthefollowingtwokinds:1.D[a][b]where[a]and[b]arethenumbersoftwocriminals,andtheybelongtodifferentgangs.2.A[a][b]where[a]and[b]arethenumbersoftwocriminals.Thisrequiresyoutodecidewhetheraandbbelongtoasamegang.InputThefirstlineoftheinputcontainsasingleintegerT(1<=T<=20),thenumberoftestcases.ThenTcasesfollow.EachtestcasebeginswithalinewithtwointegersNandM,followedbyMlineseachcontainingonemessageasdescribedabove.OutputForeachmessage"A[a][b]"ineachcase,yourprogramshouldgivethejudgmentbasedontheinformationgotbefore.Theanswersmightbeoneof"Inthesamegang.","Indifferentgangs."and"Notsureyet."SampleInput
1
55
A12
D12
A12
D24
A14
SampleOutput
Notsureyet.
Indifferentgangs.
Inthesamegang.
题意:警察要肃清两个帮派,该城市有龙帮和蛇帮两个帮派,给出两个罪犯,让我们判断是否属于一个帮派。
当前有N(1-N)个罪犯在这个城市,至少他们其中一个属于龙帮或者蛇帮,给出M行信息,信息分两种:
1.D[a][b]:a和b罪犯不属于同一个帮派。
2.A[a][b]:判断a和b是不是在一个帮派。是就输出Inthesamegang.不是就输出Indifferentgangs.不能判断就输出Notsureyet.
分析:种类并查集;
两个数组,一个为储存根节点的数组,另外一个为储存该点与根节点之间的关系(0,或者1)在寻找根节点的时候需要更新该点与根节点之间的关系;x与y合并的时候,若x1为y1的根节点,那么需要求ans[y1];
ans[y1]=(ans[x]-ans[y]+1)%2;如下图所示可得出

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: