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

×

Getting multiple answers for same input and same code (Squence Land INOI 2013)

I have tried to read input from file and then I get the write output but when I use scanf() or cin to read inputs then i get different outputs on eclipse every time I run it , even I have tried running the code on online IDEs then also I get different output for same code and input. Here is my code:

    #include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
bool bin_search(vector<int> a,int k){
    int left=0;
    int right=a.size()-1;
    int mid;
    int p;
    while(true){
        mid=(left+right)/2;
        if(left+1==right){
            if(k==a[right]||k==a[left]){
                return true;
            }else {
                return false;
            }
        }
        p=a[mid];
        if(p==k){
            return true;
            break;
        }
        if(p>k){
            right=mid;
        }else{
            left=mid;
        }

    }
        return true;
}
int main() {
    // your code goes here

    int n,m,s,k1,k2,p,ma=0;
    scanf("%d %d ", &n, &m);
    vector<vector<int> > abc;
    vector<int> ab;
    for(int i=0;i<n;i++){
        scanf("%d",&p);

        for(int j=0;j<p;j++){
            scanf("%d",&k1);
            ab.push_back(k1);
        }
        sort(ab.begin(),ab.end());
        abc.push_back(ab);
        ab.clear();

    }
    queue<int> q;
    bool vis[300];
    int temp_k=0;
    int family=0;
    q.push(0);
    while(!q.empty()){
        int s=q.front();
        q.pop();
        for(int i=1;i<n;i++){
            temp_k=0;

                int j=0;
                if(vis[i]==0){

                while(j<=abc[i].size()-1){
                    if(bin_search(abc[s],abc[i][j])==1){
                        temp_k++;
                        if(temp_k>=m){
                            q.push(i);
                            vis[i]=1;
                            family++;
                            break;
                        }
                    }
                    j++;
                }
            }
        }
    }
    cout<<family+1;

    return 0;
}

asked 25 Nov '16, 12:50

kartik3390's gravatar image

2★kartik3390
173
accept rate: 0%


@kartik3390 this might be because you haven't initialized your boolean array vis.

link

answered 25 Nov '16, 17:01

srd091's gravatar image

5★srd091
1.5k111
accept rate: 42%

2

Thanks soo much dude , it is working now but doesnt the bool array automatically gets initialised to 0 false?

(25 Nov '16, 22:02) kartik33902★
4

@kartik3390 If your array is global or static it will get initialized with default values. Otherwise, it won't.

(25 Nov '16, 22:03) srd0915★
2

Ok got it . :)

(25 Nov '16, 22:30) kartik33902★

you haven't initialized vis try using vector<bool> vis(300,false).

link

answered 25 Nov '16, 17:15

diveshuttam's gravatar image

4★diveshuttam
5398
accept rate: 27%

2

Thanks , it's working :) now.

(25 Nov '16, 22:02) kartik33902★
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:

×1,687
×1,470
×327
×143

question asked: 25 Nov '16, 12:50

question was seen: 458 times

last updated: 25 Nov '16, 22:30