-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmoocastg.java
81 lines (64 loc) · 1.9 KB
/
moocastg.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
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.StringTokenizer;
class edgess implements Comparable<edgess>{
int i;
int j;
int w;
edgess(int x, int y, int z){
i = x;
j = y;
w = z;
}
@Override
public int compareTo(edgess edgess) {
return w-edgess.w;
}
}
public class moocastg {
public static void main(String[] args) throws IOException {
// write your code here
BufferedReader f = new BufferedReader(new FileReader("moocast.in"));
// input file name goes above
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("moocast.out")));
int n = Integer.parseInt(f.readLine());
int[] x = new int[n];
int[] y = new int[n];
parent = new int[n];
for(int i = 0; i<n; i++){
StringTokenizer st = new StringTokenizer(f.readLine());
x[i] = Integer.parseInt(st.nextToken());
y[i] = Integer.parseInt(st.nextToken());
}
ArrayList<edgess> edge = new ArrayList<edgess>();
for(int i = 0; i<n; i++){
parent[i] = i;
for(int j=0;j<i;j++){
edge.add(new edgess(i,j,((x[i]-x[j])*(x[i]-x[j]))+((y[i]-y[j])*(y[i]-y[j]))));
}
}
Collections.sort(edge);
int cur = 0;
int secs = n;
for(edgess curt : edge){
if(fetch(curt.i)!=fetch(curt.j)){
merge(curt.i,curt.j);
cur = curt.w;
if(--secs==1){
break;
}
}
}
out.println(cur);
out.close();
}
static int[] parent;
static int fetch (int m){
return parent[m]==m ? m :(parent[m]=fetch(parent[m]));
}
static void merge (int i, int j){
parent[fetch(i)] = fetch(j);
}
}