Skip to content

Commit fcab084

Browse files
committed
BUGLIFE - A Bug’s Life
1 parent 9815328 commit fcab084

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

SPOJ/BUGLIFE.cpp

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* Amit Bansal - @amitbansal7 */
2+
#include <bits/stdc++.h>
3+
#include <string>
4+
#define lli long long int
5+
#define llu unsigned long long int
6+
#define S(x) scanf("%d",&x)
7+
#define Sl(x) scanf("%lld",&x)
8+
#define Mset(p,i) memset(p,i,sizeof(p))
9+
#define mlc(t,n) (t *)malloc(sizeof(t)*n)
10+
#define NIL -1
11+
#define INF 0x3f3f3f3f
12+
#define TC int testcase; S(testcase); while(testcase--)
13+
#define Pi 3.14159
14+
using namespace std;
15+
16+
bool isBipartite(vector<int>adj[],int v,int src,int visited[],int color[])
17+
{
18+
visited[src] = 1;
19+
queue <int>Q;
20+
color[src] = 1;
21+
Q.push(src);
22+
23+
while(!Q.empty())
24+
{
25+
int u = Q.front();
26+
Q.pop();
27+
vector <int>::iterator it;
28+
for(it = adj[u].begin();it != adj[u].end();it++)
29+
{
30+
if(!visited[*it] && color[*it]==NIL)
31+
{
32+
visited[*it] = 1;
33+
if(color[u] == 1)
34+
color[*it] = 0;
35+
else
36+
color[*it] = 1;
37+
38+
Q.push(*it);
39+
}
40+
else if(color[u] == color[*it])
41+
return false;
42+
43+
}
44+
}
45+
46+
return true;
47+
}
48+
49+
int main()
50+
{
51+
int test;
52+
S(test);
53+
for(int i=1;i<=test;i++)
54+
{
55+
int v,e,y,x;
56+
S(v);S(e);
57+
v++;
58+
vector <int>adj[v];
59+
60+
for(int i=0;i<e;i++)
61+
{
62+
S(x);S(y);
63+
adj[x].push_back(y);
64+
adj[y].push_back(x);
65+
}
66+
67+
int visited[v];
68+
Mset(visited,0);
69+
bool ans = 1;
70+
int color[v];
71+
Mset(color,-1);
72+
for(int i=1;i<v;i++)
73+
if(color[i]==-1)
74+
{
75+
ans = isBipartite(adj,v,i,visited,color);
76+
if(ans == 0)
77+
break;
78+
}
79+
80+
printf("Scenario #%d:\n",i);
81+
if(ans)
82+
printf("No suspicious bugs found!\n");
83+
else
84+
printf("Suspicious bugs found!\n");
85+
}
86+
return 0;
87+
}
88+

0 commit comments

Comments
 (0)