-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW07_4111056036_5.java
199 lines (171 loc) · 4.94 KB
/
HW07_4111056036_5.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
public class HW07_4111056036_5 extends LSD {
private int maxDist;
private boolean[] marked;
private int[] distTo;
private LinkedList[] adjacencyList;
private HashTable mapping;
private class HashTable {
public int counter;
private Node[] nodes;
public HashTable() {
this.nodes = new Node[8192];
this.counter = 0;
}
public int hash(int key) {
int hashKey = key & 8191;
while (nodes[hashKey] != null && nodes[hashKey].id != key) {
hashKey = (hashKey + 1) & 8191;
}
return hashKey;
}
public int get(int id) {
int key = hash(id);
Node head = nodes[key];
if(head == null) {
nodes[key] = new Node(id, this.counter);
adjacencyList[this.counter] = new LinkedList();
++this.counter;
return this.counter - 1;
}
return head.value;
}
private class Node {
int id, value;
public Node(int id, int value) {
this.id = id;
this.value = value;
}
}
}
private class Node {
int value;
Node next;
public Node(int value) {
this.value = value;
this.next = null;
}
}
private class LinkedList {
Node head;
Node last;
public LinkedList() {
head = new Node(0);
last = head;
}
public void add(int value) {
last.next = new Node(value);
last = last.next;
}
}
private class Queue {
private int[] queue;
private int head;
private int tail;
private int size;
public Queue() {
queue = new int[2];
head = 0;
tail = 0;
size = 0;
}
public void enqueue(int item) {
if (size == queue.length) {
resize(2 * queue.length);
}
queue[tail++] = item;
if (tail == queue.length) {
tail = 0;
}
size++;
}
public Integer dequeue() {
if (isEmpty()) {
return null;
}
int item = queue[head];
queue[head] = 0;
head++;
if (head == queue.length) {
head = 0;
}
size--;
if (size > 0 && size == queue.length / 4) {
resize(queue.length / 2);
}
return item;
}
public boolean isEmpty() {
return size == 0;
}
private void resize(int capacity) {
int[] copy = new int[capacity];
if (head < tail) {
System.arraycopy(queue, head, copy, 0, size);
} else {
System.arraycopy(queue, head, copy, 0, queue.length - head);
System.arraycopy(queue, 0, copy, queue.length - head, tail);
}
queue = copy;
head = 0;
tail = size;
}
}
private class Graph {
public Graph() {
mapping = new HashTable();
adjacencyList = new LinkedList[8192];
}
public void addEdge(int from, int to) {
int fromId = mapping.get(from);
int toId = mapping.get(to);
adjacencyList[fromId].add(toId);
adjacencyList[toId].add(fromId);
}
}
private int bfs(Graph graph, int s) {
Queue queue = new Queue();
queue.enqueue(s);
marked[s] = true;
distTo[s] = 0;
int v;
int lastNode = s;
int distToV;
Node head;
while(!queue.isEmpty()) {
v = queue.dequeue();
lastNode = v;
distToV = distTo[v] + 1;
head = adjacencyList[v].head.next;
while(head != null) {
if(marked[head.value] == false) {
queue.enqueue(head.value);
marked[head.value] = true;
distTo[head.value] = distToV;
if(distTo[head.value] > maxDist) {
maxDist = distTo[head.value];
}
}
head = head.next;
}
}
return lastNode;
}
@Override
public int distance(int[][] array) {
maxDist = 0;
Graph graph = new Graph();
int n = array.length;
for(int i = 0; i < n; ++i) {
graph.addEdge(array[i][0], array[i][1]);
}
n = mapping.counter;
marked = new boolean[n];
distTo = new int[n];
for(int i = 0; i < n; ++i) {
bfs(graph, i);
marked = new boolean[n];
distTo = new int[n];
}
return maxDist;
}
}