#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
#define x X
#define y Y
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second
const int maxn = 2e5;
const vector<pii > directions = {mp(-2, 0), mp(0, -2), mp(0, 2), mp(2, 0)};
bool check_possibility(int n, vector<int> x, vector<int> y) {
unordered_map<int, unordered_map<int, bool> > m;
unordered_map<int, unordered_map<int, int> > f;
for(int i = 0; i < n; i++) {
f[x[i]][y[i]] = i + 1;
}
queue<int> q;
q.push(0);
m[x[0]][y[0]] = true;
int cnt = 0;
while(!q.empty()) {
int cur = q.front();
q.pop();
cnt++;
for(pii dir : directions) {
pii new_point = mp(x[cur] + dir.fi, y[cur] + dir.se);
if(f[new_point.fi][new_point.se] && !m[new_point.fi][new_point.se]) {
m[new_point.fi][new_point.se] = true;
int new_id = f[new_point.fi][new_point.se] - 1;
q.push(new_id);
}
}
}
return (cnt == n);
}
struct fountain {
int x, y, id;
fountain() : x(0), y(0), id(-1) {}
fountain(int x_, int y_, int id_) :
x(x_), y(y_), id(id_) {}
};
int n;
vector<fountain > points;
vector<int> x;
vector<int> y;
unordered_map<int, unordered_map<int, int> > fountain_at;
unordered_map<int, unordered_map<int, bool> > bench_at;
vector<int> ans_u;
vector<int> ans_v;
vector<int> ans_a;
vector<int> ans_b;
void build_road_bench(int road_u, int road_v, pii bench_pos) {
ans_u.pb(road_u);
ans_v.pb(road_v);
ans_a.pb(bench_pos.fi);
ans_b.pb(bench_pos.se);
bench_at[bench_pos.fi][bench_pos.se] = true;
}
vector<pii> get_bench_locations(int a_idx, int b_idx) {
fountain f_a = points[a_idx], f_b = points[b_idx];
pii a, b;
if(f_a.x == f_b.x) {
int new_y = (f_a.y + f_b.y) / 2;
a = mp(f_a.x + 1, new_y), b = mp(f_a.x - 1, new_y);
} else {
int new_x = (f_a.x + f_b.x) / 2;
a = mp(new_x, f_a.y + 1), b = mp(new_x, f_a.y - 1);
}
return {min(a, b), max(a, b)};
}
void solve() {
unordered_map<int, unordered_map<int, bool> > vis;
queue<int> q;
q.push(0);
vis[x[0]][y[0]] = true;
while(!q.empty()) {
int cur = q.front();
q.pop();
for(pii dir : directions) {
pii new_point = mp(x[cur] + dir.fi, y[cur] + dir.se);
if(fountain_at[new_point.fi][new_point.se] && !vis[new_point.fi][new_point.se]) {
vis[new_point.fi][new_point.se] = true;
int new_id = fountain_at[new_point.fi][new_point.se] - 1;
q.push(new_id);
vector<pii> benches = get_bench_locations(cur, new_id);
if(!bench_at[benches[0].fi][benches[0].se]) {
build_road_bench(cur, new_id, benches[0]);
} else {
build_road_bench(cur, new_id, benches[1]);
}
}
}
}
//
}
#undef x
#undef y
int construct_roads(vector<int> x, vector<int> y) {
// edge case
if (x.size() == 1) {
build({}, {}, {}, {});
return 1;
}
// sample solution
/*vector<int> u, v, a, b;
u.push_back(0);
v.push_back(1);
a.push_back(x[0]+1);
b.push_back(y[0]-1);
build(u, v, a, b);*/
n = x.size();
if(!check_possibility(n, x, y)) {
return 0;
}
X = x;
Y = y;
points.assign(n, fountain());
for(int i = 0; i < n; i++) {
points[i] = fountain(x[i], y[i], i);
fountain_at[x[i]][y[i]] = i + 1;
}
solve();
// build solution
build(ans_u, ans_v, ans_a, ans_b);
return 1;
}
/*
5
4 4
4 6
6 4
4 2
2 4
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
238 ms |
32248 KB |
Output is correct |
10 |
Correct |
18 ms |
3096 KB |
Output is correct |
11 |
Correct |
91 ms |
16544 KB |
Output is correct |
12 |
Correct |
29 ms |
4848 KB |
Output is correct |
13 |
Correct |
28 ms |
7140 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
227 ms |
32224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
238 ms |
32248 KB |
Output is correct |
10 |
Correct |
18 ms |
3096 KB |
Output is correct |
11 |
Correct |
91 ms |
16544 KB |
Output is correct |
12 |
Correct |
29 ms |
4848 KB |
Output is correct |
13 |
Correct |
28 ms |
7140 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
227 ms |
32224 KB |
Output is correct |
17 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
533 ms |
54936 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
616 KB |
Output is correct |
28 |
Correct |
139 ms |
20084 KB |
Output is correct |
29 |
Correct |
233 ms |
31296 KB |
Output is correct |
30 |
Correct |
328 ms |
40164 KB |
Output is correct |
31 |
Correct |
437 ms |
54856 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
296 KB |
Output is correct |
42 |
Correct |
0 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 |
261 ms |
28080 KB |
Output is correct |
46 |
Correct |
384 ms |
41016 KB |
Output is correct |
47 |
Correct |
422 ms |
40764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
238 ms |
32248 KB |
Output is correct |
10 |
Correct |
18 ms |
3096 KB |
Output is correct |
11 |
Correct |
91 ms |
16544 KB |
Output is correct |
12 |
Correct |
29 ms |
4848 KB |
Output is correct |
13 |
Correct |
28 ms |
7140 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
227 ms |
32224 KB |
Output is correct |
17 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
533 ms |
54936 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
616 KB |
Output is correct |
28 |
Correct |
139 ms |
20084 KB |
Output is correct |
29 |
Correct |
233 ms |
31296 KB |
Output is correct |
30 |
Correct |
328 ms |
40164 KB |
Output is correct |
31 |
Correct |
437 ms |
54856 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
296 KB |
Output is correct |
42 |
Correct |
0 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 |
261 ms |
28080 KB |
Output is correct |
46 |
Correct |
384 ms |
41016 KB |
Output is correct |
47 |
Correct |
422 ms |
40764 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Incorrect |
0 ms |
212 KB |
Tree @(5, 3) appears more than once: for edges on positions 0 and 3 |
52 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
238 ms |
32248 KB |
Output is correct |
10 |
Correct |
18 ms |
3096 KB |
Output is correct |
11 |
Correct |
91 ms |
16544 KB |
Output is correct |
12 |
Correct |
29 ms |
4848 KB |
Output is correct |
13 |
Correct |
28 ms |
7140 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
227 ms |
32224 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
296 KB |
Output is correct |
20 |
Correct |
812 ms |
108512 KB |
Output is correct |
21 |
Correct |
768 ms |
77888 KB |
Output is correct |
22 |
Correct |
821 ms |
77872 KB |
Output is correct |
23 |
Correct |
648 ms |
78196 KB |
Output is correct |
24 |
Correct |
80 ms |
14648 KB |
Output is correct |
25 |
Correct |
75 ms |
14656 KB |
Output is correct |
26 |
Correct |
73 ms |
14664 KB |
Output is correct |
27 |
Correct |
619 ms |
53492 KB |
Output is correct |
28 |
Incorrect |
609 ms |
53460 KB |
Tree @(863, 999) appears more than once: for edges on positions 500 and 503 |
29 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
238 ms |
32248 KB |
Output is correct |
10 |
Correct |
18 ms |
3096 KB |
Output is correct |
11 |
Correct |
91 ms |
16544 KB |
Output is correct |
12 |
Correct |
29 ms |
4848 KB |
Output is correct |
13 |
Correct |
28 ms |
7140 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
227 ms |
32224 KB |
Output is correct |
17 |
Correct |
743 ms |
88260 KB |
Output is correct |
18 |
Correct |
843 ms |
90372 KB |
Output is correct |
19 |
Correct |
762 ms |
77840 KB |
Output is correct |
20 |
Incorrect |
533 ms |
47192 KB |
Tree @(263, 877) appears more than once: for edges on positions 4 and 9 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
238 ms |
32248 KB |
Output is correct |
10 |
Correct |
18 ms |
3096 KB |
Output is correct |
11 |
Correct |
91 ms |
16544 KB |
Output is correct |
12 |
Correct |
29 ms |
4848 KB |
Output is correct |
13 |
Correct |
28 ms |
7140 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
227 ms |
32224 KB |
Output is correct |
17 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
533 ms |
54936 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
616 KB |
Output is correct |
28 |
Correct |
139 ms |
20084 KB |
Output is correct |
29 |
Correct |
233 ms |
31296 KB |
Output is correct |
30 |
Correct |
328 ms |
40164 KB |
Output is correct |
31 |
Correct |
437 ms |
54856 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
296 KB |
Output is correct |
42 |
Correct |
0 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 |
261 ms |
28080 KB |
Output is correct |
46 |
Correct |
384 ms |
41016 KB |
Output is correct |
47 |
Correct |
422 ms |
40764 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Incorrect |
0 ms |
212 KB |
Tree @(5, 3) appears more than once: for edges on positions 0 and 3 |
52 |
Halted |
0 ms |
0 KB |
- |