|
| 1 | + import java.util.Scanner; |
| 2 | +import java.util.ArrayList; |
| 3 | + |
| 4 | +/* IMPORTANT: Multiple classes and nested static classes are supported */ |
| 5 | + |
| 6 | +/* |
| 7 | + * uncomment this if you want to read input. |
| 8 | +//imports for BufferedReader |
| 9 | +import java.io.BufferedReader; |
| 10 | +import java.io.InputStreamReader; |
| 11 | + |
| 12 | +//import for Scanner and other utility classes |
| 13 | +import java.util.*; |
| 14 | +*/ |
| 15 | + |
| 16 | +// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail |
| 17 | + |
| 18 | +class TestClass { |
| 19 | + public static void main(String args[] ) throws Exception { |
| 20 | + /* Sample code to perform I/O: |
| 21 | + * Use either of these methods for input |
| 22 | + |
| 23 | + //BufferedReader |
| 24 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 25 | + String name = br.readLine(); // Reading input from STDIN |
| 26 | + System.out.println("Hi, " + name + "."); // Writing output to STDOUT |
| 27 | + |
| 28 | + //Scanner |
| 29 | + Scanner s = new Scanner(System.in); |
| 30 | + String name = s.nextLine(); // Reading input from STDIN |
| 31 | + System.out.println("Hi, " + name + "."); // Writing output to STDOUT |
| 32 | + |
| 33 | + */ |
| 34 | + |
| 35 | + // Write your code here |
| 36 | + int[] vis =new int[1000001]; |
| 37 | + Scanner sc =new Scanner(System.in); |
| 38 | + int n =sc.nextInt(); |
| 39 | + int[] node =new int[n]; |
| 40 | + int maxi=0; |
| 41 | + for(int i=0;i<n;i++){ |
| 42 | + node[i] =sc.nextInt(); |
| 43 | + maxi =Math.max(maxi,node[i]); |
| 44 | + } |
| 45 | + int edgeCnt =sc.nextInt(); |
| 46 | + ArrayList<ArrayList<Integer>> adj =new ArrayList<>(); |
| 47 | + for(int i=0;i<maxi+1;i++){ |
| 48 | + adj.add(new ArrayList<>()); |
| 49 | + } |
| 50 | + for(int i=0;i<edgeCnt;i++){ |
| 51 | + int u =sc.nextInt(); |
| 52 | + int v =sc.nextInt(); |
| 53 | + |
| 54 | + adj.get(u).add(v); |
| 55 | + adj.get(v).add(u); |
| 56 | + } |
| 57 | + int src =sc.nextInt(); |
| 58 | + int dest =sc.nextInt(); |
| 59 | + |
| 60 | + if(dfs(src, dest, vis, adj)==true){ |
| 61 | + System.out.print(1); |
| 62 | + return; |
| 63 | + } |
| 64 | + System.out.print(0); |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + } |
| 69 | + public static boolean dfs(int src,int dest,int[] vis,ArrayList<ArrayList<Integer>> adj){ |
| 70 | + vis[src]=1; |
| 71 | + if(src==dest){ |
| 72 | + return true; |
| 73 | + } |
| 74 | + for(int currNode : adj.get(src)){ |
| 75 | + if(vis[currNode]!=1){ |
| 76 | + if(dfs(currNode, dest, vis, adj)==true){ |
| 77 | + return true; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + return false; |
| 82 | + } |
| 83 | +} |
0 commit comments