Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8925355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:40:32+00:00 2026-06-15T07:40:32+00:00

I am solving the Erdos number problem from the programming challenges in JAVA. The

  • 0

I am solving the Erdos number problem from the programming challenges in JAVA.
The code runs perfectly in my machine. However on the online judge it results in a runtime error. Could anyone point out the mistake i made?

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=985

Here is the code

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

class Main
{
private String inputLines[];
private String namesToBeFound[];
private String namesInEachBook[][];
private String searchItem;
private boolean solnfound=false;
private static final BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
static String read() throws IOException
{
    String line;
    while(true)
    {
        line=br.readLine();
        if(line==null) break; //eof
        else if(line.length()==0) continue; //blank line
        else
        {
            line=line.trim().replaceAll("\\s+"," ");
            return line;
        }
    }
    return null;
}

public static void main(String args[]) throws IOException
{
    Main ob=new Main();
    int totalPapers,calcAuthors,totalScenarios;
    //First input number of scenarios
    totalScenarios=Integer.parseInt(read());
    //Now start a loop for reading total number of scenarios
    for(int scenario=1;scenario<=totalScenarios;scenario++)
    {
        //Now read the line containing the number of papers and authors
        StringTokenizer line=new StringTokenizer(read()," ");
        totalPapers=Integer.parseInt(line.nextToken());
        calcAuthors=Integer.parseInt(line.nextToken());

        //Read a line containing author names along with book names
        ob.inputLines=new String[totalPapers];
        for(int i=0;i<totalPapers;i++)
            ob.inputLines[i]=read();

        //Read a line containing the names to be searched
        ob.namesToBeFound=new String[calcAuthors];
        for(int i=0;i<calcAuthors;i++)
            ob.namesToBeFound[i]=read();

        //Now generate the array
        ob.buildArray();
        //Now search
        System.out.println("Scenario "+scenario);
        for(int i=0;i<calcAuthors;i++)
        {
            ob.searchItem=ob.namesToBeFound[i];
            if(ob.searchItem.equals("Erdos, P."))
            {
                System.out.println("Erdos, P. 0");
                continue;
            }
            ob.search(ob.namesToBeFound[i],1,new ArrayList());
            if(ob.solnfound==false) System.out.println(ob.searchItem+" infinity");
            ob.solnfound=false;
        }
    }

}

private void buildArray()
{
    String str;
    namesInEachBook=new String[inputLines.length][];
    for(int i=0;i<inputLines.length;i++)
    {

        str=inputLines[i];
        str=str.substring(0,str.indexOf(':'));
        str+=",";
        namesInEachBook[i]=new String[(countCommas(str)+1)>>1];
        for(int j=0;j<namesInEachBook[i].length;j++)
        {
            str=str.trim();
            namesInEachBook[i][j]="";
            namesInEachBook[i][j]+=str.substring(0,str.indexOf(','))+",";
            str=str.substring(str.indexOf(',')+1);
            namesInEachBook[i][j]+=str.substring(0,str.indexOf(','));
            str=str.substring(str.indexOf(',')+1);
        }
    }
}

private int countCommas(String s)
{
    int num=0;
    for(int i=0;i<s.length();i++)
        if(s.charAt(i)==',') num++;
    return num;
}

private void search(String searchElem,int ernosDepth,ArrayList searchedElem)
{
    ArrayList searchSpace=new ArrayList();
    searchedElem.add(searchElem);
    for(int i=0;i<namesInEachBook.length;i++)

        for(int j=0;j<namesInEachBook[i].length;j++)
        {
            if(namesInEachBook[i][j].equals(searchElem))    //Add all authors name in this group
            {
                for(int k=0;k<namesInEachBook[i].length;k++)
                {
                    if(namesInEachBook[i][k].equals("Erdos, P.")) //Found
                    {
                        solnfound=true;
                        System.out.println(searchItem+" "+ernosDepth);
                        return;
                    }
                    else if(searchedElem.contains(namesInEachBook[i][k]) || searchSpace.contains(namesInEachBook[i][k])) continue;
                    searchSpace.add(namesInEachBook[i][k]);
                }
                break;
            }
        }
    Iterator i=searchSpace.iterator();
    while(i.hasNext())
    {
        String cSearchElem=(String)i.next();
        search(cSearchElem,ernosDepth+1,searchedElem);
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:40:34+00:00Added an answer on June 15, 2026 at 7:40 am

    Apart from NumberFormatException which may be generated if input does not contain an int, the program also does not handle termination of inputs in a good way.

    You also do not use any memoization technique and building the same search tree every time, which will result in Time Limit Exceeded error on UVa Judge.

    You should also use Buffering of both Input & Output for further reduction of Compilation times. Reduce calls to functions and if possible inline them that is, write within the same scope.

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm solving sphere's online judge shortest path problem . This bit of code is
I'm solving Sphere's Online Judge Prime Generator using the Sieve of Eratosthenes. My code
I'm solving UVA's Edit Step Ladders on an uva sub-site named programming-challenges.com, but since
I'm currently solving a programming problem to enhance my skills (I'm still a newbie)
While solving any programming problem, what is your modus operandi ? How do you
I am have difficulties solving this problem: For a positive number n, define C(n)
I was solving a programming problem, which wants to find the SYMMETRIC DIFFERENCE between
I'm currently solving a problem of starting external tool from .net app. I have
While solving exercises from the K&R C book, I stumbled upon the exercise 2.1.
After solving countless problems with the hg-fast-export tool on Windows (from finicky python version

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.