Submission #1229124

#TimeUsernameProblemLanguageResultExecution timeMemory
1229124LudisseyFountain Parks (IOI21_parks)C++20
15 / 100
320 ms29536 KiB
#include "parks.h"
#include <bits/stdc++.h>
 
using namespace std;
 
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()

vector<int> sz,parent;

int getParent(int a){
    if(parent[a]==a) return a;
    return parent[a]=getParent(parent[a]);
}

bool same(int a, int b){
    a=getParent(a);
    b=getParent(b);
    return (a==b);
}

void unite(int a, int b){
    a=getParent(a);
    b=getParent(b);
    if(a==b) return;
    if(sz[a]<sz[b]){
        swap(a,b);
    } 
    sz[a]+=sz[b];
    parent[b]=a;
}

int construct_roads(std::vector<int> x, std::vector<int> y) {
    int n=sz(x);
    vector<pair<pair<int,int>,int>> f(n);
    sz.resize(n,1);
    parent.resize(n);
    for (int i = 0; i < n; i++) parent[i]=i;

    for (int i = 0; i < n; i++) f[i]={{x[i],y[i]},i};
    map<pair<int,int>, int> mp;
    for (int i = 0; i < n; i++) mp[{x[i],y[i]}]=i;
    std::vector<int> u, v, a, b;
    sort(all(f));
    for (int i = 0; i < n; i++)
    {
        pair<int,int> und={f[i].first.first,f[i].first.second+2};
        if(mp.find(und)!=mp.end()){
            if(!same(f[i].second,mp[und])){
                unite(f[i].second,mp[und]);
                u.push_back(f[i].second);
                v.push_back(mp[und]);
                if(f[i].first.first==2){
                    a.push_back(f[i].first.first-1);
                    b.push_back(f[i].first.second+1);
                }else{
                    a.push_back(f[i].first.first+1);
                    b.push_back(f[i].first.second+1);
                }
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        pair<int,int> rgt={f[i].first.first+2,f[i].first.second};
        if(mp.find(rgt)!=mp.end()){
            if(!same(f[i].second,mp[rgt])){
                unite(f[i].second,mp[rgt]);
                u.push_back(f[i].second);
                v.push_back(mp[rgt]);
                a.push_back(f[i].first.first+1);
                b.push_back(f[i].first.second-1);
            }
        }
    }
    if (sz[getParent(0)] != n) {
        return 0;
    }
    build(u, v, a, b);
    return 1;
}
#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...