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

×

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

asked 23 Mar '17, 18:03

sagar2009kumsr's gravatar image

1★sagar2009kumsr
234
accept rate: 0%


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.

link

answered 24 Mar '17, 02:51

meooow's gravatar image

6★meooow ♦
7.0k718
accept rate: 48%

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:

×125
×5

question asked: 23 Mar '17, 18:03

question was seen: 313 times

last updated: 24 Mar '17, 02:51