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 == 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 = 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: Wrong answer on test 1):
#include <bits/stdc++.h>

using namespace std;



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

    // 1-based indexing

    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);

    }



    // Check if x and y are adjacent

    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);

    }



    // Initialize labels

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

    // Initialize queue

    queue<int> q;

    // To optimize, enqueue all nodes initially

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

        q.push(i);

    }

    // Keep track of nodes in queue

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



    while (!q.empty()) {

        int u = q.front();

        q.pop();

        in_queue[u] = false;

        // Compute mex of neighbors

        int mask = 0;

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

            if (labels[v] < 32) {

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

            }

        }

        // Find mex

        int mex = 0;

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

            ++mex;

        }

        if (labels[u] != mex) {

            labels[u] = mex;

            // Enqueue all neighbors

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

                if (!in_queue[v]) {

                    q.push(v);

                    in_queue[v] = true;

                }

            }

        }

    }



    // Output the labels

    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;

}