Language:
Description
Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs. Problem Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it. Input
The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.
Output
The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.
Sample Input 2 3 3 1 2 2 3 1 3 4 2 1 2 3 4 Sample Output Scenario #1: Suspicious bugs found! Scenario #2: No suspicious bugs found! Hint
Huge input,scanf is recommended.
Source
TUD Programming Contest 2005, Darmstadt, Germany
|
题意:某物种n只,m个描述,表示x,y为异性,判断是否有同性交配。如果发现存在同性交配输出bugs found..
思路:开一个rank数组表示其与父亲节点的关系。rank[x]=0表示x与fa[x]为同性,rank[x]=1,表示x与fa[x]为异性。rank在findset中更新时,很明显可以发现,rank[x]=(rank[x]+rank[fa[x]])%2,不过注意此时fa[x]是未更新前的fa[x]。 在Union操作中,我们需要更新fx,fy,我们发现rank[fy]=(rank[x]+rank[y]+1)%2,rank[fx]=(rank[x]+rank[y]+1)%2,则利用上面的公式更新即可。关于判断,当x,y属于同一个根的时候,如果rank[x]=rank[y],即fx=fy时,我们由rank[x]=rank[y]可推出x和y同性,那么就说明存在bug。详见代码: