-
Notifications
You must be signed in to change notification settings - Fork 80
/
Find_the_nearest_clone.java
88 lines (71 loc) · 2.77 KB
/
Find_the_nearest_clone.java
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
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
static int findShortest(int graphNodes, int[] graphFrom, int[] graphTo, int[] ids, int val) {
LinkedList<Integer>[] map = new LinkedList[graphNodes];
HashMap<Integer, Integer> distances = new HashMap();
for(int i = 0 ; i < graphNodes; i++){
map[i] = new LinkedList<Integer>();
}
for(int i = 0; i < graphFrom.length; i++){
map[graphFrom[i] - 1].add(graphTo[i]);
map[graphTo[i] - 1].add(graphFrom[i]);
}
Queue<Integer> queue = new LinkedList();
for(int i = 0; i < ids.length; i++){
if(ids[i] == val){
queue.add(i + 1);
distances.put(i + 1, 0);
}
}
if(queue.size() < 2){
return -1;
}
HashSet<Integer> seen = new HashSet();
while(queue.size() > 0){
int current = queue.poll();
seen.add(current);
for(int a : map[current - 1]){
if(distances.containsKey(a) && !seen.contains(a)){
return distances.get(a) + distances.get(current) + 1;
}
else {
queue.add(a);
distances.put(a, distances.get(current) + 1);
}
}
}
return -1;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] graphNodesEdges = br.readLine().split(" ");
int graphNodes = Integer.parseInt(graphNodesEdges[0].trim());
int graphEdges = Integer.parseInt(graphNodesEdges[1].trim());
int[] graphFrom = new int[graphEdges];
int[] graphTo = new int[graphEdges];
for (int i = 0; i < graphEdges; i++) {
String[] graphFromTo = br.readLine().split(" ");
graphFrom[i] = Integer.parseInt(graphFromTo[0].trim());
graphTo[i] = Integer.parseInt(graphFromTo[1].trim());
}
int[] ids = new int[graphNodes];
String[] idsItems = br.readLine().split(" ");
for (int i = 0; i < graphNodes; i++) {
int idsItem = Integer.parseInt(idsItems[i]);
ids[i] = idsItem;
}
int val = Integer.parseInt(br.readLine().split(" ")[0]);
br.close();
int ans = findShortest(graphNodes, graphFrom, graphTo, ids, val);
bufferedWriter.write(String.valueOf(ans));
bufferedWriter.newLine();
bufferedWriter.close();
}
}