Problem Statement

Building Teams

Implementation


void solve() {
    ll n, m;
    cin >> n >> m;

    vvll v(n);
    rep(i, m) {
        ll x, y;
        cin >> x >> y;
        --x, --y;
        v[x].pb(y); v[y].pb(x);
    }

    vll ans(n, -1);
    function <bool(ll, ll, ll)> dfs = [&](ll node, ll p, ll c) {
        debug(p, node);
        ans[node] = c;
        for (auto& child : v[node]) {
            if (child == p) continue;
            if (ans[child] == -1) {
                if (dfs(child, node, c ^ 1) == 1) return 1;
            }
            else if (ans[child] == c) return 1;
        }
        return 0;
        };

    bool ok = 1;
    rep(i, n) {
        if (ans[i] == -1) {
            if (dfs(i, i, 0)) {
                ok = 0;
            }
        }
    }

    if (!ok) cout << "IMPOSSIBLE\n";
    else {
        rep(i, n) cout << ans[i] + 1 << " ";
        cout << "\n";
    }

}