답안 #435516

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
435516 2021-06-23T11:51:18 Z Ozy 분수 공원 (IOI21_parks) C++17
45 / 100
628 ms 58976 KB
#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}; // 0-NORTE, 1-OESTE, 2-SUR, 3-ESTE
int dy[4] = {2, 0, -2, 0};
#define norte 0
#define oeste 1
#define sur 2
#define este 3

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

int grupo[MAX_N], gfinal;
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, vector<int> &x, vector<int> &y){

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

                int bx, by; // COORDENADAS DE LA BANCA
                if (x[nodo] == x[h]){ // ES UNA UNION VERTICAL
                    by = (y[nodo] + y[h]) / 2; // LA COORDENADA VERTICAL DE LA BANCA ES EL PROMEDIO DE LAS DOS FUENTES
                    if ((x[nodo] + y[nodo]) & 3){ // EL &3 EQUIVALE A %4 Y ES PARA VER SI LA FUENTE QUE ESTAMOS REVISANDO SE ENCUENTRA EN UNA DIAGONAL "PAR" O "IMPAR"
                                                  // PARA DETERMINAR SI ENTRE ESA DIAGONAL Y LA QUE SIGUE LAS BANCAS VAN A LA IZQUIERDA O A LA DERECHA
                        if (i == norte) bx = x[nodo] - 1; // SI EL CAMINO ES AL NORTE, LA FUENTE A LA IZQUIERDA
                        else bx = x[nodo] + 1;  // SI EL CAMINO ES AL SUR, LA FUENTE A LA DERECHA
                    }
                    else{
                        // AQUI ES LA OTRA DIAGONAL DE MODO QUE LAS BANCAS VAN ALTERNADAS.
                        if (i == norte) bx = x[nodo] + 1;
                        else bx = x[nodo] - 1;
                    }
                }
                else{ // ES UNA UNION HORIZONTAL
                    bx = (x[nodo] + x[h]) / 2;
                    if ((x[nodo] + y[nodo]) & 3){
                        if (i == 1) by = y[nodo] - 1;
                        else by = y[nodo] + 1;
                    }
                    else{
                        if (i == 1) by = y[nodo] + 1;
                        else by = y[nodo] - 1;
                    }
                }

                a.push_back(bx);
                b.push_back(by);

                dfs(h, nodo, 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,
    // SE VAN A AGREGAR CAMINOS UNICAMENTE SI SON NECESARIOS PARA QUE EL GRAFO SEA CONEXO (SE VALIDA MEDIANTE UN DSU)
    // PARA ACOMODAR LAS BANCAS DE FORMA QUE NO CHOQUEN SE UTILIZAN LAS DIAGONALES, EN ESTE CASO SE USAN LAS DE TIPO (x + y),
    // LA IDEA ES QUE TODOS LOS CAMINOS VERTICALES QUE QUEDEN ENTRE UN PAR DE DIAGONALES TENGAN LA BANCA DEL MISMO LADO, LO MISMO CON LOS HORIZONTALES.
    // ADEMAS, LOS CAMINOS ENTRE UN PAR DE DIAGONALES Y EL PAR DE DIAGONALES INMEDIATAMENTE SIGUIENTE ALTERNAN SU LADO.
    //     H
    //   \---\---\   \   \      LAS LETRAS H CORRESPONDEN A LA BANCA DEL CAMINO HORIZONTAL
    //    \  |\h |\   \   \     LAS LETRAS V CORRESPONDEN A LA BANCA DEL CAMINO VERTICAL
    //     \V| \ |V\   \   \    LAS LETRAS h EN EL DIBUJO SON BANCAS QUE COLISIONAN
    //      \| h\|  \   \   \
    //       \---\---\   \   \
    //             H
    // LAS BANCAS ENTRE UN PAR DE DIAGONALES NUNCA VAN A COLISIONAR.
    // LA UNICA FORMA DE COLISIONAR ES DOS CAMINOS HORIZONTALES O VERTICALES ENCONTRADOS EN PARES DE DIAGONALES CONSECUTIVOS.
    // PARA QUE ESTO SEA POSIBLE ES FORZOSO QUE HAYA UN CUADRO DE 2x2 CON FUENTES EN LAS 4 ESQUINAS, LO CUAL NO SUCEDE EN LOS SUBTASKS 4 y 5
    // POR LO QUE ESTE CODIGO RESUELVE ESOS SUBTASKS

    rep(i, 0, x.size() - 1) grupo[i] = i; // INICIALIZA EL DSU
    dfs(0, -1, 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

parks.cpp:106:5: warning: multi-line comment [-Wcomment]
  106 |     //      \| h\|  \   \   \
      |     ^
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:95:5: note: in expansion of macro 'rep'
   95 |     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:114:5: note: in expansion of macro 'rep'
  114 |     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:120:5: note: in expansion of macro 'rep'
  120 |     rep(i, 1, x.size() - 1) if (comp(i) != gfinal){
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 198 ms 29668 KB Output is correct
10 Correct 17 ms 7756 KB Output is correct
11 Correct 86 ms 18824 KB Output is correct
12 Correct 26 ms 8972 KB Output is correct
13 Correct 54 ms 13164 KB Output is correct
14 Correct 4 ms 5068 KB Output is correct
15 Correct 5 ms 5196 KB Output is correct
16 Correct 213 ms 24420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 198 ms 29668 KB Output is correct
10 Correct 17 ms 7756 KB Output is correct
11 Correct 86 ms 18824 KB Output is correct
12 Correct 26 ms 8972 KB Output is correct
13 Correct 54 ms 13164 KB Output is correct
14 Correct 4 ms 5068 KB Output is correct
15 Correct 5 ms 5196 KB Output is correct
16 Correct 213 ms 24420 KB Output is correct
17 Correct 4 ms 4940 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Incorrect 3 ms 4940 KB Tree @(3, 3) appears more than once: for edges on positions 2 and 4
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 198 ms 29668 KB Output is correct
10 Correct 17 ms 7756 KB Output is correct
11 Correct 86 ms 18824 KB Output is correct
12 Correct 26 ms 8972 KB Output is correct
13 Correct 54 ms 13164 KB Output is correct
14 Correct 4 ms 5068 KB Output is correct
15 Correct 5 ms 5196 KB Output is correct
16 Correct 213 ms 24420 KB Output is correct
17 Correct 4 ms 4940 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Incorrect 3 ms 4940 KB Tree @(3, 3) appears more than once: for edges on positions 2 and 4
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 198 ms 29668 KB Output is correct
10 Correct 17 ms 7756 KB Output is correct
11 Correct 86 ms 18824 KB Output is correct
12 Correct 26 ms 8972 KB Output is correct
13 Correct 54 ms 13164 KB Output is correct
14 Correct 4 ms 5068 KB Output is correct
15 Correct 5 ms 5196 KB Output is correct
16 Correct 213 ms 24420 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 4 ms 4940 KB Output is correct
20 Correct 531 ms 55932 KB Output is correct
21 Correct 518 ms 50904 KB Output is correct
22 Correct 496 ms 48060 KB Output is correct
23 Correct 412 ms 40704 KB Output is correct
24 Correct 224 ms 21340 KB Output is correct
25 Correct 183 ms 21412 KB Output is correct
26 Correct 191 ms 21412 KB Output is correct
27 Correct 405 ms 31232 KB Output is correct
28 Correct 420 ms 31204 KB Output is correct
29 Correct 508 ms 31104 KB Output is correct
30 Correct 561 ms 31048 KB Output is correct
31 Correct 4 ms 4940 KB Output is correct
32 Correct 28 ms 7924 KB Output is correct
33 Correct 72 ms 13204 KB Output is correct
34 Correct 461 ms 51852 KB Output is correct
35 Correct 12 ms 5964 KB Output is correct
36 Correct 44 ms 9352 KB Output is correct
37 Correct 85 ms 13460 KB Output is correct
38 Correct 157 ms 15684 KB Output is correct
39 Correct 208 ms 19556 KB Output is correct
40 Correct 290 ms 23584 KB Output is correct
41 Correct 389 ms 27640 KB Output is correct
42 Correct 459 ms 31608 KB Output is correct
43 Correct 4 ms 4940 KB Output is correct
44 Correct 4 ms 4940 KB Output is correct
45 Correct 3 ms 4940 KB Output is correct
46 Correct 3 ms 4968 KB Output is correct
47 Correct 3 ms 4940 KB Output is correct
48 Correct 4 ms 4940 KB Output is correct
49 Correct 4 ms 4940 KB Output is correct
50 Correct 4 ms 4940 KB Output is correct
51 Correct 4 ms 4940 KB Output is correct
52 Correct 3 ms 4940 KB Output is correct
53 Correct 3 ms 4940 KB Output is correct
54 Correct 5 ms 5196 KB Output is correct
55 Correct 5 ms 5196 KB Output is correct
56 Correct 186 ms 25712 KB Output is correct
57 Correct 285 ms 35772 KB Output is correct
58 Correct 292 ms 35044 KB Output is correct
59 Correct 3 ms 4940 KB Output is correct
60 Correct 4 ms 4940 KB Output is correct
61 Correct 3 ms 4940 KB Output is correct
62 Correct 416 ms 48792 KB Output is correct
63 Correct 401 ms 49180 KB Output is correct
64 Correct 395 ms 51712 KB Output is correct
65 Correct 6 ms 5452 KB Output is correct
66 Correct 8 ms 5788 KB Output is correct
67 Correct 178 ms 22188 KB Output is correct
68 Correct 315 ms 33820 KB Output is correct
69 Correct 423 ms 39488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 198 ms 29668 KB Output is correct
10 Correct 17 ms 7756 KB Output is correct
11 Correct 86 ms 18824 KB Output is correct
12 Correct 26 ms 8972 KB Output is correct
13 Correct 54 ms 13164 KB Output is correct
14 Correct 4 ms 5068 KB Output is correct
15 Correct 5 ms 5196 KB Output is correct
16 Correct 213 ms 24420 KB Output is correct
17 Correct 450 ms 58640 KB Output is correct
18 Correct 486 ms 55036 KB Output is correct
19 Correct 628 ms 58976 KB Output is correct
20 Correct 495 ms 46940 KB Output is correct
21 Correct 429 ms 39916 KB Output is correct
22 Correct 4 ms 4940 KB Output is correct
23 Correct 58 ms 10572 KB Output is correct
24 Correct 22 ms 7492 KB Output is correct
25 Correct 58 ms 11844 KB Output is correct
26 Correct 111 ms 15940 KB Output is correct
27 Correct 234 ms 25196 KB Output is correct
28 Correct 298 ms 31016 KB Output is correct
29 Correct 397 ms 36124 KB Output is correct
30 Correct 434 ms 40592 KB Output is correct
31 Correct 487 ms 46396 KB Output is correct
32 Correct 452 ms 51016 KB Output is correct
33 Correct 455 ms 58556 KB Output is correct
34 Correct 7 ms 5580 KB Output is correct
35 Correct 11 ms 5956 KB Output is correct
36 Correct 181 ms 23036 KB Output is correct
37 Correct 317 ms 32748 KB Output is correct
38 Correct 435 ms 41884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 198 ms 29668 KB Output is correct
10 Correct 17 ms 7756 KB Output is correct
11 Correct 86 ms 18824 KB Output is correct
12 Correct 26 ms 8972 KB Output is correct
13 Correct 54 ms 13164 KB Output is correct
14 Correct 4 ms 5068 KB Output is correct
15 Correct 5 ms 5196 KB Output is correct
16 Correct 213 ms 24420 KB Output is correct
17 Correct 4 ms 4940 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Incorrect 3 ms 4940 KB Tree @(3, 3) appears more than once: for edges on positions 2 and 4
20 Halted 0 ms 0 KB -