-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlab11first.cpp
62 lines (56 loc) · 978 Bytes
/
lab11first.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
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
using namespace std;
vector <vector <int> > adj;
vector<int> color;
bool dfs(int u)
{
bool temp=true;
for(auto x:adj[u]){
if(color[x]==-1)
{
color[x]=1-color[u];
temp=(temp&dfs(x));
}
else if(color[x]+color[u]!=1) return false;
}
return temp;
}
// Driver Code
int main()
{
// no of nodes
int n,m,x,y;
cin>>n;
adj.resize(n);
color.resize(n);
for(int i = 0;i<n;i++)
{
color[i]=-1;
}
cin>>m;
for(int i = 0;i<m;i++)
{
cin>>x>>y;
adj[x-1].push_back(y-1);
adj[y-1].push_back(x-1);
}
bool ans=true;
for(int i = 0; i<n;i++)
{
if(color[i]==-1)
{
color[i]=0;
ans=dfs(i);
}
if(ans==0)break;
}
if(ans==0)
{
cout<<"NO";
}
else{
cout<<"YES";
}
cout<<endl;
return 0;
}