(SPOJ) Número de Erdos - Solution

Link to the problem: http://br.spoj.com/problems/NUMERDOS/

The solution below used Breadth-First Search to identify the number of Erdos. First, for every author, I tried to find its "distance" to P. Erdos. However, this solution got Time Limit Exceeded. Then, I decided to start from P. Erdos and calculate its distance to every other author.


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

class Main {
    public HashMap<String, ArrayList<String>> adjList;
    public List<Info> answer;
    public HashSet<String> visited;
   
    public void bfs(String start) {
        Queue<Erdos> queue = new ArrayDeque<Erdos>();
        queue.add(new Erdos(start, 0));
       
        while (queue.size() > 0) {
            Erdos curr = queue.poll();
            String currAuthor = curr.author;
            int currNumErdos = curr.numErdos;
           
            if (visited.contains(currAuthor)) {
                continue;
            }
            visited.add(currAuthor);
           
            if (!currAuthor.equals(start)) {
                String[] n = currAuthor.split(" ");
                answer.add(new Info(n[0], n[1], currNumErdos));
            }
                       
            ArrayList<String> reachAuthors = adjList.get(currAuthor);
            if (reachAuthors == null) {
                continue;
            }
            for (int i = 0; i < reachAuthors.size(); i++) {
                queue.add(new Erdos(reachAuthors.get(i), currNumErdos+1));
            }
        }
       
        return;
    }
   
    public void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int numCase = 0;
        String line = br.readLine();
        int numArticles = Integer.parseInt(line);

        while (numArticles != 0) {
            adjList = new HashMap<>();
            answer = new ArrayList<>();
            HashSet<String> names = new HashSet<>();
           
            for (int i = 0; i < numArticles; i++) {
                line = br.readLine();
                String[] s = line.split(", ");
                s[s.length-1] = s[s.length-1].substring(0, s[s.length-1].length()-1); // ignores the '.' in the end of the line

                for (int j = 0; j < s.length; j++) {
                    if (names.add(s[j])) { // if it is the first appearance
                        adjList.put(s[j], new ArrayList<String>());
                    }
                }
               
                for (int j = 0; j < s.length; j++) {
                    for (int k = j+1; k < s.length; k++) {
                        adjList.get(s[j]).add(s[k]);
                        adjList.get(s[k]).add(s[j]);
                    }
                }
            }
           
            visited = new HashSet<>();
            bfs("P. Erdos");
            for (String ss : names) {
                if (visited.contains(ss)) { // it is related to Erdos
                    continue;
                }
                // for those that are not related to Erdos, numErdos = -1
                String[] n = ss.split(" ");
                answer.add(new Info(n[0], n[1], -1));
            }
                   
            Collections.sort(answer);
           
            bw.write("Teste " + ++numCase + "\n");
            for (int i = 0; i < answer.size(); i++) {
                Info info = answer.get(i);
                if (info.numErdos == -1) {
                    bw.write(info.firstName + " " + info.surname + ": infinito\n");
                }
                else {
                    bw.write(info.firstName + " " + info.surname + ": " + info.numErdos + "\n");
                }
            }
            bw.write("\n");
           
            line = br.readLine();
            numArticles = Integer.parseInt(line);
        }
       
        bw.flush();
        br.close();
        bw.close();
       
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
       
        System.exit(0);
    }
}

class Erdos {
    String author;
    int numErdos;
   
    public Erdos(String a, int nE) {
        author = a;
        numErdos = nE;
    }
}

class Info implements Comparable<Info> {
    String firstName;
    String surname;
    int numErdos;
   
    public Info(String fN, String s, int nE) {
        firstName = fN;
        surname = s;
        numErdos = nE;
    }
   
    public int compareTo(Info i) {
        if (this.surname.compareTo(i.surname) == 0) {
            return this.firstName.compareTo(i.firstName);
        }
        return this.surname.compareTo(i.surname);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução