Help with solution


#1

My solution seems to give correct answers when I’m using test with custom input, but while testing with same input, it gives different output.

public class Solution {
    static boolean found=false;
    public int solve(int A, ArrayList<ArrayList<Integer>> B) {
        ArrayList<ArrayList<Integer>> t = new ArrayList<>();
        boolean[] visited=new boolean[A+1];
        for(int i=0;i<=A;i++){
            t.add(new ArrayList<>());
            visited[i]=false;
        }
        for(int e=0;e<B.size();e++){
            int u = B.get(e).get(0);
            int v = B.get(e).get(1);
            t.get(u).add(v);
            //System.out.println(t);
        }
        //System.out.println(t);
        visited[1]=true;
        dfs(1,A,t,visited);
        if(found)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    
    public void dfs(int start,int A, ArrayList<ArrayList<Integer>> t,boolean[] visited)
    {
        for(int ch:t.get(start)){
            //visited[ch]=true;
            if(ch==A){
                found=true;
                return;
            }
            else{
                if(visited[ch]==false)
                {
                    visited[ch]=true;
                    dfs(ch,A,t,visited);
                }
            }
        }
    }
}