A Bug's Life

A Bug's Life
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 6905 Accepted: 2064

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

Source Code

Problem: 2492 User: FLY2008
Memory: 5428K Time: 10657MS
Language: Java Result: Accepted
  • Source Code
    import java.util.Scanner;
    
    public class Main{
    
     static int n,m,t,flag;
     static int[] farther=new int[2001];
     static int[] rank=new int[2001];
    
     public static void main(String [] args){
      
      try{
       
       Scanner cin=new Scanner(System.in);
       int nums=cin.nextInt();
       for(int Case=1;Case<=nums;Case++){
        
        n=cin.nextInt();
        m=cin.nextInt();
       
        initSet();
    
        for(int k=1;k<=m;k++){
    
         int a=cin.nextInt();
         int b=cin.nextInt();
         //不存性别冲突,实行"并操作"
         if(flag==0) unio(a,b);  
         
        }
      
        System.out.println("Scenario #"+Case+":");
    
        if(flag==1){
         
         System.out.println("Suspicious bugs found!");
         flag=0;
        }
        else{
         
         System.out.println("No suspicious bugs found!"); 
        }
        if(Case<nums) System.out.println();
        
       }
       
      }catch(Exception e){}
     }
    
     public static void initSet(){ //初始化
    
      for(int i=1;i<=n;i++){
        
       farther[i]=0;
       rank[i]=0;
      }  
     }
     
     public static int find(int p){ //寻找父亲节点
    
      t=0;
      while(farther[p]!=0){
       
       t=t+rank[p];
       p=farther[p];
      }
      return p;
     }
    
     public static void unio(int a,int b){ //并操作
      
      int t1,t2;
      a=find(a);
      t1=t;
      b=find(b);
      t2=t;
    
      if(a!=b){
    
       if(t1<t2){
    
        farther[a]=b;
        rank[a]=(t1-t2+1)%2;
       }
       else{
    
        farther[b]=a;
        rank[b]=(t1-t2+1)%2;
       }
      }
      else{
        
       if((t2-t1)%2==0) flag=1;  //有性别冲突flag=1;
      }
    
     } 
      
    }

你可能感兴趣的:(并查集)