Problem: 2049C - MEX Cycle
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>

using namespace std;

void solve(int n, int x, int y) {

    vector<vector<int>> adj(n + 1, vector<int>());

    for (int i = 0; i < n; ++i) {

        int prev = (i - 1 + n) % n;

        int next = (i + 1) % n;

        adj[i].push_back(prev);

        adj[i].push_back(next);

    }

    auto is_adjacent = [&](int u, int v) -> bool {

        return (u == v - 1) || (u == v + 1) || (u == 0 && v == n - 1) || (u == n - 1 && v == 0);

    };

    if (!is_adjacent(x, y)) {

        adj[x].push_back(y);

        adj[y].push_back(x);

    }

    vector<int> labels(n + 1, 0);

    queue<int> q;

    for (int i = 0; i < n; ++i) {

        q.push(i);

    }

    vector<bool> in_queue(n + 1, true);

    while (!q.empty()) {

        int u = q.front();

        q.pop();

        in_queue[u] = false;

        int mask = 0;

        for (const auto &v : adj[u]) {

            if (labels[v] < 32) {

                mask |= (1 << labels[v]);

            }

        }

        int mex = 0;

        while (mask & (1 << mex)) {

            ++mex;

        }

        if (labels[u] != mex) {

            labels[u] = mex;

            for (const auto &v : adj[u]) {

                if (!in_queue[v]) {

                    q.push(v);

                    in_queue[v] = true;

                }

            }

        }

    }

    for (int i = 0; i < n; ++i) {

        cout << labels[i] << " ";

    }

    cout << "\n";

}

int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    int t;

    cin >> t;

    while (t--) {

        int n, x, y;

        cin >> n >> x >> y;

        solve(n, x, y);

    }

    return 0;

}



Submission (verdict: Accepted):
#include <bits/stdc++.h>

using namespace std;

void solve(int n, int x, int y) {

    vector<vector<int>> adj(n + 1, vector<int>());

    for (int i = 1; i <= n; ++i) {

        int prev = i - 1;

        if (prev == 0) {

            prev = n;

        }

        int next = i + 1;

        if (next > n) {

            next = 1;

        }

        adj[i].push_back(prev);

        adj[i].push_back(next);

    }

    auto is_adjacent = [&](int u, int v) -> bool {

        return (u == v - 1) || (u == v + 1) || (u == 1 && v == n) || (u == n && v == 1);

    };

    if (!is_adjacent(x, y)) {

        adj[x].push_back(y);

        adj[y].push_back(x);

    }

    vector<int> labels(n + 1, 0);

    queue<int> q;

    for (int i = 1; i <= n; ++i) {

        q.push(i);

    }

    vector<bool> in_queue_flag(n + 1, true);

    while (!q.empty()) {

        int u = q.front();

        q.pop();

        in_queue_flag[u] = false;

        int mask = 0;

        for (auto &v : adj[u]) {

            if (labels[v] < 32) {

                mask |= (1 << labels[v]);

            }

        }

        int mex = 0;

        while (mask & (1 << mex)) {

            ++mex;

        }

        if (labels[u] != mex) {

            labels[u] = mex;

            for (auto &v : adj[u]) {

                if (!in_queue_flag[v]) {

                    q.push(v);

                    in_queue_flag[v] = true;

                }

            }

        }

    }

    for (int i = 1; i <= n; ++i) {

        cout << labels[i] << (i < n ? " " : "\n");

    }

}

int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    int t;

    cin >> t;

    while (t--) {

        int n, x, y;

        cin >> n >> x >> y;

        solve(n, x, y);

    }

}