-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD.java
More file actions
123 lines (96 loc) · 3.44 KB
/
Copy pathD.java
File metadata and controls
123 lines (96 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.io.*;
import java.util.*;
public class D {
static boolean[] visited;
static int[] parent;
static int[] distance;
public static void main(String[] args) throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
PrintWriter out= new PrintWriter(System.out);
StringTokenizer st1= new StringTokenizer(br.readLine(), " ");
int nodes= Integer.parseInt(st1.nextToken());
int edges= Integer.parseInt(st1.nextToken());
int s= Integer.parseInt(st1.nextToken());
int d= Integer.parseInt(st1.nextToken());
int k= Integer.parseInt(st1.nextToken()); //Mandatory node
//Make arrayList before making the graph
ArrayList<Integer>[] graph = new ArrayList[nodes+1];
for(int i=1; i<= nodes; i++){
graph[i]= new ArrayList<>();
}
//making the graph
for(int i=0; i<edges; i++){
StringTokenizer st2= new StringTokenizer(br.readLine(), " ");
int u= Integer.parseInt(st2.nextToken());
int v= Integer.parseInt(st2.nextToken());
graph[u].add(v); //Directed
}
//-----BFS 1: s to k--------
ArrayList<Integer> path1= bfs(graph, s, k, nodes);
if(path1.size()==1 && path1.get(0)== -1){ //path1.size()==1 means one node only
//path1.get(0)== -1 means unreaceable
out.println(-1);
out.flush();
return;
}
int distance1= distance[k];
//-----BFS 2: k to d--------
ArrayList<Integer> path2= bfs(graph, k, d, nodes);
if(path2.size()==1 && path2.get(0)== -1){
out.println(-1);
out.flush();
return;
}
int distance2= distance[d];
//Output 1: Distance
out.println(distance1 + distance2);
//Output 2: Path
ArrayList<Integer> finalPath= new ArrayList<>();
for(int i=0; i<path1.size(); i++){
finalPath.add(path1.get(i));
}
for(int i=1; i<path2.size(); i++){
finalPath.add(path2.get(i)); //skip extra k
}
for(int i=0; i<finalPath.size(); i++){
out.print(finalPath.get(i));
if(i+1<finalPath.size()) out.print(" ");
}
out.flush();
}
public static ArrayList<Integer> bfs(ArrayList<Integer>[] graph, int s, int d, int nodes){
visited= new boolean[nodes+1];
parent= new int[nodes+1];
distance= new int[nodes+1];
Queue<Integer> q=new LinkedList<>();
q.add(s);
visited[s]= true;
distance[s]=0;
parent[s]=0;
while(!q.isEmpty()){
int curr= q.poll();
for(int neighbor: graph[curr]){
if(!visited[neighbor]){
q.add(neighbor);
visited[neighbor]= true;
distance[neighbor]=distance[curr]+1;
parent[neighbor]= curr;
}
}
}
//Backtrack
ArrayList<Integer> path= new ArrayList<>();
if(!visited[d]){
path.add(-1);
return path;
}
int curr= d;
while(curr!=0){ //stops when parent becomes 0
path.add(curr);
curr= parent[curr];
}
//reverse the path
Collections.reverse(path);
return path;
}
}