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