首页 > 专题系列 > Java解POJ > POJ 1703 Find them, Catch them [解题报告] Java
2013
11-10

POJ 1703 Find them, Catch them [解题报告] Java

Find them, Catch them

问题描述 :

The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)

Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:

1. D [a] [b]

where [a] and [b] are the numbers of two criminals, and they belong to different gangs.

2. A [a] [b]

where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.

输入:

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message as described above.

输出:

For each message “A [a] [b]” in each case, your program should give the judgment based on the information got before. The answers might be one of “In the same gang.”, “In different gangs.” and “Not sure yet.”

样例输入:

1
5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4

样例输出:

Not sure yet.
In different gangs.
In the same gang.

解题代码:

//* @author: [email protected]
import java.io.*;
public class Main
{
 static int[] p,q;
 static int u;
 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());
	while((a--)!=0)
	{
	 String[] ss=in.readLine().split(" ");
	 int n=Integer.parseInt(ss[0]);
	 int m=Integer.parseInt(ss[1]);
	 p=new int[n+1];
	 q=new int[n+1];
	 for(int i=0;i<=n;i++)
		p[i]=i;
	 for(int i=0;i< m;i++)
	 {
		ss=in.readLine().split(" ");
		int x=Integer.parseInt(ss[1]);
		int y=Integer.parseInt(ss[2]);
		if(ss[0].equals("D"))
			union(x,y);
		else{
		   if(root(x)!=root(y))
			System.out.println("Not sure yet.");
		   else
		   {
			if(get(x)==get(y))
			  System.out.println("In the same gang.");
			else
			  System.out.println("In different gangs.");
		    }
		}
	  }
	}
 }
	static int root(int a)
	{
		while(p[a]!=a)
			a=p[a];
		return a;
	}
  static void union(int x,int y)
  {
	int s1=get(x);
	int x1=u;
	int s2=get(y);
	int y1=u;
	if(x1==y1)return;
	if(x1< y1)
	{
		p[y1]=x1;
		if(s1==s2)
			q[y1]=q[x1]+1;
	}
	else
	{
		p[x1]=y1;
		if(s1==s2)
			q[x1]=q[y1]+1;
	}
    }
	
  static int get(int a)
 {
	int t=0;
	while(p[a]!=a)
	{
		t+=q[a];
		a=p[a];
	}
	u=a;
	return t%2;
  }
}

  1. 第23行:
    hash = -1是否应该改成hash[s ] = -1

    因为是要把从字符串s的start位到当前位在hash中重置

    修改提交后能accept,但是不修改居然也能accept

  2. 因为是要把从字符串s的start位到当前位在hash中重置,修改提交后能accept,但是不修改居然也能accept