Hi i am doing this problem https://www.spoj.com/problems/BUGLIFE/ on SPOJ but i am getting WA, can anyone help. Here is my code.
I'm trying to use sets to solve this problem. I've heard about using Bipartite Graphs to solve this problem but I think using this approach should suffice unless there's some fault with my approach . I've tried quite a few Testcases , but I don't know where my code is failing .
Additional Test cases for anyone who's willing to help :-
http://spojtoolkit.com/history/BUGLIFE
Expected output for the test cases
:- http://spojtoolkit.com/test/BUGLIFE
Sample Input :-
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
Sample Output :-
Scenario #1:
Suspicious bugs found!
Scenario #2:
No suspicious bugs found!
My code's output is same as expected output .
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
for(int s = 0 ; s < t ; s++ ){
int bugs , inter; // Bugs and Interactions
cin >> bugs >> inter;
map<int,int> isDiscovered , male , female;
int bug1 , bug2;
vector<pair<int , int> > b; //stores pair of interactions
for(int i = 0 ; i < inter ; i++){
cin >> bug1 >> bug2;
b.push_back(make_pair(bug1,bug2));
}
sort(b.begin() , b.end());
bool ans = true;
for(int i = 0 ; i < b.size() ; i++){
bug1 = b[i].first;
bug2 = b[i].second;
//both not classified
if(isDiscovered.find(bug1) == isDiscovered.end() && isDiscovered.find(bug2) == isDiscovered.end()){
isDiscovered[bug1]++;
isDiscovered[bug2]++;
male[bug1]++;
female[bug2]++;
}
//one classified
if(isDiscovered.find(bug1) != isDiscovered.end() || isDiscovered.find(bug2) != isDiscovered.end()){
if(isDiscovered.find(bug1) == isDiscovered.end()){
//bug1 does not exist
isDiscovered[bug1]++;
if(male.find(bug2) == male.end()){
//bug2 is female
male[bug1]++;
}
else{
//bug2 is male
female[bug1]++;
}
}
else{
//bug2 does not exist
isDiscovered[bug2]++;
if(male.find(bug1) == male.end()){
//bug1 is female
male[bug2]++;
}else{
female[bug2]++;
}
}
}
//both classified
if(isDiscovered.find(bug1) != isDiscovered.end() && isDiscovered.find(bug2) != isDiscovered.end()){
if(male.find(bug1) != male.end() && male.find(bug2) != male.end()){
//both males
ans = false;
}
else if(female.find(bug1) != female.end() && female.find(bug2) != female.end()){
//both females
ans = false;
}
}
if(ans == false){
break;
}
}
cout << "Scenario #" << s+1 << ":" << endl;
if(ans == false){
cout << "Suspicious bugs found!" << endl;
}
else{
cout << "No suspicious bugs found!" << endl;
}
}
return 0;
}
In case both bugs are new, you unconditionally say that the first one is a male, and the second one is female. It is not necessarily so. We only may assume that they are of different genders, but there is no grounds to assign the gender yet. Try
4 2
2 1
3 4
(same as case 2, with the second pair swapped).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.