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){
				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;
}

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

2 Likes

you haven’t initialized vis try using vector vis(300,false).

1 Like

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

1 Like

Thanks , it’s working :slight_smile: now.

1 Like

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

2 Likes

Ok got it . :slight_smile:

1 Like