This is a standard bipartite graph question which involves coloring the graph in two colors such that the adjacent vertices never have the same color.
It can be solved easily by applying DFS and passing a parameter color(either "0" or "1") in my case and check for each vertex
--> if its adjacent is unmarked then mark it with a different color
--> if it is already marked and that too with the same color as the present vertex then bipartite graph cannot be formed(here flag is set to check this).
//\\__ hr1212 __//\\
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007
int n,m,flag,mk[MAX],color[MAX];
vi v[MAX];
int dfs(int x,int c){
int i;
mk[x]=1;
color[x]=c;
f(i,0,v[x].size()){
if(!mk[v[x][i]])
dfs(v[x][i],1-c);
else if(color[v[x][i]]==color[x])
flag=1;
}
}
int main(){
int r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
ll ans=0;string p;
while(si(n)){
if(n==0)
break;
clr(mk,0);
clr(color,-1);
si(m);
f(i,0,m){
sii(x,y);
v[x].pb(y);
v[y].pb(x);
}
flag=0;
dfs(0,0);
if(flag)
printf("NOT BICOLORABLE.\n");
else
printf("BICOLORABLE.\n");
f(i,0,n+5)
v[i].clear();
}
return 0;
}
It can be solved easily by applying DFS and passing a parameter color(either "0" or "1") in my case and check for each vertex
--> if its adjacent is unmarked then mark it with a different color
--> if it is already marked and that too with the same color as the present vertex then bipartite graph cannot be formed(here flag is set to check this).
My Code:
//\\__ hr1212 __//\\
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007
int n,m,flag,mk[MAX],color[MAX];
vi v[MAX];
int dfs(int x,int c){
int i;
mk[x]=1;
color[x]=c;
f(i,0,v[x].size()){
if(!mk[v[x][i]])
dfs(v[x][i],1-c);
else if(color[v[x][i]]==color[x])
flag=1;
}
}
int main(){
int r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
ll ans=0;string p;
while(si(n)){
if(n==0)
break;
clr(mk,0);
clr(color,-1);
si(m);
f(i,0,m){
sii(x,y);
v[x].pb(y);
v[y].pb(x);
}
flag=0;
dfs(0,0);
if(flag)
printf("NOT BICOLORABLE.\n");
else
printf("BICOLORABLE.\n");
f(i,0,n+5)
v[i].clear();
}
return 0;
}
No comments:
Post a Comment