1255C - League of Leesins - CodeForces Solution


constructive algorithms implementation *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#define ar array
#define ll long long
typedef int uci;
#define int long long
#define F first
#define S second
typedef complex<double> cd;

seed_seq seq{
    (uint64_t) chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count(),
    (uint64_t) __builtin_ia32_rdtsc(),
    (uint64_t) (uintptr_t) make_unique<char>().get()
};
mt19937 rng(seq);
// mt19937_64 lrng(seq);

struct debugger{
    template <typename T>
    debugger& operator<<(T &a){
        #ifdef DEBUG
            cerr << a;
        #endif
        return *this;
    }
    template <typename T>
    debugger& operator<<(T &&a){
        #ifdef DEBUG
            cerr << a;
        #endif
        return *this;
    }
} deb;

const double PI = acos(-1.0);
const int MAX_N = 1e5 + 1;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LINF = 1e18;

//! function insert

//THINK FIRST, CODE SECOND
//DON'T GET STUCK ON ONE STRATEGY
//CALM DOWNNN FOR ONCE IN YOUR LIFE
//REDUCE
//COUGH E??!?!?!! O.O
//uwu i see you ryan

ar<int, 2> getothers(int no, ar<int, 3> a){
    if(a[0] == no)
        return {a[1], a[2]};
    else if(a[1] == no)
        return {a[0], a[2]};
    else
        return {a[0], a[1]};
}

int getlast(int no, int no2, ar<int, 3> a){
    for(int i{}; i < 3; ++i)
        if(a[i] != no && a[i] != no2)
            return a[i];
    return a[0];
}

int notused(vector<int> &a, vector<bool> &used){
    for(int i : a)
        if(!used[i])
            return i;
    return -1;
}

void solve() {
    int n;
    cin >> n;
    vector<ar<int, 3>> a(n-2);
    for(int i{}; i < n-2; ++i){
        cin >> a[i][0] >> a[i][1] >> a[i][2];
        sort(a[i].begin(), a[i].end());
    }

    vector<int> counts(n+1);
    for(auto &t : a){
        for(int i : t)
            counts[i]++;
    }
    vector<vector<int>> inside(n+1);
    for(int j{}; j < n-2; ++j){
        for(int i : a[j])
            inside[i].push_back(j);
    }

    map<ar<int, 2>, vector<int>> inds;
    for(int i{}; i < n-2; ++i){
        inds[{a[i][0], a[i][1]}].push_back(i); 
        inds[{a[i][0], a[i][2]}].push_back(i); 
        inds[{a[i][1], a[i][2]}].push_back(i); 
    }

    vector<int> out;
    vector<bool> used(n);
    for(int i{1}; i <= n; ++i){
        if(counts[i] == 1){
            out.push_back(i);
            int t = inside[i][0];
            used[t] = true;
            ar<int, 2> others = getothers(i, a[t]);
            if(counts[others[0]] == 2){
                out.push_back(others[0]);
                out.push_back(others[1]);
            }else{
                out.push_back(others[1]);
                out.push_back(others[0]);
                swap(others[0], others[1]);
            }

            while(out.size() < n){
                int next = notused(inds[{min(others[0], others[1]), max(others[0], others[1])}], used);
                used[next] = true;
                int tt = getlast(others[0], others[1], a[next]);
                out.push_back(tt);
                swap(others[0], others[1]);
                others[1] = tt;
            }
            break;
        }
    }
    for(int i : out)
        cout << i << ' ';
    cout << '\n';
}

uci main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

        // cout << "Case #" << t  << ": ";
        solve();
}

/*
    random number generator stuff
    num = rng(); gives integer number
    num = uniform_int_distribution<int>(a, b)(rng); -> bounds [a, b]
    num = uniform_real_distribution<double>(a, b)(rng); -> bounds [a, b)
    can also instantiate distributions and call on generator:
    uniform_int_distribution<int> thing(a, b);
    num = thing(rng);
*/
// struct custom_hash {
//     static uint64_t splitmix64(uint64_t x) {
//         // http://xorshift.di.unimi.it/splitmix64.c
//         x += 0x9e3779b97f4a7c15;
//         x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
//         x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
//         return x ^ (x >> 31);
//     }
//     size_t operator()(uint64_t x) const {
//         static const uint64_t FIXED_RANDOM = lrng();
//         return splitmix64(x + FIXED_RANDOM);
//     }
// };


Comments

Submit
0 Comments
More Questions

1091A - New Year and the Christmas Ornament
1352B - Same Parity Summands
1102A - Integer Sequence Dividing
630B - Moore's Law
1004A - Sonya and Hotels
1680B - Robots
1690A - Print a Pedestal (Codeforces logo)
1295A - Display The Number
1077A - Frog Jumping
1714G - Path Prefixes
1369C - RationalLee
289B - Polo the Penguin and Matrix
1716A - 2-3 Moves
1670B - Dorms War
1716B - Permutation Chain
987A - Infinity Gauntlet
1676G - White-Black Balanced Subtrees
1716D - Chip Move
1352F - Binary String Reconstruction
1487B - Cat Cycle
1679C - Rooks Defenders
56A - Bar
1694B - Paranoid String
35A - Shell Game
1684A - Digit Minimization
43B - Letter
1017A - The Rank
1698B - Rising Sand
235A - LCM Challenge
1075B - Taxi drivers and Lyft