Repository navigation
Expand file tree
/
Copy pathBUGLIFE.cpp
More file actions
115 lines (110 loc) · 2.1 KB
/
Copy pathBUGLIFE.cpp
File metadata and controls
115 lines (110 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<iostream>
#include<unordered_set>
#include<cstdio>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;
set<int> arr[2001];
bool test()
{
int n,m; // n = no. of bugs , m = no. of interactions
cin>>n>>m;
int i,j,k;
unordered_set<int> left;
for(i=1;i<=n;i++)
{
arr[i].clear();
left.insert(i);
}
while(m--)
{
cin>>i>>j;
arr[i].insert(j);
arr[j].insert(i);
}
//cout<<"The graph is initialized:"<<endl;
set<int> white;
set<int> black;
queue<int> q1,q2; //q1=white, q2=black
set<int>::iterator it;
bool tr=true;
int base;
//int tmp,tmp2;
// now start processing
while(left.size()!=0)
{
base=*left.begin();
white.insert(base);
left.erase(base);
q1.push(base); //push the first element
// cout<<"Base ele: "<<base<<endl;
// cin>>k;
while(!q1.empty() || !q2.empty())
{
// cout<<"Size of white queue :"<<q1.size()<<endl;
// cin>>k;
while(!q1.empty()) // there are white elements
{
// cout<<"Processing (white) : "<<q1.front()<<endl;
it=arr[q1.front()].begin();
for(;it!=arr[q1.front()].end();it++) //over all the links of q1.front()
{
if( white.find(*it)!=white.end() ) //i.e. the element is in white
{
tr=false; //invalid situation
break;
}
else if( black.find(*it)==black.end() )
{
black.insert(*it);
q2.push(*it);
left.erase(*it);
}
}
q1.pop();
}
// cout<<"Size of black queue :"<<q2.size()<<endl;
// cin>>k;
while(!q2.empty())
{
// cout<<"Processing (black) : "<<q2.front()<<endl;
it=arr[q2.front()].begin();
for(;it!=arr[q2.front()].end();it++)
{
if( black.find(*it)!=black.end())
{
tr=false;
break;
}
else if(white.find(*it)==white.end())
{
white.insert(*it);
q1.push(*it);
left.erase(*it);
}
}
q2.pop();
}
}
}
return tr;
}
int main()
{
int n;
cin>>n;
int i;
bool tk;
for(i=1;i<=n;i++)
{
tk=test();
cout<<"Scenario #"<<i<<":"<<endl;
if(tk)
cout<<"No suspicious bugs found!"<<endl;
else
cout<<"Suspicious bugs found!"<<endl;
}
return 0;
}