-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisGraphBipartite.cpp
More file actions
56 lines (53 loc) · 1.25 KB
/
Copy pathisGraphBipartite.cpp
File metadata and controls
56 lines (53 loc) · 1.25 KB
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
#include <bits/stdc++.h>
using namespace std;
// Is Graph Bipartite?
class Solution
{
public:
bool isBipartite(vector<vector<int>> &graph)
{
int n = graph.size();
vector<int> color(n, -1);
queue<int> q;
for (int i = 0; i < n; i++)
{
if (color[i] == -1)
{
q.push(i);
color[i] = 0;
while (!q.empty())
{
int src = q.front();
q.pop();
for (int neigh : graph[src])
{
if (color[neigh] == -1)
{
color[neigh] = (color[src]) ? 0 : 1;
q.push(neigh);
}
else if (color[neigh] == color[src])
return false;
}
}
}
}
return true;
}
};
int main()
{
int n;
cin >> n;
vector<vector<int>> graph(n);
int edges = 0;
cin >> edges;
while (edges--)
{
int u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
cout << Solution().isBipartite(graph);
}