-
Notifications
You must be signed in to change notification settings - Fork 0
/
bipartitegraph.cpp
85 lines (82 loc) · 1.96 KB
/
bipartitegraph.cpp
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
/*its vertices can be divided into two disjoint
and independent sets u and v such that every edge connects a vertex in u to one in v
the graph does not contain any odd-length cycles
the graph is 2 colorable*/
#include <iostream>
#include <cmath>
#include <stack>
#include <cstring>
#include <stdlib.h>
#include <unordered_set>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <algorithm>
#include <queue>
using namespace std;
#define vi vector<int>
#define vvi vector<vi>
#define pii vector<int, int>
#define vii vector<pii>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define ff first
#define ss second
bool bipart;
vvi adj;
vector<bool> vis;
vector<int> col;
bool dfs(int node, int curr, int col[], vector<int> adj[]){
col[node]=curr;
for(auto i: adj[node]){
if(col[i]==-1){
if(dfs(i,!curr, col,adj)==false){
return false;
}
}
else if(col[i]==curr){
return false;
}
}
return true;
}
bool isbipartite(int v, vector<int> adj[]){
int col[v];
for(int i=0;i<v;i++){
col[i]=-1;
}
for(int i=0;i<v;i++){
if(col[i]==-1){
if(dfs(i,0,col,adj)==false){
return false;
}
}
}
return true;
}
bool bfs(int start, vector<int> adj[], vector<int>&col){
queue<int> q;
q.push(start);
col[start]=0;
while(!q.empty()){
int node=q.front();
q.pop();
for(auto it:adj[node]){
if(col[it]!=-1){
q.push(it);
col[it]=!col[node];
}
else if(col[it]==col[node])return false;
}
}
return true;
}
bool isbipartite(int v, vector<int> adj[]){
vector<int> col(v,-1);
for(int i=0;i<v;i++){
if(col[i]!=-1){
if(!bfs(i,adj,col))return false;
}
}
return true;
}