Problem: 2049C - MEX Cycle
Language: c++23 

Code 1:


#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

#define FASTIO() \

    ios_base::sync_with_stdio(false); \

    cin.tie(NULL);

using namespace std;



//<------------------------------------------------------------------------------------------------------------------------------------------------------------------------------>

//                                                            DEBUG TEMPLATE

void __print(int x) {cerr << x;}

void __print(long x) {cerr << x;}

void __print(long long x) {cerr << x;}

void __print(unsigned x) {cerr << x;}

void __print(unsigned long x) {cerr << x;}

void __print(unsigned long long x) {cerr << x;}

void __print(float x) {cerr << x;}

void __print(double x) {cerr << x;}

void __print(long double x) {cerr << x;}

void __print(char x) {cerr << '\'' << x << '\'';}

void __print(const char *x) {cerr << '\"' << x << '\"';}

void __print(const string &x) {cerr << '\"' << x << '\"';}

void __print(bool x) {cerr << (x ? "true" : "false");}



template<typename T, typename V>

void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}

template<typename T>

void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}

void _print() {cerr << "]\n";}

template <typename T, typename... V>

void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}



#ifndef ONLINE_JUDGE

#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)

#else

#define debug(x...)

#endif



// ordered set

// find_by_order, order_of_key

// order_of_key (k) : Number of items strictly smaller than k.

// find_by_order(k) : K-th element in a set (counting from zero)

using namespace __gnu_pbds;

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update> ;

template<class T> using ordered_multiset = tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;

template<class key, class value, class cmp = std::less<key>> using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;

struct pair_hash {

    template <class T1, class T2>

    size_t operator()(const std::pair<T1, T2>& p) const {

        auto h1 = std::hash<T1>{}(p.first);

        auto h2 = std::hash<T2>{}(p.second);

        return h1 ^ (h2 << 1);

    }

};

class DSU{

    public:

    vector<int> Rank, parent;

    void make(int n) {

        Rank.resize(n+1,0);

        parent.resize(n+1);

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

            parent[i] = i;

        }

    }

    int get(int node) {

        if (node == parent[node])

            return node;

        return parent[node] = get(parent[node]);

    }

    void unite(int u, int v) {

        int ulp_u = get(u);

        int ulp_v = get(v);

        if (ulp_u == ulp_v) return;

        if (Rank[ulp_u] < Rank[ulp_v]) {

            parent[ulp_u] = ulp_v;

        }

        else if (Rank[ulp_v] < Rank[ulp_u]) {

            parent[ulp_v] = ulp_u;

        }

        else {

            parent[ulp_v] = ulp_u;

            Rank[ulp_u]++;

        }

    }

};

//<----------------------------------------------------------------------------------------------------------------------------------------------------------------------------->





void getpath(int n, unordered_map<int,set<int>>&mp, vector<int>&path, vector<int>&visited){

    queue<pair<int,int>>q; // node, parent

    q.push({path[0], -1});

    visited[path[0]] = 1;

    while(!q.empty()){

        auto [node, parent] = q.front();

        q.pop();

        for(auto &neigh: mp[node]){

            if(!visited[neigh]){

                visited[neigh] = 1;

                q.push({neigh, node});

                path.push_back(neigh);

                break;

            }

            else{

                if(neigh == parent) continue;

                return;

            }

        }

    }

}



void solve(){

    unordered_map<int,set<int>>mp;

    int n, x, y;

    cin >> n >> x >> y;

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

        int f1 = i + 1, f2 = i - 1;

        if(f1 == n + 1) f1 = 1;

        if(f2 == 0) f2 = n;

        mp[i].insert(f1);

        mp[i].insert(f2);

    }

    mp[x].insert(y);mp[y].insert(x);

    bool exist = false;

    vector<int>path1, path2;

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

        if(mp[i].size() == 3){

            path1.push_back(i);

            path2.push_back(i);

            exist = true;

            break;

        }

    }

    if(!exist){

        if(n % 2 == 0){

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

                cout << (i % 2 == 1 ? 0 : 1) << " ";

            }

        }

        else{

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

                cout << i - 1 << " ";

            }

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

                cout << (i % 2 == 1) << " ";

            }

        }

        cout << "\n";

        return;

    }

    //debug(path1, path2);

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

    int endnode = -1;

    getpath(n, mp, path1, visited);

    int nc1 = path2[0] + 1, nc2 = path2[0] - 1;

    if(nc1 == n + 1) nc1 = 1;

    if(nc2 == 0) nc2 = n;

    if(!visited[nc2]){

        for(int i = n; i >= 1; --i){

            if(visited[i]) continue;

            else{

                path2.push_back(i);

            }

        }

    }

    else{

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

            if(visited[i]) continue;

            else{

                path2.push_back(i);

            }

        }

    }

    //debug(path1);

    path2.push_back(path1.back());

    //debug(path2);

    vector<int>ans(n + 1, -1);

    if(path1.size() % 2 == 1 and path2.size() % 2 == 1){

        ans[path1[0]] = 2;

        int i = 1;

        while(i < path1.size() or i < path2.size()){

            if(i < path1.size()){

                ans[path1[i]] = i % 2;

            }

            if(i < path2.size()){

                ans[path2[i]] = i % 2;

            }

            ++i;

        }

    }



    else if(path1.size() % 2 == 1){

        ans[path1[0]] = 0;

        ans[path1[1]] = 2;

        ans[path1[2]] = 1;

        for(int i = 3; i < path1.size(); ++i){

            if(i & 1) ans[path1[i]] = 0;

            else ans[path1[i]] = 1;

        }

        for(int i = 0; i < path2.size(); ++i){

            if(ans[path2[i]] != -1) continue;

            //debug(path2[i]);

            ans[path2[i]] = i % 2;

        }

    }

    else if(path2.size() % 2 == 1){

        ans[path2[0]] = 0;

        ans[path2[1]] = 2;

        ans[path2[2]] = 1;

        for(int i = 3; i < path2.size(); ++i){

            if(i & 1) ans[path2[i]] = 0;

            else ans[path2[i]] = 1;

        }

        for(int i = 0; i < path1.size(); ++i){

            if(ans[path1[i]] != -1) continue;

            ans[path1[i]] = i % 2;

        }

    }

    else{

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

            ans[i] = (i % 2 == 0);

        }

    }

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

        cout << ans[i] << " ";

    }

    cout << "\n";





}



int main() {

    FASTIO();

    int t; t = 1;

    cin >> t;

    while(t--){

        solve();

    }



}



Code 2:


#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

#define FASTIO() \

    ios_base::sync_with_stdio(false); \

    cin.tie(NULL);

using namespace std;



//<------------------------------------------------------------------------------------------------------------------------------------------------------------------------------>

//                                                            DEBUG TEMPLATE

void __print(int x) {cerr << x;}

void __print(long x) {cerr << x;}

void __print(long long x) {cerr << x;}

void __print(unsigned x) {cerr << x;}

void __print(unsigned long x) {cerr << x;}

void __print(unsigned long long x) {cerr << x;}

void __print(float x) {cerr << x;}

void __print(double x) {cerr << x;}

void __print(long double x) {cerr << x;}

void __print(char x) {cerr << '\'' << x << '\'';}

void __print(const char *x) {cerr << '\"' << x << '\"';}

void __print(const string &x) {cerr << '\"' << x << '\"';}

void __print(bool x) {cerr << (x ? "true" : "false");}



template<typename T, typename V>

void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}

template<typename T>

void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}

void _print() {cerr << "]\n";}

template <typename T, typename... V>

void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}



#ifndef ONLINE_JUDGE

#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)

#else

#define debug(x...)

#endif



// ordered set

// find_by_order, order_of_key

// order_of_key (k) : Number of items strictly smaller than k.

// find_by_order(k) : K-th element in a set (counting from zero)

using namespace __gnu_pbds;

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update> ;

template<class T> using ordered_multiset = tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;

template<class key, class value, class cmp = std::less<key>> using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;

struct pair_hash {

    template <class T1, class T2>

    size_t operator()(const std::pair<T1, T2>& p) const {

        auto h1 = std::hash<T1>{}(p.first);

        auto h2 = std::hash<T2>{}(p.second);

        return h1 ^ (h2 << 1);

    }

};

class DSU{

    public:

    vector<int> Rank, parent;

    void make(int n) {

        Rank.resize(n+1,0);

        parent.resize(n+1);

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

            parent[i] = i;

        }

    }

    int get(int node) {

        if (node == parent[node])

            return node;

        return parent[node] = get(parent[node]);

    }

    void unite(int u, int v) {

        int ulp_u = get(u);

        int ulp_v = get(v);

        if (ulp_u == ulp_v) return;

        if (Rank[ulp_u] < Rank[ulp_v]) {

            parent[ulp_u] = ulp_v;

        }

        else if (Rank[ulp_v] < Rank[ulp_u]) {

            parent[ulp_v] = ulp_u;

        }

        else {

            parent[ulp_v] = ulp_u;

            Rank[ulp_u]++;

        }

    }

};

//<----------------------------------------------------------------------------------------------------------------------------------------------------------------------------->





void getpath(int n, unordered_map<int,set<int>>&mp, vector<int>&path, vector<int>&visited){

    queue<pair<int,int>>q; // node, parent

    q.push({path[0], -1});

    visited[path[0]] = 1;

    while(!q.empty()){

        auto [node, parent] = q.front();

        q.pop();

        for(auto &neigh: mp[node]){

            if(!visited[neigh]){

                visited[neigh] = 1;

                q.push({neigh, node});

                path.push_back(neigh);

                break;

            }

            else{

                if(neigh == parent) continue;

                return;

            }

        }

    }

}



void solve(){

    unordered_map<int,set<int>>mp;

    int n, x, y;

    cin >> n >> x >> y;

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

        int f1 = i + 1, f2 = i - 1;

        if(f1 == n + 1) f1 = 1;

        if(f2 == 0) f2 = n;

        mp[i].insert(f1);

        mp[i].insert(f2);

    }

    mp[x].insert(y);mp[y].insert(x);

    bool exist = false;

    vector<int>path1, path2;

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

        if(mp[i].size() == 3){

            path1.push_back(i);

            path2.push_back(i);

            exist = true;

            break;

        }

    }

    if(!exist){

        if(n % 2 == 0){

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

                cout << (i % 2 == 1 ? 0 : 1);

            }

        }

        else{

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

                cout << i - 1 << " ";

            }

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

                cout << (i % 2 == 0) << " ";

            }

        }

        cout << "\n";

        return;

    }

    //debug(path1, path2);

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

    int endnode = -1;

    getpath(n, mp, path1, visited);

    int nc1 = path2[0] + 1, nc2 = path2[0] - 1;

    if(nc1 == n + 1) nc1 = 1;

    if(nc2 == 0) nc2 = n;

    if(!visited[nc2]){

        for(int i = n; i >= 1; --i){

            if(visited[i]) continue;

            else{

                path2.push_back(i);

            }

        }

    }

    else{

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

            if(visited[i]) continue;

            else{

                path2.push_back(i);

            }

        }

    }

    //debug(path1);

    path2.push_back(path1.back());

    //debug(path2);

    vector<int>ans(n + 1, -1);

    if(path1.size() % 2 == 1 and path2.size() % 2 == 1){

        ans[path1[0]] = 2;

        int i = 1;

        while(i < path1.size() or i < path2.size()){

            if(i < path1.size()){

                ans[path1[i]] = i % 2;

            }

            if(i < path2.size()){

                ans[path2[i]] = i % 2;

            }

            ++i;

        }

    }



    else if(path1.size() % 2 == 1){

        ans[path1[0]] = 0;

        ans[path1[1]] = 2;

        ans[path1[2]] = 1;

        for(int i = 3; i < path1.size(); ++i){

            if(i & 1) ans[path1[i]] = 0;

            else ans[path1[i]] = 1;

        }

        for(int i = 0; i < path2.size(); ++i){

            if(ans[path2[i]] != -1) continue;

            //debug(path2[i]);

            ans[path2[i]] = i % 2;

        }

    }

    else if(path2.size() % 2 == 1){

        ans[path2[0]] = 0;

        ans[path2[1]] = 2;

        ans[path2[2]] = 1;

        for(int i = 3; i < path2.size(); ++i){

            if(i & 1) ans[path2[i]] = 0;

            else ans[path2[i]] = 1;

        }

        for(int i = 0; i < path1.size(); ++i){

            if(ans[path1[i]] != -1) continue;

            ans[path1[i]] = i % 2;

        }

    }

    else{

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

            ans[i] = (i % 2 == 0);

        }

    }

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

        cout << ans[i] << " ";

    }

    cout << "\n";





}



int main() {

    FASTIO();

    int t; t = 1;

    cin >> t;

    while(t--){

        solve();

    }



}



Answer: 1