-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBipartite_Check.cpp
54 lines (51 loc) · 939 Bytes
/
Bipartite_Check.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
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int isBipartitite(vector<ll> adj[],int u, vector<ll> color)
{
for(ll j = 0; j < adj[u].size(); j++)
{
if (color[adj[u][j]] == -1){
color[adj[u][j]] = 1 - color[u];
return isBipartitite(adj, adj[u][j], color);
}
if (color[u] == color[adj[u][j]]){
return 0;
}
}
return 1;
}
int main()
{
int n, m;
cin >> n >> m;
ll x, y;
vector<ll> adj[n + 1];
for(ll i = 0; i < m; i++)
{
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<ll> color(n+1, -1);
int ans;
if (color[1] == -1) {
color[1] = 1;
ans = isBipartitite(adj , 1, color);
}
if (ans == 1) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
/*
Input:
4 3
1 2
2 3
3 4
Output:
Yes
*/