Submission #1227728

#TimeUsernameProblemLanguageResultExecution timeMemory
1227728steveonalexFountain Parks (IOI21_parks)C++20
100 / 100
414 ms40248 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(abs(a), abs(b));}
ll lcm(ll a, ll b){return abs(a) / gcd(a, b) * abs(b);}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }

template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

#include "parks.h"

ostream& operator << (ostream &os, pair<int, int> x){
    return os << "(" << x.first << ", " << x.second << ")";
}

struct DSU{
    int n;
    vector<int> parent, sz;

    DSU(int _n){
        n = _n;
        parent.resize(n); sz.resize(n, 1);
        for(int i = 0; i<n; ++i) parent[i] = i;
    }

    int find_set(int u){return (u == parent[u]) ? u : (parent[u] = find_set(parent[u]));}
    bool same_set(int u, int v){return find_set(u) == find_set(v);}

    bool join_set(int u, int v){
        u = find_set(u), v = find_set(v);
        if (u != v){
            if (sz[u] < sz[v]) swap(u, v);
            parent[v] = u;
            sz[u] += sz[v];
            return true;
        }
        return false;
    }

    int get_size(int u){return sz[find_set(u)];}
};



int construct_roads(vector<int> x, vector<int> y){
    vector<pair<int, int>> pos;
    int n = x.size();
    for(int i = 0; i < n; ++i) pos.push_back(make_pair(x[i], y[i]));

    map<pair<int, int>, int> mp;
    for(int i = 0; i < n; ++i) 
        mp[pos[i]] = i;

    sort(ALL(pos), [](pair<int, int> x, pair<int,int> y){
        return x.first + x.second < y.first + y.second;
    });

    DSU mst(n);

    vector<int> u, v, a, b;
    set<pair<int, int>> S;
    for(pair<int, int> i: pos){
        int idx1 = mp[i];
        pair<int, int> left = make_pair(i.first - 2, i.second);
        pair<int, int> down = make_pair(i.first, i.second - 2);

        // cout << i << " " << left << " " << down << "\n";

        if (mp.count(left)){
            int idx2 = mp[left];
            pair<int, int> garbagol = make_pair(i.first - 1, i.second);
            if ((i.first + i.second) % 4 == 0) garbagol.second++;
            else garbagol.second--;

            // cout << "Left: " << garbagol << "\n";
            if (S.find(garbagol) == S.end()){
                if (mst.join_set(idx1, idx2)) {
                    S.insert(garbagol);
                    u.push_back(idx1); v.push_back(idx2);
                    a.push_back(garbagol.first); b.push_back(garbagol.second);
                }
            }
        }
        if (mp.count(down)){
            int idx3 = mp[down];
            pair<int, int> garbagol = make_pair(i.first, i.second - 1);
            if ((i.first + i.second) % 4 == 2) garbagol.first++;
            else garbagol.first--;
            // cout << "Down: " << garbagol << "\n";
            if (S.find(garbagol) == S.end()){
                if (mst.join_set(idx1, idx3)) {
                    S.insert(garbagol);
                    u.push_back(idx1); v.push_back(idx3);
                    a.push_back(garbagol.first); b.push_back(garbagol.second);
                }
            }
        }
    }

    if (mst.get_size(0) == n){
        build(u, v, a, b);
        return 1;
    }
    else return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...