-
Notifications
You must be signed in to change notification settings - Fork 1
/
postmid_lab4.cpp
47 lines (34 loc) · 732 Bytes
/
postmid_lab4.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
#include <bits/stdc++.h>
using namespace std;
bool bipartite(int v, vector<int> arr[], vector<int>& visited, vector<int>& colour){
visited[v] = 1;
for(int i : arr[v]){
if(!visited[i]){
visited[i]=1;
colour[i]=1-colour[v];
if(!bipartite(i, arr, visited, colour)) return false;
}else{
if(colour[i]==colour[v]) return false;
}
}
return true;
}
int main(){
int n, m;
cin>>n>>m;
vector<int> arr[n+1];
vector<int> visited(n+1);
vector<int> colour(n+1);
for(int i=0; i<m; i++){
int u, v;
cin>>u>>v;
arr[u].push_back(v);
arr[v].push_back(u);
}
for(int i=0; i<n; i++){
if(visited[i+1]==0)
if(!bipartite(i+1, arr, visited, colour)){ cout<<"NO\n"; return 0;}
}
cout<<"YES\n";
return 0;
}