2013
11-10

# 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]/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */
import java.io.*;
public class Main
{
static int[] p,q;
static int u;
public static void main(String[] args) throws IOException
{
while((a--)!=0)
{
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++)
{
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