(UVA) Ubiquitous Religions - Solução 2

You can check another solution for this problem here

For this solution, I changed the structure used in the count() method. In the previous solution, I used an ArrayList of Integer. Now, I use a simple array. This solution is better because we know that the array will have at most "numPeople"+1 and an ArrayList resizes itself periodically.


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

class Main  {
    public static Element[] vector;
  
    public static int count() {
        int[] v = new int[vector.length];
        for (int i = 1; i < vector.length; i++) {
            v[root(vector[i].parent)] += 1;
        }
       
        int components = 0;
        for (int i = 1; i < v.length; i++) {
            if (v[i] > 0) {
                components++;
            }
        }
      
        return components;
    }
  
    public static int root(int n) {
        int currNode = n;
        while (vector[currNode].parent != currNode) {
            currNode = vector[currNode].parent;
        }
      
        return currNode;
    }
  
    public static void union(int p1, int p2) {
        int rootP1 = root(p1);
        int rootP2 = root(p2);
       
        if (rootP1 != rootP2) { // they are not connected
            if (vector[rootP1].depth >= vector[rootP2].depth) {
                vector[rootP2].parent = vector[rootP1].parent;
                if (vector[rootP1].depth == vector[rootP2].depth) {
                    vector[rootP1].depth++;
                }
            }
            else {
                vector[rootP1].parent = vector[rootP2].parent;
            }
        }
    }
  
    public static void readEntries(BufferedReader br, int numPairs) throws NumberFormatException, IOException {
        for (int i = 0; i < numPairs; i++) {
            int p1 = reader(br);
            int p2 = reader(br);
            union(p1, p2);
        }
    }
  
    public static void initVector(int numPeople) {
        vector = new Element[numPeople+1];
        for (int i = 1; i <= numPeople; i++) {
            Element e = new Element(i, 0);
            vector[i] = e;
        }
    }
  
    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 numPeople = reader(br);
        int numPairs = reader(br);
        int numCase = 1;

        while (numPeople != 0 || numPairs != 0) {
            vector = new Element[numPeople+1];
            initVector(numPeople);
          
            readEntries(br, numPairs);
              
            System.out.println("Case " + numCase++ + ": " + count());
      
            numPeople = reader(br);
            numPairs = reader(br);
        }
              
        return;
    }
  
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();

        System.exit(0);
    }
}

class Element {
    int parent;
    int depth;
  
    public Element(int parent, int depth) {
        this.parent = parent;
        this.depth = depth;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução