Skip to content

Commit 37e746f

Browse files
authored
Adding SPOJ Problem
Bipartite algorithm is very simple to code. In this we pass one type of node to root and to every adjacent child of it we pass oppsite type and so on for every other node. The sample problem taken from SPOJ named BUGLIFE. URL: https://www.spoj.com/problems/BUGLIFE/
1 parent 52ceacc commit 37e746f

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* Bipartite algorithm is very simple to code.
2+
In this we pass one type of node to root and to every adjacent child of it
3+
we pass oppsite type and so on for every other node
4+
*/
5+
6+
7+
#include<iostream>
8+
using namespace std;
9+
#include<queue>
10+
#include<map>
11+
#include<math.h>
12+
#define lli long long int
13+
#include<vector>
14+
#define vi vector<int>
15+
#define fo(i,n) for(int i=1;i<=n;i++)
16+
17+
#include <bits/stdc++.h>
18+
19+
vi adj[2003];
20+
int vis[2003] , col[2003];
21+
22+
bool dfs(int node,int color){
23+
24+
vis[node]=1;
25+
col[node]=color;
26+
27+
for(int child: adj[node]){
28+
if(!vis[child]){
29+
if(!dfs(child,color^1)) return false;}
30+
31+
else if(vis[child]==1){
32+
if(col[child]==col[node]) return false;
33+
}
34+
35+
}
36+
37+
return true;
38+
}
39+
40+
int main(){
41+
int t,a,b; cin>>t;
42+
43+
fo(k,t){
44+
45+
for(int i=0;i<2003;i++) vis[i]=0, adj[i].clear();
46+
47+
lli n,m; cin>>n>>m;
48+
49+
while(m--)
50+
51+
cin>>a>>b , adj[a].push_back(b) , adj[b].push_back(a);
52+
53+
cout<<"Scenario #"<<k<<":"<<endl;
54+
55+
if(dfs(1,0))
56+
cout<<"Suspicious bugs found!"<<endl;
57+
else
58+
cout<<"No suspicious bugs found!"<<endl;
59+
60+
61+
}
62+
63+
}

0 commit comments

Comments
 (0)