Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class Solution {
- private static int dist(Vertex u, Vertex v){
- int x = u.x - v.x;
- int y = u.y - v.y;
- return x * x + y * y;
- }
- public static void main(String[] args) throws IOException {
- PrintWriter writer = new PrintWriter(new BufferedOutputStream(System.out));
- BufferedReader in = new BufferedReader(new FileReader("input.txt"));
- String[] arr = in.readLine().split(" ");
- int n = Integer.parseInt(arr[0]);
- List<Vertex> g = new ArrayList<Vertex>();
- for (int i = 0; i < n; i++) {
- arr = in. readLine().split(" ");
- int x = Integer.parseInt(arr[0]);
- int y = Integer.parseInt(arr[1]);
- g.add(new Vertex(x, y));
- }
- arr = in.readLine().split(" ");
- int m = Integer.parseInt(arr[0]);
- boolean[][] edges = new boolean[n][n];
- for (int i = 0; i < m; i++) {
- arr = in.readLine().split(" ");
- int u = Integer.parseInt(arr[0]) - 1;
- int v = Integer.parseInt(arr[1]) - 1;
- edges[u][v] = true;
- edges[v][u] = true;
- }
- int[] d = new int[n];
- int[] p = new int[n];
- boolean[] used = new boolean[n];
- Arrays.fill(d, Integer.MAX_VALUE);
- d[0] = 0;
- for (int k = 0; k < n; k++){
- int i = -1;
- for (int j = 0; j < n; j++) {
- if (!used[j] && (i == -1 || d[j] < d[i])) {
- i = j;
- }
- }
- used[i] = true;
- for (int j = 0; j < n; j++){
- int w = 0;
- if (!edges[i][j]) {
- w = dist(g.get(i), g.get(j));
- }
- if (!used[j] && d[j] > w){
- d[j] = w;
- p[j] = i;
- }
- }
- }
- for (int i = 1; i < n; i++) {
- if (!edges[p[i]][i]) {
- writer.println((p[i] + 1) + " " + (i + 1));
- }
- }
- writer.close();
- }
- private static final class Vertex {
- private int x;
- private int y;
- public Vertex(int x, int y) {
- this.x = x;
- this.y = y;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement