#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;
bool imposible = false;
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, 0, 3){
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)){
// 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]) / 2;
if ((x[nodo] + y[nodo]) & 3){
if (!i) bx = x[nodo] - 1;
else bx = x[nodo] + 1;
}
else{
if (!i) 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, 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, 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 || imposible) return 0;
build(u, v, a, b);
return 1;
}
Compilation message
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:92:5: note: in expansion of macro 'rep'
92 | 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:95:5: note: in expansion of macro 'rep'
95 | 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:101:5: note: in expansion of macro 'rep'
101 | rep(i, 1, x.size() - 1) if (comp(i) != gfinal){
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
5 ms |
4972 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 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 |
194 ms |
29528 KB |
Output is correct |
10 |
Correct |
16 ms |
7756 KB |
Output is correct |
11 |
Correct |
82 ms |
18820 KB |
Output is correct |
12 |
Correct |
25 ms |
9080 KB |
Output is correct |
13 |
Correct |
52 ms |
13164 KB |
Output is correct |
14 |
Correct |
4 ms |
5068 KB |
Output is correct |
15 |
Correct |
6 ms |
5196 KB |
Output is correct |
16 |
Correct |
180 ms |
24500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
5 ms |
4972 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 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 |
194 ms |
29528 KB |
Output is correct |
10 |
Correct |
16 ms |
7756 KB |
Output is correct |
11 |
Correct |
82 ms |
18820 KB |
Output is correct |
12 |
Correct |
25 ms |
9080 KB |
Output is correct |
13 |
Correct |
52 ms |
13164 KB |
Output is correct |
14 |
Correct |
4 ms |
5068 KB |
Output is correct |
15 |
Correct |
6 ms |
5196 KB |
Output is correct |
16 |
Correct |
180 ms |
24500 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4984 KB |
Output is correct |
19 |
Incorrect |
4 ms |
4940 KB |
Tree @(3, 3) appears more than once: for edges on positions 2 and 4 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
5 ms |
4972 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 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 |
194 ms |
29528 KB |
Output is correct |
10 |
Correct |
16 ms |
7756 KB |
Output is correct |
11 |
Correct |
82 ms |
18820 KB |
Output is correct |
12 |
Correct |
25 ms |
9080 KB |
Output is correct |
13 |
Correct |
52 ms |
13164 KB |
Output is correct |
14 |
Correct |
4 ms |
5068 KB |
Output is correct |
15 |
Correct |
6 ms |
5196 KB |
Output is correct |
16 |
Correct |
180 ms |
24500 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4984 KB |
Output is correct |
19 |
Incorrect |
4 ms |
4940 KB |
Tree @(3, 3) appears more than once: for edges on positions 2 and 4 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
5 ms |
4972 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 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 |
194 ms |
29528 KB |
Output is correct |
10 |
Correct |
16 ms |
7756 KB |
Output is correct |
11 |
Correct |
82 ms |
18820 KB |
Output is correct |
12 |
Correct |
25 ms |
9080 KB |
Output is correct |
13 |
Correct |
52 ms |
13164 KB |
Output is correct |
14 |
Correct |
4 ms |
5068 KB |
Output is correct |
15 |
Correct |
6 ms |
5196 KB |
Output is correct |
16 |
Correct |
180 ms |
24500 KB |
Output is correct |
17 |
Correct |
4 ms |
4908 KB |
Output is correct |
18 |
Correct |
5 ms |
4940 KB |
Output is correct |
19 |
Correct |
4 ms |
4940 KB |
Output is correct |
20 |
Correct |
552 ms |
55900 KB |
Output is correct |
21 |
Correct |
598 ms |
50796 KB |
Output is correct |
22 |
Correct |
508 ms |
48032 KB |
Output is correct |
23 |
Correct |
422 ms |
40684 KB |
Output is correct |
24 |
Correct |
202 ms |
21472 KB |
Output is correct |
25 |
Correct |
186 ms |
21512 KB |
Output is correct |
26 |
Correct |
185 ms |
21404 KB |
Output is correct |
27 |
Correct |
381 ms |
31120 KB |
Output is correct |
28 |
Correct |
417 ms |
31124 KB |
Output is correct |
29 |
Correct |
580 ms |
31132 KB |
Output is correct |
30 |
Correct |
598 ms |
31080 KB |
Output is correct |
31 |
Correct |
4 ms |
4940 KB |
Output is correct |
32 |
Correct |
26 ms |
7940 KB |
Output is correct |
33 |
Correct |
78 ms |
13284 KB |
Output is correct |
34 |
Correct |
569 ms |
51872 KB |
Output is correct |
35 |
Correct |
11 ms |
5964 KB |
Output is correct |
36 |
Correct |
41 ms |
9360 KB |
Output is correct |
37 |
Correct |
103 ms |
13480 KB |
Output is correct |
38 |
Correct |
176 ms |
15728 KB |
Output is correct |
39 |
Correct |
223 ms |
19540 KB |
Output is correct |
40 |
Correct |
341 ms |
23720 KB |
Output is correct |
41 |
Correct |
412 ms |
27632 KB |
Output is correct |
42 |
Correct |
521 ms |
31604 KB |
Output is correct |
43 |
Correct |
4 ms |
4940 KB |
Output is correct |
44 |
Correct |
4 ms |
4940 KB |
Output is correct |
45 |
Correct |
4 ms |
4940 KB |
Output is correct |
46 |
Correct |
3 ms |
4940 KB |
Output is correct |
47 |
Correct |
4 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 |
4 ms |
4940 KB |
Output is correct |
54 |
Correct |
6 ms |
5196 KB |
Output is correct |
55 |
Correct |
5 ms |
5196 KB |
Output is correct |
56 |
Correct |
212 ms |
25768 KB |
Output is correct |
57 |
Correct |
322 ms |
35764 KB |
Output is correct |
58 |
Correct |
302 ms |
34980 KB |
Output is correct |
59 |
Correct |
5 ms |
4940 KB |
Output is correct |
60 |
Correct |
5 ms |
4940 KB |
Output is correct |
61 |
Correct |
4 ms |
4940 KB |
Output is correct |
62 |
Correct |
425 ms |
48764 KB |
Output is correct |
63 |
Correct |
507 ms |
49060 KB |
Output is correct |
64 |
Correct |
491 ms |
51652 KB |
Output is correct |
65 |
Correct |
6 ms |
5452 KB |
Output is correct |
66 |
Correct |
8 ms |
5724 KB |
Output is correct |
67 |
Correct |
251 ms |
22144 KB |
Output is correct |
68 |
Correct |
392 ms |
33784 KB |
Output is correct |
69 |
Correct |
541 ms |
39628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
5 ms |
4972 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 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 |
194 ms |
29528 KB |
Output is correct |
10 |
Correct |
16 ms |
7756 KB |
Output is correct |
11 |
Correct |
82 ms |
18820 KB |
Output is correct |
12 |
Correct |
25 ms |
9080 KB |
Output is correct |
13 |
Correct |
52 ms |
13164 KB |
Output is correct |
14 |
Correct |
4 ms |
5068 KB |
Output is correct |
15 |
Correct |
6 ms |
5196 KB |
Output is correct |
16 |
Correct |
180 ms |
24500 KB |
Output is correct |
17 |
Correct |
529 ms |
58640 KB |
Output is correct |
18 |
Correct |
534 ms |
54936 KB |
Output is correct |
19 |
Correct |
642 ms |
58880 KB |
Output is correct |
20 |
Correct |
504 ms |
46796 KB |
Output is correct |
21 |
Correct |
422 ms |
40060 KB |
Output is correct |
22 |
Correct |
4 ms |
4940 KB |
Output is correct |
23 |
Correct |
70 ms |
10464 KB |
Output is correct |
24 |
Correct |
39 ms |
7468 KB |
Output is correct |
25 |
Correct |
63 ms |
11828 KB |
Output is correct |
26 |
Correct |
126 ms |
15860 KB |
Output is correct |
27 |
Correct |
204 ms |
25164 KB |
Output is correct |
28 |
Correct |
345 ms |
30924 KB |
Output is correct |
29 |
Correct |
468 ms |
36192 KB |
Output is correct |
30 |
Correct |
455 ms |
40564 KB |
Output is correct |
31 |
Correct |
579 ms |
46380 KB |
Output is correct |
32 |
Correct |
585 ms |
51244 KB |
Output is correct |
33 |
Correct |
544 ms |
58604 KB |
Output is correct |
34 |
Correct |
8 ms |
5580 KB |
Output is correct |
35 |
Correct |
12 ms |
5964 KB |
Output is correct |
36 |
Correct |
207 ms |
23068 KB |
Output is correct |
37 |
Correct |
326 ms |
32800 KB |
Output is correct |
38 |
Correct |
467 ms |
42020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
5 ms |
4972 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 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 |
194 ms |
29528 KB |
Output is correct |
10 |
Correct |
16 ms |
7756 KB |
Output is correct |
11 |
Correct |
82 ms |
18820 KB |
Output is correct |
12 |
Correct |
25 ms |
9080 KB |
Output is correct |
13 |
Correct |
52 ms |
13164 KB |
Output is correct |
14 |
Correct |
4 ms |
5068 KB |
Output is correct |
15 |
Correct |
6 ms |
5196 KB |
Output is correct |
16 |
Correct |
180 ms |
24500 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4984 KB |
Output is correct |
19 |
Incorrect |
4 ms |
4940 KB |
Tree @(3, 3) appears more than once: for edges on positions 2 and 4 |
20 |
Halted |
0 ms |
0 KB |
- |