-
Notifications
You must be signed in to change notification settings - Fork 0
/
IsenbaevsNumber.java
93 lines (88 loc) · 2.66 KB
/
IsenbaevsNumber.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
89
90
91
92
93
/** http://acm.timus.ru/problem.aspx?space=1&num=1837 #bfs */
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;
public class IsenbaevsNumber {
public static int getIdx(
HashMap<String, Integer> hashMap,
ArrayList<String> arr,
ArrayList<ArrayList<Integer>> graph,
String name) {
if (!hashMap.containsKey(name)) {
arr.add(name);
int idx = arr.size() - 1;
graph.add(new ArrayList<>());
hashMap.put(name, idx);
return idx;
} else {
return hashMap.get(name);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String name1, name2, name3;
int idx1, idx2, idx3;
HashMap<String, Integer> hashMap = new HashMap<>();
ArrayList<String> arr = new ArrayList<>();
ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
for (int i = 0; i < n; i++) {
name1 = sc.next();
idx1 = getIdx(hashMap, arr, graph, name1);
name2 = sc.next();
idx2 = getIdx(hashMap, arr, graph, name2);
name3 = sc.next();
idx3 = getIdx(hashMap, arr, graph, name3);
graph.get(idx1).add(idx2);
graph.get(idx1).add(idx3);
graph.get(idx2).add(idx1);
graph.get(idx2).add(idx3);
graph.get(idx3).add(idx1);
graph.get(idx3).add(idx2);
}
ArrayList<Integer> distArr = new ArrayList<>();
ArrayList<Boolean> visitedArr = new ArrayList<>();
for (int i = 0; i < arr.size(); i++) {
distArr.add(Integer.MAX_VALUE);
visitedArr.add(false);
}
if (hashMap.containsKey("Isenbaev")) {
int startIdx = hashMap.get("Isenbaev");
BFS(graph, distArr, visitedArr, startIdx);
}
// result
Collections.sort(arr);
int idx;
for (String str : arr) {
idx = hashMap.get(str);
if (distArr.get(idx) == Integer.MAX_VALUE) {
System.out.println(str + " undefined");
} else {
System.out.println(str + " " + distArr.get(idx));
}
}
}
public static void BFS(
ArrayList<ArrayList<Integer>> graph,
ArrayList<Integer> dist,
ArrayList<Boolean> visitedArr,
int startNode) {
visitedArr.set(startNode, true);
dist.set(startNode, 0);
Deque<Integer> queue = new LinkedList<>();
queue.add(startNode);
while (!queue.isEmpty()) {
int node = queue.pollFirst();
for (int otherNode : graph.get(node)) {
if (!visitedArr.get(otherNode)) {
visitedArr.set(otherNode, true);
dist.set(otherNode, dist.get(node) + 1);
queue.add(otherNode);
}
}
}
}
}