Poj2492 and query set

Source: Internet
Author: User

Check the set and train of thought: classify the bug I interact (I think there will be a misunderstanding of "sexual intercourse") as one type to see if there are similar interact behaviors (sexual behavior ), if Suspicious bugs found is output!

 

[Html]
# Include <iostream>
Using namespace std;
 
Class Node
{
Public:
Node ()
{
NodeID = 0;
Parent = NULL;
Rank = 0;
}
Node (int id)
{
NodeID = id;
Parent = NULL;
Rank = 0;
}
Int nodeID;
Node * parent;
Int rank;
};
 
Void MakeSet (Node * node)
{
Node-> parent = node;
Node-> rank = 0;
}
 
Void LinkSets (Node * node1, Node * node2)
{
If (node1-> rank> node2-> rank)
{
Node2-> parent = node1;
}
Else if (node1-> rank <node2-> rank)
{
Node1-> parent = node2;
}
Else
{
Node1-> parent = node2;
Node2-> rank ++;
}
Return;
}
 
Node * FindSets (Node * node)
{
If (node-> parent! = Node)
{
Node-> parent = FindSets (node-> parent );
}
Return node-> parent;
}
 
 
Void UnionSets (Node * node1, Node * node2)
{
LinkSets (FindSets (node1), FindSets (node2 ));
}
 
Int main ()
{
Node * pNode [2002];
Node * pLinkNode [2002];
Int iScenario, iCount;
Cin> iScenario;
ICount = 1;
While (iCount <= iScenario)
{
Int iNumber, iInteraction;
Scanf ("% d", & iNumber, & iInteraction );
For (int I = 0; I <iNumber; I ++)
{
PNode [I] = new Node (I + 1 );
MakeSet (pNode [I]);
PLinkNode [I] = NULL;
}
Bool flag = true;
Int iBug1, iBug2;
For (int I = 0; I <iInteraction; I ++)
{
Scanf ("% d", & iBug1, & iBug2 );
If (FindSets (pNode [iBug1-1])! = FindSets (pNode [iBug2-1])
{
If (Fig [iBug1-1] = NULL)
{
PLinkNode [iBug1-1] = pNode [iBug2-1];
}
Else
{
UnionSets (pNode [iBug2-1], pLinkNode [iBug1-1]);
}
 
If (Fig [iBug2-1] = NULL)
{
PLinkNode [iBug2-1] = pNode [iBug1-1];
}
Else
{
UnionSets (pLinkNode [iBug2-1], pNode [iBug1-1]);
}
}
Else
{
Flag = false;
}
}
Printf ("Scenario # % d: \ n", iCount );
If (flag = false)
{
Printf ("Suspicious bugs found! \ N ");
}
Else
{
Printf ("No suspicious bugs found! \ N ");
}
 
 
ICount ++;
}
}
Author: qiul12345

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.