(SPOJ) Rede ótica - Solution 1

In order to solve this problem, I used Kruskal's algorithm.

After reading the edges, I sort them accordingly with the cost of each edge. Then, I group them with the Union-Find method.



import java.io.*;
import java.util.*;

class Main  {
    public static ArrayList<EdgeWithCost> edges;
    public static int[] nodeParent;
    public static int[] depth;
    public static int count;
   
    public static int root(int n) {
        int currNode = n;
        while (nodeParent[currNode] != currNode) {
            currNode = nodeParent[currNode];
        }
       
        return currNode;
    }
   
    public static boolean union(int n1, int n2) {
        int rootN1 = root(n1);
        int rootN2 = root(n2);
       
        if (rootN1 != rootN2) {
            if (depth[rootN1] >= depth[rootN2]) {
                nodeParent[rootN2] = nodeParent[rootN1];
                if (depth[rootN1] == depth[rootN2]) {
                    depth[rootN1] += 1;
                }
            }
            else {
                nodeParent[rootN1] = nodeParent[rootN2];
            }
            count++;
           
            return true;
        }
       
        return false;
    }
   
    public static int reader(BufferedReader br) throws NumberFormatException, IOException {     
        int n;
        int resp = 0;     
      
        while (true) {         
            n = br.read();         
            if (n >= '0' && n <= '9') {
                break;
            }
        }
           
        while (true) {         
            resp = resp*10 + n-'0';         
            n = br.read();         
            if (n < '0' || n > '9') {
                break;     
            }
        }
      
        return resp;     
    }
       
    public static void process() throws NumberFormatException, IOException {   
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        int countTest = 0;
       
        int numPoints = reader(br);
        int numEdges = reader(br);
        while (numPoints != 0 || numEdges != 0) {
            edges = new ArrayList<EdgeWithCost>();
            for (int i = 0; i < numEdges; i++) {
                int p1 = reader(br);
                int p2 = reader(br);
                int cost = reader(br);
                EdgeWithCost e = new EdgeWithCost(p1, p2, cost);
                edges.add(e);
            }
       
            Collections.sort(edges);
           
            nodeParent = new int[numPoints+1];
            depth = new int[numPoints+1];
            for (int i = 1; i <= numPoints; i++) {
                nodeParent[i] = i;
                depth[i] = 0; 
            }
           
            System.out.println("Teste " + ++countTest);
            count = 0;
            for (int i = 0; i < numEdges && count < numPoints; i++) {
                int n1 = edges.get(i).source;
                int n2 = edges.get(i).dest;
                if (union(n1, n2)) {
                    if (n1 > n2) {
                        System.out.println(n2 + " " + n1);
                    }
                    else {
                        System.out.println(n1 + " " + n2);
                    }
                }
            }       
            System.out.println();
                       
            numPoints = reader(br);
            numEdges = reader(br);
        }
                         
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();

        System.exit(0);
    }
}

class EdgeWithCost implements Comparable<EdgeWithCost> {
    int source;
    int dest;
    int cost;
   
    public EdgeWithCost(int s, int d, int c) {
        source = s;
        dest = d;
        cost = c;
    }
   
    public int compareTo(EdgeWithCost e) {
        return this.cost - e.cost;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução