forked from shivammaniharsahu/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbipartite graph.cpp
More file actions
71 lines (71 loc) · 881 Bytes
/
bipartite graph.cpp
File metadata and controls
71 lines (71 loc) · 881 Bytes
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
64
65
66
67
68
69
70
71
#include<bits/stdc++.h>
using namespace std;
int vis[100001];
int col[100001];
vector<int> ar[100001];
bool dfs(int n,int c)
{
vis[n]=1;
col[n]=c;
for(int i=0;i<ar[n].size();i++)
{
if(vis[ar[n][i]]==0)
{
if(dfs(ar[n][i],c^1)==false)
{
return false;
}
}
else
{
if(col[n]==col[ar[n][i]])
{
return false;
}
}
}
return true;
}
int main()
{
bool res;
int n,m,a,b,flag=1;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>a>>b;
ar[a].push_back(b);
ar[b].push_back(a);
}
for(int i=1;i<=n;i++)
{
if(vis[i]==0)
{
res=dfs(i,1);
if(res==false)
{
flag=0;
break;
}
}
}
if(flag==0)
{
cout<<"IMPOSSIBLE";
}
else
{
for(int i=1;i<=n;i++)
{
if(col[i]==0)
{
cout<<2<<" ";
}
else
{
cout<<1<<" ";
}
}
}
return 0;
}