fork download
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class Main
  5. {
  6. static Pair[] array;
  7. static Vector<Triple> vect;
  8. static UF uf;
  9.  
  10. public static void main(String[] args) throws IOException
  11. {
  12.  
  13.  
  14.  
  15. String line = br.readLine();
  16.  
  17. while(line != null)
  18. {
  19. int n = Integer.parseInt(line);
  20.  
  21. array = new Pair[n];
  22.  
  23. vect = new Vector<Triple>();
  24.  
  25. uf = new UF(n);
  26.  
  27. double min = 0;
  28.  
  29. for(int i = 0; i < n; i++)
  30. {
  31. line = br.readLine();
  32.  
  33. StringTokenizer sTok = new StringTokenizer(line);
  34.  
  35. int x = Integer.parseInt(sTok.nextToken());
  36.  
  37. int y = Integer.parseInt(sTok.nextToken());
  38.  
  39. array[i] = new Pair(x,y);
  40. }
  41.  
  42. for(int i = 0; i < n; i++)
  43. {
  44. for(int j = i+1; j < n; j++)
  45. {
  46. double d = Math.sqrt(Math.pow(array[i].x - array[j].x,2) + Math.pow(array[i].y - array[j].y,2));
  47.  
  48. vect.add(new Triple(d,i,j));
  49. }
  50. }
  51.  
  52. Collections.sort(vect);
  53.  
  54. line = br.readLine();
  55.  
  56. int m = Integer.parseInt(line);
  57.  
  58. for(int i = 0; i < m; i++)
  59. {
  60. line = br.readLine();
  61.  
  62. StringTokenizer sTok = new StringTokenizer(line);
  63.  
  64. int a = Integer.parseInt(sTok.nextToken());
  65.  
  66. int b = Integer.parseInt(sTok.nextToken());
  67.  
  68. uf.union(a-1, b-1);
  69. }
  70.  
  71. for(int i = 0; i < vect.size(); i++)
  72. {
  73. if(uf.size() == 1)
  74. break;
  75.  
  76. Triple t = vect.get(i);
  77.  
  78. double w = t.w;
  79.  
  80. int v1 = t.v1;
  81.  
  82. int v2 = t.v2;
  83.  
  84. if(!uf.sameSet(v1, v2))
  85. {
  86. min += w;
  87. uf.union(v1, v2);
  88. }
  89. }
  90.  
  91. str.append(String.format("%.2f\n", min));
  92.  
  93. line = br.readLine();
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100. out.write(str.toString());
  101.  
  102. out.flush();
  103. }
  104. }
  105.  
  106. class UF
  107. {
  108. int[] array;
  109. int[] sizes;
  110. int count;
  111.  
  112. UF(int n)
  113. {
  114. array = new int[n];
  115. sizes = new int[n];
  116. count = n;
  117.  
  118. for(int i = 0; i < n; i++)
  119. {
  120. array[i] = i;
  121. sizes[i] = 1;
  122. }
  123. }
  124.  
  125. int size()
  126. {
  127. return count;
  128. }
  129.  
  130. int findSet(int i)
  131. {
  132. if(array[i] == i)
  133. return i;
  134. else
  135. return findSet(array[i]);
  136. }
  137.  
  138. boolean sameSet(int i, int j)
  139. {
  140. return findSet(i) == findSet(j);
  141. }
  142.  
  143. void union(int i, int j)
  144. {
  145. int a = findSet(i);
  146. int b = findSet(j);
  147.  
  148. if(a != b)
  149. {
  150. if(sizes[a] > sizes[b])
  151. {
  152. array[b] = a;
  153. sizes[a] += sizes[b];
  154. }
  155. else
  156. {
  157. array[a] = b;
  158. sizes[b] += sizes[a];
  159. }
  160.  
  161. count--;
  162. }
  163. }
  164. }
  165.  
  166. class Pair
  167. {
  168. int x;
  169. int y;
  170.  
  171. Pair(int a, int b)
  172. {
  173. x = a;
  174. y = b;
  175. }
  176. }
  177.  
  178. class Triple implements Comparable<Triple>
  179. {
  180. double w;
  181. int v1;
  182. int v2;
  183.  
  184. Triple(double a, int b, int c)
  185. {
  186. w = a;
  187. v1 = b;
  188. v2 = c;
  189. }
  190.  
  191. @Override
  192. public int compareTo(Triple that) {
  193. if(this.w > that.w)
  194. return 1;
  195. else if(this.w < that.w)
  196. return -1;
  197. else
  198. return 0;
  199. }
  200. }
Runtime error #stdin #stdout 0.08s 380224KB
stdin
6
0 0
200 50
100 200
50 300
300 200
300 300
2
3 4
5 6

6
0 0
200 50
100 200
50 300
300 200
300 300
5
1 2
2 3
3 4
4 5
5 6
stdout
Standard output is empty