首页 > 专题系列 > Java解POJ > POJ 2492 A Bug’s Life [解题报告] Java
2013
11-11

POJ 2492 A Bug’s Life [解题报告] Java

A Bug’s Life

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

2
3 3
1 2
2 3
1 3
4 2
1 2
3 4

样例输出:

Scenario #1:
Suspicious bugs found!

Scenario #2:
No suspicious bugs found!

温馨提示:

Huge input,scanf is recommended.

解题代码:

//* @author: [email protected]
import java.io.*;
public class Main
{
 static my[] p;
 public static void main(String[] args) throws IOException
 {
  InputStreamReader is=new InputStreamReader(System.in);
  BufferedReader in=new BufferedReader(is);
  int a=Integer.parseInt(in.readLine());
  int cnt=0;
  while((a--)!=0)
  {
   cnt++;
   String[] ss=in.readLine().split(" ");
   int n=Integer.parseInt(ss[0]);
   p=new my[n+1];
   for(int i=0;i<=n;i++)
     p[i]=new my(i);
   int t=Integer.parseInt(ss[1]);
   boolean bb=true;
   while((t--)!=0)
   {
	ss=in.readLine().split(" ");
	if(bb)
	{
          int x1=Integer.parseInt(ss[0]);
	  int x2=Integer.parseInt(ss[1]);
	  int r1=root(x1),r2=root(x2);
	  if(r1< r2){
	    p[r2].root=r1;
	    p[x2].d=(p[x1].d+1)%2;
	  }
	  else if(r1>r2){
	    p[r1].root=r2;
	    p[x1].d=(p[x2].d+1)%2;
	  }
	  else if(p[x2].d==p[x1].d)bb=false;
	}
     }
     System.out.println("Scenario #"+cnt+":");
     if(bb)System.out.println("No suspicious bugs found!");
     else System.out.println("Suspicious bugs found!");
     System.out.println();
  }
 }

 static int root(int a)
  {
	int u=a;
	while(p[a].root!=a)a=p[a].root;
	p[u].root=a;
	return a;
  }
	
}

 class my
 {
	int root;
	int d=0;
	public my(int r)
	{
		root=r;
	}
 }

  1. 很高兴你会喜欢这个网站。目前还没有一个开发团队,网站是我一个人在维护,都是用的开源系统,也没有太多需要开发的部分,主要是内容整理。非常感谢你的关注。

  2. 如果两个序列的最后字符不匹配(即X [M-1]!= Y [N-1])
    L(X [0 .. M-1],Y [0 .. N-1])= MAX(L(X [0 .. M-2],Y [0 .. N-1]),L(X [0 .. M-1],Y [0 .. N-1])
    这里写错了吧。