Could you please find the bug in the euler circuit in java.. Pleaseee

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

public class ConnectingNode {

static int[][] a;
static int n, m;
static int[] degree;
static boolean[] visited;
public static void main(String[] args) {
	// TODO Auto-generated method stub
	try(BufferedReader bf = new BufferedReader(new InputStreamReader(System.in))){
		Scanner in = new Scanner(System.in);
		int testCase = in.nextInt();
		for(int t = 1; t <= testCase; t++){
			n = in.nextInt();
			m = in.nextInt();
			a = new int[n][n];
			degree = new int[n];
			visited = new boolean[n];
			for(int i = 0; i < m; i++){
				int p = in.nextInt();
				int q = in.nextInt();
				a[p-1][q-1] = 1;
				a[q-1][p-1] = 1;
				degree[p-1]++;
				degree[q-1]++;
			}
			if(isEven()){
				if(isConnected()){
					System.out.println("Case "+t+": Yes");
				}else{
					System.out.println("Case "+t+": No");
				}
			}else{
				System.out.println("Case "+t+": No");
			}
			//System.out.println("Case "+t+": "+(isEven()?(isConnected()?"Yes":"No"):"No"));
		}
	}catch(IOException exc){
		System.out.println(exc);
	}
}

static void dfs(int dest){
	visited[dest] = true;
	for(int i = 0; i < n; i++){
		if(!visited[i]&a[dest][i]==1){
			dfs(i);
		}
	}
}

static boolean isEven(){
	for(int i = 0; i < n; i++){
		if(degree[i]%2!=0){
			return false;
		}
	}
	return true;
}

static boolean isConnected(){
	dfs(0);
	for(int i = 0; i < n ; i++){
		if(!visited[i]){
			return false;
		}
	}
	return true;
}

}
Problem

Please help me… I got stuck…

“if(!visited[i]&a[dest][i]==1)” could not make it accepted but here “&” should be “&&”
“if(!visited[i]&&a[dest][i]==1)”

1 Like

Hey, your logic seems to be correct don’t know the reason for NZEC. But an exact similar implementation in C++ gave an AC. You can see here.

1 Like

Hm, check this out. It suggests that it might be due to program bugs which cause segmentation fault (unlikely here), or if input format is not correct. Quoting the key phrase-

Barring unlikely scenarios, the only possibilities for getting a nonzero exit code with that programme that I see are
1.division by zero, but that can't happen if the input conforms to the specifications
2.unexpected input format resulting in a NumberFormatException 

Assuming @ssaxena32 's implementation and things are exactly same, and only difference is that he used C++ instead of JAVA, I advise you to try Buffered Reader and see if it still gives NZE error or not. If the issue resolves, then the problem is in input/testcases.

Check and get back to me in case issue doesn’t resolve.

1 Like

@sagar2009kumsr, your code is quite fine but there are issues with the test cases. This submission gives a runtime error and if you see this one you can understand that the runtime error you are getting is a NoSuchElementException (this gets WA because I suprressed that particular error), and another submission seems to indicate the the end-of-file is reached prematurely as it returns with a NZEC.

My best guess is that the test input is an incomplete file which is causing the error when you try to read beyond the end of the file, so it is not a fault of your code.

1 Like

Would you please give me reason for this… I would realize my mistake. Hey , brother i am getting NZEC which really sucks me and rips apart.

& is for bitwise and, && is logical and, I think you want to make logical and… that’s why

But still i am getting NZEC what could be the possible reason for that…Are the logics are correct

logic seems to be correct…

Hey… @vijju123 could you please give a try to the question… and help debugging it.

My brain fused on hearing a scary name as euler circuit XD. Sorry bro, I have no idea about it as of yet.

Thanks @ssaxena36 for pointing out. I don’t know i think the testcases are wrong… How to check that. Do you have any idea…

Yeah, I have used BufferedReader first… That doesn’t work… That’s why i move to Scanner Class…

There must be some mismatch in the input format of the testcases that is why a runtime error is produced in Java. C++ is efficiently able to handle them.

1 Like

I too feel the same @ssaxena36 . Mismatch in input format in test cases is likely.

Thank you all… for your time…

@meooow Thanks for your help…

Yeah, even I suspected that it was due to test case input. Thanks for confirming it!!