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
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;
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++){
}
}
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){
}
}
}

``````}
``````

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{
OutputWriter out = new OutputWriter(System.out);
while(t–>0){

``````       bit=new BitSet[n];

for(int i=0;i<n;i++){

bit[i]=new BitSet(252);
}
for(int i=0;i<n;i++){

bit[i].set(x);
}
st=new ArrayList<ArrayList<Integer>>();
for(int i=0;i<n;i++){
}

for(int j=0;j<n-1;j++){
}

part=new ArrayList<ArrayList<Integer>>();
for(int i=0;i<n;i++){
}
height=new int[n];
//	v=new boolean[n];
preprocess(n,root);
while(m-->0){
int lc=lca(u,v,n);
System.out.println(bit[lc].cardinality());
}
}
``````

}
{
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;
}

{
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars)
{
curChar = 0;
try
{
} catch (IOException e)
{
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}

{
while (isSpaceChar(c))
int sgn = 1;
if (c == '-')
{
sgn = -1;
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
} while (!isSpaceChar(c));
return res * sgn;
}

{
while (isSpaceChar(c))
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
} while (!isSpaceChar(c));
return res.toString();
}

{
while (isSpaceChar(c))
int sgn = 1;
if (c == '-')
{
sgn = -1;
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
}
if (c == '.')
{
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
}
}
return res * sgn;
}

{
while (isSpaceChar(c))
int sgn = 1;
if (c == '-')
{
sgn = -1;
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
} 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()
{
}

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();
}
}
``````

}