-
Notifications
You must be signed in to change notification settings - Fork 7
/
bipartite.cpp
63 lines (57 loc) · 1004 Bytes
/
bipartite.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
63
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1e5 + 9;
const long long INF = 1e6 + 9;
typedef long long lli;
typedef unsigned long long uli;
vector<int>g[MAX];
queue<int>que;
int color[MAX];
bool isBipartite;
bool bipartite(int s){
que.push(s);
color[s] = 0;
isBipartite = true;
while(!que.empty() && isBipartite)
{
int x = que.front();
que.pop();
cout<<x<<endl;
for (int i = 0; i < g[x].size(); ++i)
{
int y = g[x][i];
if(color[y] == INF)
{
color[y] = 1 - color[x];
que.push(y);
}
if(color[y] == color[x])
{
isBipartite = false;
}
}
}
if(isBipartite)return true;
return false;
}
int main()
{
int n,m,u,v;
cin>>n>>m;
for(int i=0;i<=n;i++)
{
color[i] = INF;
}
while(m--){
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
bipartite(1);
for(int i=1;i<=n;i++)
cout<<color[i]<<" ";
cout<<endl;
if(isBipartite)cout<<"is bipartite graph"<<endl;
else cout<<"not a bipartite graph"<<endl;
return 0;
}