I am solving ADAORANG - Ada and Orange Tree question from spoj in java
every time i submit my solution ,getting TlE after 15 case ,can any one help here how to solve this quetions
or find mistake from code which i am making and explain
Thank you in Advance.
here is my code:
import java.util.;
import java.io.;
import java.lang.*;
class sparse{
static ArrayList<ArrayList>st;
static ArrayList<ArrayList> part;
static int height[];
static BitSet []bit;
static void dfs(int node ,int source,int h){
height[node]=h;
//part[node][0]=source;
part.get(node).add(0,source);
Iteratorit=st.get(node).iterator();
while(it.hasNext()){
int p=(int)it.next();
if(p!=source){
dfs(p,node,h+1);
bit[node].or(bit[p]);
}
}
}
static void preprocess(int n,int root){
for(int i=0;i<n;i++){
for(int j=0;(1<<j)<n;j++){
part.get(i).add(j,-1);
}
}
dfs(root,-1,0);
for(int j=1;(1<<j)<n;j++){
for(int i=0;i<n;i++){
if(part.get(i).get(j-1)!=-1){
part.get(i).add(j, part.get(part.get(i).get(j-1)).get(j-1));
}
}
}
}
static int lca(int p,int q,int n){
if(height[p]<height[q]){
p=p^q;
q=p^q;
p=p^q;
}
int lg=1;
for(lg=1;(1<<lg)<=height[p];lg++);
lg–;
for(int i=lg;i>=0;i–){
if(height[p]-(1<<i)>=height[q]){
p=part.get§.get(i);
}
}
if(p==q)
return p;
for(int i=lg;i>=0;i–){
if(part.get§.get(i)!=-1 && part.get§.get(i)!=part.get(q).get(i)){
p=part.get§.get(i);
q=part.get(q).get(i);
}
}
return part.get§.get(0);
}
public static void main(String[]args) throws java.lang.Exception{
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
int t=in.readInt();
while(t–>0){
int n=in.readInt();
int m=in.readInt();
int root=in.readInt();
bit=new BitSet[n];
for(int i=0;i<n;i++){
bit[i]=new BitSet(252);
}
for(int i=0;i<n;i++){
int x=in.readInt();
bit[i].set(x);
}
st=new ArrayList<ArrayList<Integer>>();
for(int i=0;i<n;i++){
st.add(i,new ArrayList<Integer>());
}
for(int j=0;j<n-1;j++){
int x=in.readInt();
int y=in.readInt();
st.get(x).add(y);
st.get(y).add(x);
}
part=new ArrayList<ArrayList<Integer>>();
for(int i=0;i<n;i++){
part.add(i,new ArrayList<Integer>());
}
height=new int[n];
// v=new boolean[n];
preprocess(n,root);
while(m-->0){
int u=in.readInt();
int v=in.readInt();
int lc=lca(u,v,n);
System.out.println(bit[lc].cardinality());
}
}
}
private static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = stream.read(buf);
} catch (IOException e)
{
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public double readDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public long readLong()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next()
{
return readString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
private static class OutputWriter
{
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream)
{
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer)
{
this.writer = new PrintWriter(writer);
}
public void print(Object... objects)
{
for (int i = 0; i < objects.length; i++)
{
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects)
{
print(objects);
writer.println();
}
public void close()
{
writer.close();
}
public void flush()
{
writer.flush();
}
}
}