답안 #677953

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
677953 2023-01-04T17:55:09 Z APROHACK 분수 공원 (IOI21_parks) C++17
15 / 100
530 ms 46280 KB
#include "parks.h"
#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
using namespace std;
int N;
vector<pair<int, int > >node;
map<pair<int, int>, int>mp;
vector<int>u, v, a, b;
int padre[200005];

int findPadre(int k){
    if(padre[k] == k)return k;
    return padre[k] = findPadre(padre[k]);
}

void unir(int p, int q){
    p = findPadre(p);
    q = findPadre(q);
    if(p == q){
        return;
    }
    padre[p] = q;
}

int construct_roads(std::vector<int> x, std::vector<int> y) {
    if (x.size() == 1) {
	build({}, {}, {}, {});
        return 1;
    }
    N = x.size();
    for(int i = 0 ; i < N ; i ++){
        node.pb({x[i], y[i]});
        mp[{x[i], y[i]}] = i;
        padre[i] = i;
    }

    queue<int>fila;
    fila.push(0);
    int current;
    while(!fila.empty()){
        current = fila.front();
        fila.pop();
        pair<int, int > derecha = {node[current].ff+2, node[current].ss}, izquierda = {node[current].ff-2, node[current].ss}, abajo = {node[current].ff, node[current].ss-2}, arriba = {node[current].ff, node[current].ss+2};
        vector<pair<int, int> >porRecorrer;
        
        porRecorrer.pb(izquierda);
        porRecorrer.pb(arriba);
        porRecorrer.pb(derecha);
        porRecorrer.pb(abajo);
        for(pair<int, int> siguiente : porRecorrer){
            if(!mp.count(siguiente))continue;
            int indxnxt = mp[siguiente];
            if(findPadre(indxnxt) == findPadre(0))continue; //(No va a pasar)
            unir(indxnxt, current);
            u.pb(current);
            v.pb(indxnxt);
            //cout << "se une " << current <<" con " << indxnxt << endl;
            fila.push(indxnxt);
        }
    }

    for(int i =0  ; i < N ; i ++){
        if(findPadre(i) != findPadre(0)){
            //cout << "padre de " <<i  << "!= padre de " << 0 << "(" << findPadre(i) << "!=" << findPadre(0) << ")" << endl;
            return 0;
        }
    }
    map<pair<int, int >, int > coordenadasUsadas;
    vector<int>mientrasA, mientrasB;
    int prioridad;
    bool seLogro = true;
    for(int i = 0 ; i < v.size() ; i++){
        if(node[v[i]].ff == node[u[i]].ff){//vertical
            pair<int, int > izquierdo = {node[v[i]].ff-1, (node[v[i]].ss+node[u[i]].ss) / 2}, derecho = {node[v[i]].ff+1, (node[v[i]].ss+node[u[i]].ss) / 2};
            if(!coordenadasUsadas.count(izquierdo)){
                mientrasA.pb(izquierdo.ff);
                mientrasB.pb(izquierdo.ss);
                //cout << " probando (" << izquierdo.ff << ", " << izquierdo.ss << ") para la union numero " << i << endl;
                coordenadasUsadas[izquierdo] = i;
            }else if(!coordenadasUsadas.count(derecho)){
                mientrasA.pb(derecho.ff);
                mientrasB.pb(derecho.ss);
                //cout << " probando (" << derecho.ff << ", " << derecho.ss << ") para la union numero " << i << endl;
                coordenadasUsadas[derecho] = i;
            }else{
                seLogro = false;
                prioridad = i;
                //cout << "Paila, reintentando desde " << prioridad;
                break;
            }
        }else{ // horizontal
            pair<int, int > arriba = {(node[v[i]].ff+node[u[i]].ff) / 2, node[v[i]].ss+1}, abajo = {(node[v[i]].ff+node[u[i]].ff) / 2, node[v[i]].ss-1};
            if(!coordenadasUsadas.count(arriba)){
                mientrasA.pb(arriba.ff);
                mientrasB.pb(arriba.ss);
                //cout << " probando (" << arriba.ff << ", " << arriba.ss << ") para la union numero " << i << endl;
                coordenadasUsadas[arriba] = i;
            }else if(!coordenadasUsadas.count(abajo)){
                mientrasA.pb(abajo.ff);
                mientrasB.pb(abajo.ss);
                //cout << " probando (" << abajo.ff << ", " << abajo.ss << ") para la union numero " << i << endl;
                coordenadasUsadas[abajo] = i;
            }else{
                seLogro = false;
                prioridad = i;
                //cout << "Paila, reintentando desde " << prioridad;
                break;
            }
        }


    }

    if(seLogro){
        build(u, v, mientrasA, mientrasB);
        return 1;
    }else{
        for(int i = 0 ; i < v.size() ; i ++){
            a.pb(0);
            b.pb(0);
        }

        coordenadasUsadas.clear();


        if(node[v[prioridad]].ff == node[u[prioridad]].ff){//vertical
            pair<int, int > izquierdo = {node[v[prioridad]].ff-1, (node[v[prioridad]].ss+node[u[prioridad]].ss) / 2}, derecho = {node[v[prioridad]].ff+1, (node[v[prioridad]].ss+node[u[prioridad]].ss) / 2};
            if(!coordenadasUsadas.count(izquierdo)){
                a[prioridad] = izquierdo.ff;
                b[prioridad] = izquierdo.ss;
                coordenadasUsadas[izquierdo] = prioridad;
            }else if(!coordenadasUsadas.count(derecho)){
                a[prioridad] = derecho.ff;
                b[prioridad] = derecho.ss;
                coordenadasUsadas[derecho] = prioridad;
            }else{
                seLogro = false;
                prioridad = prioridad;
                return 0;
            }
        }else{ // horizontal
            pair<int, int > arriba = {(node[v[prioridad]].ff+node[u[prioridad]].ff) / 2, node[v[prioridad]].ss+1}, abajo = {(node[v[prioridad]].ff+node[u[prioridad]].ff) / 2, node[v[prioridad]].ss-1};
            if(!coordenadasUsadas.count(arriba)){
                a[prioridad] = arriba.ff;
                b[prioridad] = arriba.ss;
                coordenadasUsadas[arriba] = prioridad;
            }else if(!coordenadasUsadas.count(abajo)){
                a[prioridad] = abajo.ff;
                b[prioridad] = abajo.ss;
                coordenadasUsadas[abajo] = prioridad;
            }else{
                seLogro = false;
                prioridad = prioridad;
                return 0;
            }
        }
        //cout << "en prioridad se unió en " << a[prioridad] <<", " << b[prioridad] << endl;



        for(int i = 0 ; i < v.size() ; i++){
            if(i==prioridad)continue;
            if(node[v[i]].ff == node[u[i]].ff){//vertical
                pair<int, int > izquierdo = {node[v[i]].ff-1, (node[v[i]].ss+node[u[i]].ss) / 2}, derecho = {node[v[i]].ff+1, (node[v[i]].ss+node[u[i]].ss) / 2};
                if(!coordenadasUsadas.count(izquierdo)){
                    a[prioridad] = izquierdo.ff;
                    b[prioridad] = izquierdo.ss;
                    coordenadasUsadas[izquierdo] = i;
                }else if(!coordenadasUsadas.count(derecho)){
                    a[prioridad] = derecho.ff;
                    b[prioridad] = derecho.ss;
                    coordenadasUsadas[derecho] = i;
                }else{
                    seLogro = false;
                    prioridad = i;
                    return 0;
                }
            }else{ // horizontal
                pair<int, int > arriba = {(node[v[i]].ff+node[u[i]].ff) / 2, node[v[i]].ff+1}, abajo = {(node[v[i]].ff+node[u[i]].ff) / 2, node[v[i]].ff-1};
                if(!coordenadasUsadas.count(arriba)){
                    a[prioridad] = arriba.ff;
                    b[prioridad] = arriba.ss;
                    coordenadasUsadas[arriba] = i;
                }else if(!coordenadasUsadas.count(abajo)){
                    a[prioridad] = abajo.ff;
                    b[prioridad] = abajo.ss;
                    coordenadasUsadas[abajo] = i;
                }else{
                    seLogro = false;
                    prioridad = i;
                    //cout << "Paila, no se pudo en " << i << endl;
                    return 0;
                }
            }
            //cout <<"la union " << i << "se unio en " << mientrasA.back() << ", " << mientrasB.back() << endl;


        }


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

}

Compilation message

parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:75:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |     for(int i = 0 ; i < v.size() ; i++){
      |                     ~~^~~~~~~~~~
parks.cpp:121:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |         for(int i = 0 ; i < v.size() ; i ++){
      |                         ~~^~~~~~~~~~
parks.cpp:164:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  164 |         for(int i = 0 ; i < v.size() ; i++){
      |                         ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 173 ms 21588 KB Output is correct
10 Correct 13 ms 2388 KB Output is correct
11 Correct 86 ms 11672 KB Output is correct
12 Correct 23 ms 3520 KB Output is correct
13 Correct 34 ms 4548 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 164 ms 21548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 173 ms 21588 KB Output is correct
10 Correct 13 ms 2388 KB Output is correct
11 Correct 86 ms 11672 KB Output is correct
12 Correct 23 ms 3520 KB Output is correct
13 Correct 34 ms 4548 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 164 ms 21548 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 447 ms 42880 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 3 ms 468 KB Output is correct
26 Correct 2 ms 584 KB Output is correct
27 Correct 2 ms 596 KB Output is correct
28 Correct 154 ms 17412 KB Output is correct
29 Correct 233 ms 25924 KB Output is correct
30 Correct 355 ms 34384 KB Output is correct
31 Correct 425 ms 42932 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 0 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 2 ms 468 KB Output is correct
44 Correct 2 ms 596 KB Output is correct
45 Correct 167 ms 21640 KB Output is correct
46 Correct 294 ms 31308 KB Output is correct
47 Correct 279 ms 31248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 173 ms 21588 KB Output is correct
10 Correct 13 ms 2388 KB Output is correct
11 Correct 86 ms 11672 KB Output is correct
12 Correct 23 ms 3520 KB Output is correct
13 Correct 34 ms 4548 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 164 ms 21548 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 447 ms 42880 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 3 ms 468 KB Output is correct
26 Correct 2 ms 584 KB Output is correct
27 Correct 2 ms 596 KB Output is correct
28 Correct 154 ms 17412 KB Output is correct
29 Correct 233 ms 25924 KB Output is correct
30 Correct 355 ms 34384 KB Output is correct
31 Correct 425 ms 42932 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 0 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 2 ms 468 KB Output is correct
44 Correct 2 ms 596 KB Output is correct
45 Correct 167 ms 21640 KB Output is correct
46 Correct 294 ms 31308 KB Output is correct
47 Correct 279 ms 31248 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Incorrect 0 ms 212 KB a[0] = 0 is not an odd integer
51 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 173 ms 21588 KB Output is correct
10 Correct 13 ms 2388 KB Output is correct
11 Correct 86 ms 11672 KB Output is correct
12 Correct 23 ms 3520 KB Output is correct
13 Correct 34 ms 4548 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 164 ms 21548 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 405 ms 46280 KB Output is correct
21 Correct 403 ms 45936 KB Output is correct
22 Correct 404 ms 45956 KB Output is correct
23 Correct 336 ms 38684 KB Output is correct
24 Correct 130 ms 20028 KB Output is correct
25 Correct 132 ms 19860 KB Output is correct
26 Correct 162 ms 19840 KB Output is correct
27 Incorrect 530 ms 43700 KB a[0] = 0 is not an odd integer
28 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 173 ms 21588 KB Output is correct
10 Correct 13 ms 2388 KB Output is correct
11 Correct 86 ms 11672 KB Output is correct
12 Correct 23 ms 3520 KB Output is correct
13 Correct 34 ms 4548 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 164 ms 21548 KB Output is correct
17 Correct 385 ms 43288 KB Output is correct
18 Correct 435 ms 45700 KB Output is correct
19 Correct 439 ms 45844 KB Output is correct
20 Incorrect 352 ms 24340 KB Solution announced impossible, but it is possible.
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 173 ms 21588 KB Output is correct
10 Correct 13 ms 2388 KB Output is correct
11 Correct 86 ms 11672 KB Output is correct
12 Correct 23 ms 3520 KB Output is correct
13 Correct 34 ms 4548 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 164 ms 21548 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 447 ms 42880 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 3 ms 468 KB Output is correct
26 Correct 2 ms 584 KB Output is correct
27 Correct 2 ms 596 KB Output is correct
28 Correct 154 ms 17412 KB Output is correct
29 Correct 233 ms 25924 KB Output is correct
30 Correct 355 ms 34384 KB Output is correct
31 Correct 425 ms 42932 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 0 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 2 ms 468 KB Output is correct
44 Correct 2 ms 596 KB Output is correct
45 Correct 167 ms 21640 KB Output is correct
46 Correct 294 ms 31308 KB Output is correct
47 Correct 279 ms 31248 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Incorrect 0 ms 212 KB a[0] = 0 is not an odd integer
51 Halted 0 ms 0 KB -