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

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();
        }
    }
    
    
}
1 Like

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

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++