Skip to content

Commit 77944dc

Browse files
authored
Create Code2.java
1 parent b80c939 commit 77944dc

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

Juspay Round-1/Code2.java

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

0 commit comments

Comments
 (0)