CSES - Shared codeLink to this code:
https://cses.fi/paste/5d2c151ba7202436a24e3a/
#include <bits/stdc++.h>
using namespace std;
vector<int> TwoColor(vector<vector<int>>& graph) {
int n = graph.size();
vector<int> col(n, -1);
queue<int> q;
for (int i = 0; i < n; i++) {
if (col[i] == -1) q.push(i), col[i] = 0;
while (q.size()) {
int u = q.front(); q.pop();
for (auto v : graph[u]) {
if (col[v] == col[u]) return {};
if (col[v] == -1) {
col[v] = col[u]^1; // sets color to opposite of current
q.push(v);
}
}
}
}
return col;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
vector<vector<int>> graph(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--; v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
vector<int> col = TwoColor(graph);
if (col.empty()) cout << "IMPOSSIBLE\n";
else for (auto v : col) cout << v+1 << ' ';
return 0;
}