Could you please find the bug in the code that i submitted in TOURIST of jan long challenge ??

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

public class TOURIST3 {

static Node[] vertex;
static int n, e;
static ArrayList<Point> alp;
public static void main(String[] args) {
	// TODO Auto-generated method stub
	try(BufferedReader bf = new BufferedReader(new InputStreamReader(System.in))){
			StringTokenizer stk = new StringTokenizer(bf.readLine());
			n = Integer.parseInt(stk.nextToken());
			e = Integer.parseInt(stk.nextToken());
			vertex = new Node[n];
			for(int i = 0; i < n; i++){
				vertex[i] = new Node(i);
			}
			int p, q;
			for(int i = 0; i < e; i++){
				stk = new StringTokenizer(bf.readLine());
				p = Integer.parseInt(stk.nextToken())-1;
				q = Integer.parseInt(stk.nextToken())-1;
				vertex[p].al.add(q);
				vertex[q].al.add(p);
				vertex[p].degree++;
				vertex[q].degree++;
			}
			alp = new ArrayList<Point>();
			if(isEven()){
				if(isConnected()){
					System.out.println("YES");
					for(int i = 0; i < alp.size(); i++){
						System.out.println((alp.get(i).x+1)+" "+(alp.get(i).y+1));
					}
				}else{
					System.out.println("NO");
				}
			}else{
				System.out.println("NO");
			}
	}catch(IOException exc){
		System.out.println();
	}
}

static class Node{
	int myIndexNumber;
	boolean isVisited;
	int degree;
	ArrayList<Integer> al;
	Node(int i ){
		myIndexNumber = i;
		al = new ArrayList<Integer>();
	}
}

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

static class Point{
	int x, y;
	Point(int i, int j){
		x = i; y = j;
	}
}

static void dfs(int dest){
	vertex[dest].isVisited = true;
	for(int i = 0; i < vertex[dest].al.size();i++){
		int p = vertex[dest].al.get(i);
			vertex[dest].al.remove(new Integer(p));
			vertex[p].al.remove(new Integer(dest));
			alp.add(new Point(dest,p));
			dfs(p);
	}
}

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

}

Please help me finding the bug… i am getting 3 wrong answers for the testCases… @meooow @vijju123Problem Link

I do not know why the editorial contains swap operation. Please help me…

You solution seems to be okay, but the problem statement specifies that “The ith line of output should represent the ith road in the input. It should have two integers a and b denoting that the final orientation of that road is from a to b.”
So you are required to output the roads in the order they appeared in the input, which you need to fix first in your code.

3 Likes