-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbipartite_graph.h
44 lines (38 loc) · 933 Bytes
/
bipartite_graph.h
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
class BipartiteGraph {
private:
vector<vector<int>> graph;
int V; // 頂点数
vector<int> color; // 頂点iの色(1 or -1)
bool Dfs(int v, int color);
public:
BipartiteGraph(int n);
void AddEdge(int f, int t);
bool Run(); // 2部グラフか否か
};
BipartiteGraph::BipartiteGraph(int n):
graph(vector<vector<int>>(n)),
V(n),
color(vector<int>(n))
{}
void BipartiteGraph::AddEdge(int f, int t){
graph[f].push_back(t);
graph[t].push_back(f);
}
bool BipartiteGraph::Dfs(int v, int c){
color[v] = c;
for(auto x: graph[v]){
if(color[x] == c) return false;
if(color[x] == 0 && !Dfs(x, -c)) return false;
}
return true;
}
bool BipartiteGraph::Run(){
for(int i = 0; i < V; i++){
if(color[i] == 0){
if(!Dfs(i, 1)){
return false;
}
}
}
return true;
}