题目大意是给出m个虫子的交配组合,确定其中是否存在同性交配现象。
并查集的常规应用是判断两个元素是否处于同一个集合,但这题如果按照这种常规思路构建模型显然是行不通的。于是在常规的模型基础上增加一个数组sex[a]表示节点a与其根节点(注意和父节点相区分)的性别情况,true表示相同,false表示不同。
如果输入的a、b为同一集合中的元素,则直接比较其与根节点的性别情况即可判断是否为同性。如果处于不同集合,则合并两个集合后再进行比较。但合并之后要重新确定被合并的集合的原根节点与现根节点的性别情况。假设a的根节点为c,b的根节点为d,将d合并到c中,那么d相对于c的性别情况有以下可能:
- sex[a] == true,sex[b] == true,即a、c的性别相同,b、d的性别相同,可知d,c的性别相反,即sex[d] = false;
- sex[a] == true,sex[b] == false,即a、c的性别相同,b、d的性别不同,可知d,c的性别相同,即sex[d] = true;
- sex[a] == false,sex[b] == true,和第二种情况相同,sex[d] = true;
- sex[a] == false,sex[b] == false,即a、c的性别相反,c、d的性别相反,可知d,c的性别相反,即sex[d] = false;
即sex[d] = sex[a] ^ sex[b];
对sex的更新可以在求根节点的同时进行,根据与父节点的性别关系进行处理即可,利用递归实现非常便捷。此外在合并完成之后需要对被合并的集合内的节点的性别关系进行更新,之后再进行同性判断。
7781952 IwfWcf 2492 Accepted 404K 860MS G++ 1315B 2010-10-23 23:55:30
I don't know if it'ѕ just me ог if perhаps everуbody else encountering
回复删除problеms with your ωebsite. It аρpears aѕ if sοme of the text οn your content аrе
гunning off thе sсrеen.
Сan somebody elsе please provіde feeԁback and lеt me know if this is happening tο them as
ωell? This mаy be a isѕuе with my internet
browser becauѕе I've had this happen previously. Thank you
Here is my web page: parcel auditing company
toms outlet, http://www.tomsoutlet-stores.com/
回复删除nike roshe, http://www.nikerosherunshoes.co.uk/
prada handbags, http://www.pradahandbagsoutlet.co.uk/
soccer jerseys, http://www.cheapsoccerjersey.net/
michael kors outlet store, http://www.michaelkorsoutlet-store.us.com/
true religion jeans, http://www.truereligionjeansoutlets.us.com/
oakley sunglasses, http://www.oakleysunglasses-outlet.us.com/
nike free 5, http://www.nikefree5.us/
louis vuitton bags, http://www.louisvuittonbag.us.com/
chanel handbags, http://www.chanelhandbags-outlet.co.uk/
chanel handbags, http://www.chanelhandbags-outlet.us.com/
louis vuitton handbags, http://www.louisvuittonhandbag.us/
louis vuitton outlet, http://www.louisvuittonoutlet.in.net/
ghd hair straighteners, http://www.ghdhairstraightenerssale.co.uk/
nba jerseys, http://www.nbajerseys.us.com/
coach outlet, http://www.coachoutletstores.com.co/
true religion jeans, http://www.truereligionjeanscanada.com/
swarovski crystal, http://www.swarovskicrystals.co.uk/
mbt shoes, http://www.mbtshoesoutlet.us.com/
michael kors handbags, http://www.michaelkorshandbags.in.net/
chanel handbags, http://www.chanelhandbagsoutlet.in.net/
michael kors outlet online, http://www.michaelkorsoutletonline.in.net/
polo ralph lauren, http://www.poloralphlauren.us.org/
the north face clearance, http://www.thenorthfaceclearances.us.com/
jordan shoes, http://www.jordan-shoes.us.com/
beats by dr dre, http://www.beatsbydrdre-headphones.us.com/
the north face uk, http://www.thenorthfaces.org.uk/
1003maoqiuyun
louis vuitton borse
回复删除coach factory outlet
canada goose jackets
the north face outlet
adidas superstar trainers
cheap ugg boots
ugg outlet online
oakley sunglasses outlet
true religion outlet
timberland shoes
201612.26chenjinyan
cheap jordans
回复删除golden goose sneakers
yeezy
lebron 15 shoes
air max 270
jordan shoes
jordan 11
yeezy shoes
nike sneakers
michael jordan shoes