-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBipartite_graph.cpp
More file actions
63 lines (60 loc) · 1.07 KB
/
Copy pathBipartite_graph.cpp
File metadata and controls
63 lines (60 loc) · 1.07 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
57
58
59
60
61
62
63
#include<bits/stdc++.h>
using namespace std;
int color[100005];
bool bipartite(vector<int>list[],int n)
{
queue<int>q;
for(int i=0;i<=n;i++)
{
color[i]=-1;
}
int ans=1;
for(int i=1;i<=n;i++)
{
if(color[i]==-1)
{
q.push(i);
color[i]=0;
while(!q.empty())
{
int v=q.front();
q.pop();
for(int u: list[v])
{
if(color[u]==-1)
{
color[u]=color[v] ^ 1;
q.push(u);
}
else
{
ans &=(color[u] !=color[v]);
}
}
}
}
}
return ans;
}
int main()
{
int n,m;
cin>>n>>m;
vector<int>list[n+1];
for(int i=0;i<m;i++)
{
int u,v;
cin>>u>>v;
list[u].push_back(v);
list[v].push_back(u);
}
bool ans=bipartite(list,n);
if(ans)
{
cout<<"YES";
}
else
{
cout<<"NO";
}
}