Submission #435069

#TimeUsernameProblemLanguageResultExecution timeMemory
435069ocarimaFountain Parks (IOI21_parks)C++17
5 / 100
640 ms77504 KiB
#include "parks.h"
#include <bits/stdc++.h>

using namespace std;

#define lli long long
#define rep(i, a, b) for(lli i = (a); i <= (b); ++i)
#define repa(i, a, b) for(lli i = (a); i >= (b); --i)

#define nl "\n"
#define debugsl(x) cout << #x << " = " << x << ", "
#define debug(x) debugsl(x) << nl
#define debugarr(x, a, b) cout << #x << " = ["; rep(ii, a, b) cout << x[ii] << ", "; cout << "]" << nl

#define MAX_N 200002

int dx[4] = {0, -2, 0, 2}; // USARE COMO KAREL 0-NORTE, 1-OESTE, 2-SUR, 3-ESTE
int dy[4] = {2, 0, -2, 0};

map<pair<int, int>, int> fuentes;
vector<int> hijos[MAX_N];

int grupo[MAX_N], gfinal;
map<pair<int, int>, int> bancas;
std::vector<int> u, v, a, b;

int comp(int a){
    if (grupo[a] == a) return a;
    else return grupo[a] = comp(grupo[a]);
}

bool une(int a, int b){
    int ga = comp(a);
    int gb = comp(b);
    if (ga == gb) return false;
    grupo[ga] = gb;
    return true;
}

void dfs(int nodo, int padre, int direccion, vector<int> &x, vector<int> &y){

    rep(i, 0, 3){
        if (fuentes.find({x[nodo] + dx[(direccion + i) & 3], y[nodo] + dy[(direccion + i) & 3]}) != fuentes.end()){ // EXISTE FUENTE EN ESA DIRECCION
            int h = fuentes[{x[nodo] + dx[(direccion + i) & 3], y[nodo] + dy[(direccion + i) & 3]}];
            if (une(nodo, h)){
                // ESTOS DOS NO ESTABAN UNIDOS, ES UN NUEVO CAMINO.
                u.push_back(nodo);
                v.push_back(h);

                int bx, by; // LAS COORDENADAS DE LA BANCA
                if (x[nodo] == x[h]){ // ES UNA UNION VERTICAL
                    by = (y[nodo] > y[h]) ? y[h] + 1 : y[h] - 1;
                    bx = x[h] + 1;
                    if (bancas.find({bx, by}) != bancas.end()) bx = x[h] - 1; // SI ESE YA EXISTE, USA LA OTRA OPCION
                }
                else{ // ES UNA UNION HORIZONTAL
                    bx = (x[nodo] > x[h]) ? x[h] + 1 : x[h] - 1;
                    by = y[h] + 1;
                    if (bancas.find({bx, by}) != bancas.end()) by = y[h] - 1; // SI YA EXISTE, USA LA OTRA OPCION
                }

                a.push_back(bx);
                b.push_back(by);
                bancas[{bx, by}] = 1;

                int dirhijo = (direccion + i + 2) & 3; // Norte se vuelve sur, este oeste, etc.
                dfs(h, nodo, dirhijo, x, y);
            }
        }
    }
}

int construct_roads(std::vector<int> x, std::vector<int> y) {
    if (x.size() == 1) {
	build({}, {}, {}, {});
        return 1;
    }

    // AGREGA LAS FUENTES A UN MAP PARA PODER BUSCARLAS FACILMENTE
    rep(i, 0, x.size() - 1) fuentes[{x[i], y[i]}] = i;

    // AHORA HAZ UNA BUSQUEDA EN PROFUNDIDAD DESDE CUALQUIER FUENTE, AGREGANDO UNICAMENTE CAMINOS SI SON NECESARIOS USANDO UN UNION FIND Y ACOMODANDO LAS BANCAS DE FORMA GREEDY
    rep(i, 0, x.size() - 1) grupo[i] = i; // INICIALIZA EL DSU
    dfs(0, -1, 0, x, y);

    // VERIFICA SI TODOS QUEDARON UNIDOS EN EL MISMO COMPONENTE
    bool ok = true;
    gfinal = comp(0);
    rep(i, 1, x.size() - 1) if (comp(i) != gfinal){
        ok = false;
        break;
    }

    if (!ok) return 0;

    build(u, v, a, b);
    return 1;
}

Compilation message (stderr)

parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:7:41: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define rep(i, a, b) for(lli i = (a); i <= (b); ++i)
      |                                         ^
parks.cpp:80:5: note: in expansion of macro 'rep'
   80 |     rep(i, 0, x.size() - 1) fuentes[{x[i], y[i]}] = i;
      |     ^~~
parks.cpp:7:41: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define rep(i, a, b) for(lli i = (a); i <= (b); ++i)
      |                                         ^
parks.cpp:83:5: note: in expansion of macro 'rep'
   83 |     rep(i, 0, x.size() - 1) grupo[i] = i; // INICIALIZA EL DSU
      |     ^~~
parks.cpp:7:41: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define rep(i, a, b) for(lli i = (a); i <= (b); ++i)
      |                                         ^
parks.cpp:89:5: note: in expansion of macro 'rep'
   89 |     rep(i, 1, x.size() - 1) if (comp(i) != gfinal){
      |     ^~~
#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...