-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
46 lines (42 loc) · 836 Bytes
/
main.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
#include <vector>
#include <iostream>
using namespace std;
bool bipartite = true;
vector<vector<int>> edges;
vector<int> colors;
int invert(const int& color) {
return (color == 1) ? 2 : 1;
}
void dfs(const int& u, const int& color) {
colors[u] = color;
for (const int& v : edges[u]) {
if (colors[v] == 0) {
dfs(v, invert(color));
} else if (colors[v] == color) {
bipartite = false;
}
}
}
int main() {
int n, m;
cin >> n >> m;
edges.resize(n);
colors.resize(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
--u, --v;
edges[u].push_back(v);
edges[v].push_back(u);
}
for (int u = 0; u < n; u++) {
if (colors[u] == 0) {
dfs(u, 1);
if (!bipartite) {
break;
}
}
}
cout << ( bipartite ? "YES" : "NO" ) << endl;
return 0;
}