You are not logged in. Please login at www.codechef.com to post your questions!

×

NZEC in FIREESC (language used is Java)

I am trying to solve FIRESC question but getting Run Time Error. I tried a lot but couldn't figure it out why

Can anybody of you help me?

Here is link to my solution

PS: I am using uwi's input/output file format

asked 22 Jun '15, 18:01

ankurverma1994's gravatar image

4★ankurverma1994
415114
accept rate: 8%

edited 23 Jun '15, 13:27


See the changes i have made to your code now it gives WA. I have written a try catch in main function to handle any type of error using throwable class, your code works fine for small values but for large values it is throwing error may be OutOFMemory, so try to solve in different way. Before that if your interested to find real error try with differnet Error classes from this link. Replace throwable in main with different classes and see when you get WA and not NZEC then that is the error occuring. Hope my answer helps, comment any queries.

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.InputMismatchException;
/* *
 *
 * @author ankurverma1994
 */
class FIRESC {
    InputStream obj;
    PrintWriter out;
    String check="";
    public static void main(String[] args)throws IOException{
        try{
            new FIRESC().main1();
           // code which you think might throw exception
        }catch(java.lang.Throwable t){
           // you got the exception. Now what??
        }
      //new FIRESC().main1();

    }
    void main1()throws IOException{
        out=new PrintWriter(System.out);
        obj=check.isEmpty() ? System.in : new ByteArrayInputStream(check.getBytes());
        solve();
        out.flush();
        out.close();
    }
    //------------> Solution starts here!!
    boolean visit[];
    int comp_size=0;
    void solve(){
        int mod=1000000007;
        for(int tc=ii();tc>0;tc--){
            int n=ii();
            visit=new boolean[n];
            ArrayList<Integer> graph[]=new ArrayList[n];
            for(int i=0;i<n;i++)
                graph[i]=new ArrayList<Integer>();
            for(int friends=ii();friends>0;friends--){


                int u=ii(),v=ii();
                graph[u-1].add(v-1);
                graph[v-1].add(u-1);
            }
            long ans=0;
            long ways=1;
            for(int u=0;u<n;u++){
                if(!visit[u]){
                    ans++;
                    comp_size=0;
                    dfs(graph, u);
                    ways=(comp_size*ways)%mod;
                }
            }
            out.println(ans+" "+ways);
        }
    }
    void dfs(ArrayList graph[],int ver)
    {
        comp_size++;
        visit[ver]=true;
        for(int i=0;i<graph[ver].size();i++){

            if(!visit[(int)graph[ver].get(i)])

                dfs(graph,(int) graph[ver].get(i));
        }
    }
    //------------> Solution ends here!!
    byte inbuffer[]=new byte[1024];
    int lenbuffer=0,ptrbuffer=0;
    int readByte(){
        if(lenbuffer==-1)
            throw new InputMismatchException();
        if(ptrbuffer>=lenbuffer)
        {
            ptrbuffer=0;
            try {
                lenbuffer=obj.read(inbuffer);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
        }
        if(lenbuffer<=0)
            return -1;
        return inbuffer[ptrbuffer++];
    }

    int ii()
    {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-')
        {
            minus = true;
            b = readByte();
        }
        while(true)
        {
            if(b >= '0' && b <= '9')
            {
                num = num * 10 + (b - '0');
            }
            else
            {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }


}
link

answered 24 Jun '15, 11:47

drj_reddy's gravatar image

3★drj_reddy
186110
accept rate: 8%

I don't think this is due to OutOfMemoryError. As I have catched this error in main and submitted the solution but the judge shows NZEC

(28 Jun '15, 15:23) ankurverma19944★

Problem solved using iterative DFS. http://www.codechef.com/viewsolution/7322206

When using recursive DFS it gives StackOverflowError in Java but same type solution gets AC using C++.

I wonder Java throws Overflow Error before C++

link

answered 03 Jul '15, 14:19

ankurverma1994's gravatar image

4★ankurverma1994
415114
accept rate: 8%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×545
×21

question asked: 22 Jun '15, 18:01

question was seen: 1,068 times

last updated: 03 Jul '15, 14:19