#include <bits/stdc++.h>
#include "parks.h"
using namespace std;
const int nax = 200000;
vector<int>g[nax];
bool vis[nax];
int par[nax], sz[nax];
void trav(int v) {
vis[v] = 1;
for(int u : g[v]) if(!vis[u]) {
trav(u);
}
}
int find(int v) {
return par[v] = (v==par[v]) ? v : find(par[v]);
}
void unite(int u, int v) {
u = find(u), v = find(v);
if(sz[u] < sz[v]) swap(u, v);
sz[u] += sz[v];
par[v] = u;
}
int construct_roads(vector<int> x, vector<int> y) {
int n = x.size();
map<pair<int,int>, int>ft;
for(int i=0; i<n; ++i) {
ft[{x[i], y[i]}] = i;
}
int dx[] = {2, -2, 0, 0};
int dy[] = {0, 0, 2, -2};
vector<int>u, v, a, b;
map<pair<int,int>, int>ben;
for(int i=0; i<n; ++i) {
par[i] = i, sz[i] = 1;
}
for(int i=0; i<n; ++i) {
for(int j=0; j<4; ++j) {
int nx = x[i] + dx[j], ny = y[i] + dy[j];
if(ft.count(make_pair(nx, ny))) {
if(find(i) == find(ft[{nx, ny}])) continue;
if(j < 2) { //horizontal
if((x[i] + nx >> 1) < 4) {
if(!ben.count(make_pair(x[i] + nx >> 1, ny - 1))) {
ben[{x[i] + nx >> 1, ny - 1}] = 1;
a.push_back(x[i] + nx >> 1);
b.push_back(ny - 1);
} else if(!ben.count(make_pair(x[i] + nx >> 1, ny + 1))) {
ben[{x[i] + nx >> 1, ny + 1}] = 1;
a.push_back(x[i] + nx >> 1);
b.push_back(ny + 1);
} else return 0;
} else {
if(!ben.count(make_pair(x[i] + nx >> 1, ny + 1))) {
ben[{x[i] + nx >> 1, ny + 1}] = 1;
a.push_back(x[i] + nx >> 1);
b.push_back(ny + 1);
} else if(!ben.count(make_pair(x[i] + nx >> 1, ny - 1))) {
ben[{x[i] + nx >> 1, ny - 1}] = 1;
a.push_back(x[i] + nx >> 1);
b.push_back(ny - 1);
} else return 0;
}
} else { //vertical
if(x[i]==2) {
a.push_back(x[i] - 1);
b.push_back(y[i] + ny >> 1);
} else if(x[i]==6) {
a.push_back(x[i] + 1);
b.push_back(y[i] + ny >> 1);
} else {
continue;
}
}
g[i].push_back(ft[{nx, ny}]);
g[ft[{nx, ny}]].push_back(i);
unite(i, ft[{nx, ny}]);
u.push_back(i);
v.push_back(ft[{nx, ny}]);
}
}
}
for(int i=0; i<n; ++i) {
for(int j=2; j<4; ++j) {
int nx = x[i] + dx[j], ny = y[i] + dy[j];
if(ft.count(make_pair(nx, ny))) {
if(find(i) == find(ft[{nx, ny}])) continue;
g[i].push_back(ft[{nx, ny}]);
g[ft[{nx, ny}]].push_back(i);
unite(i, ft[{nx, ny}]);
u.push_back(i);
v.push_back(ft[{nx, ny}]);
if(!ben.count(make_pair(nx-1, y[i] + ny >> 1))) {
ben[{nx-1, y[i] + ny >> 1}] = 1;
a.push_back(nx-1);
b.push_back(y[i] + ny >> 1);
} else if(!ben.count(make_pair(nx+1, y[i] + ny >> 1))) {
ben[{nx+1, y[i] + ny >> 1}] = 1;
a.push_back(nx+1);
b.push_back(y[i] + ny >> 1);
} else return 0;
}
}
}
trav(0);
for(int i=0; i<n; ++i) if(!vis[i]) {
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:52:30: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | if((x[i] + nx >> 1) < 4) {
parks.cpp:53:54: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | if(!ben.count(make_pair(x[i] + nx >> 1, ny - 1))) {
parks.cpp:54:39: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
54 | ben[{x[i] + nx >> 1, ny - 1}] = 1;
parks.cpp:55:46: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
55 | a.push_back(x[i] + nx >> 1);
parks.cpp:57:61: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
57 | } else if(!ben.count(make_pair(x[i] + nx >> 1, ny + 1))) {
parks.cpp:58:39: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
58 | ben[{x[i] + nx >> 1, ny + 1}] = 1;
parks.cpp:59:46: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
59 | a.push_back(x[i] + nx >> 1);
parks.cpp:64:54: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
64 | if(!ben.count(make_pair(x[i] + nx >> 1, ny + 1))) {
parks.cpp:65:39: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
65 | ben[{x[i] + nx >> 1, ny + 1}] = 1;
parks.cpp:66:46: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
66 | a.push_back(x[i] + nx >> 1);
parks.cpp:69:61: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
69 | } else if(!ben.count(make_pair(x[i] + nx >> 1, ny - 1))) {
parks.cpp:70:39: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
70 | ben[{x[i] + nx >> 1, ny - 1}] = 1;
parks.cpp:71:46: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
71 | a.push_back(x[i] + nx >> 1);
parks.cpp:78:42: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
78 | b.push_back(y[i] + ny >> 1);
parks.cpp:81:42: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
81 | b.push_back(y[i] + ny >> 1);
parks.cpp:106:52: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
106 | if(!ben.count(make_pair(nx-1, y[i] + ny >> 1))) {
parks.cpp:107:37: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
107 | ben[{nx-1, y[i] + ny >> 1}] = 1;
parks.cpp:109:38: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
109 | b.push_back(y[i] + ny >> 1);
parks.cpp:110:59: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
110 | } else if(!ben.count(make_pair(nx+1, y[i] + ny >> 1))) {
parks.cpp:111:37: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
111 | ben[{nx+1, y[i] + ny >> 1}] = 1;
parks.cpp:113:38: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
113 | b.push_back(y[i] + ny >> 1);
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4980 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
258 ms |
23080 KB |
Output is correct |
10 |
Correct |
16 ms |
7060 KB |
Output is correct |
11 |
Correct |
101 ms |
14812 KB |
Output is correct |
12 |
Correct |
25 ms |
8032 KB |
Output is correct |
13 |
Correct |
72 ms |
11424 KB |
Output is correct |
14 |
Correct |
4 ms |
5076 KB |
Output is correct |
15 |
Correct |
5 ms |
5204 KB |
Output is correct |
16 |
Correct |
261 ms |
22388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4980 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
258 ms |
23080 KB |
Output is correct |
10 |
Correct |
16 ms |
7060 KB |
Output is correct |
11 |
Correct |
101 ms |
14812 KB |
Output is correct |
12 |
Correct |
25 ms |
8032 KB |
Output is correct |
13 |
Correct |
72 ms |
11424 KB |
Output is correct |
14 |
Correct |
4 ms |
5076 KB |
Output is correct |
15 |
Correct |
5 ms |
5204 KB |
Output is correct |
16 |
Correct |
261 ms |
22388 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
2 ms |
4948 KB |
Output is correct |
23 |
Correct |
899 ms |
45848 KB |
Output is correct |
24 |
Correct |
2 ms |
4948 KB |
Output is correct |
25 |
Correct |
4 ms |
5204 KB |
Output is correct |
26 |
Correct |
7 ms |
5460 KB |
Output is correct |
27 |
Correct |
9 ms |
5664 KB |
Output is correct |
28 |
Correct |
258 ms |
21388 KB |
Output is correct |
29 |
Correct |
412 ms |
29424 KB |
Output is correct |
30 |
Correct |
641 ms |
37804 KB |
Output is correct |
31 |
Correct |
887 ms |
45708 KB |
Output is correct |
32 |
Correct |
2 ms |
4948 KB |
Output is correct |
33 |
Correct |
2 ms |
4948 KB |
Output is correct |
34 |
Correct |
2 ms |
4948 KB |
Output is correct |
35 |
Correct |
2 ms |
4948 KB |
Output is correct |
36 |
Correct |
2 ms |
4948 KB |
Output is correct |
37 |
Correct |
2 ms |
4948 KB |
Output is correct |
38 |
Correct |
2 ms |
4948 KB |
Output is correct |
39 |
Correct |
2 ms |
4920 KB |
Output is correct |
40 |
Correct |
2 ms |
4948 KB |
Output is correct |
41 |
Correct |
2 ms |
4948 KB |
Output is correct |
42 |
Correct |
2 ms |
4948 KB |
Output is correct |
43 |
Correct |
5 ms |
5280 KB |
Output is correct |
44 |
Correct |
7 ms |
5460 KB |
Output is correct |
45 |
Correct |
322 ms |
26736 KB |
Output is correct |
46 |
Correct |
589 ms |
36636 KB |
Output is correct |
47 |
Correct |
582 ms |
36564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4980 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
258 ms |
23080 KB |
Output is correct |
10 |
Correct |
16 ms |
7060 KB |
Output is correct |
11 |
Correct |
101 ms |
14812 KB |
Output is correct |
12 |
Correct |
25 ms |
8032 KB |
Output is correct |
13 |
Correct |
72 ms |
11424 KB |
Output is correct |
14 |
Correct |
4 ms |
5076 KB |
Output is correct |
15 |
Correct |
5 ms |
5204 KB |
Output is correct |
16 |
Correct |
261 ms |
22388 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
2 ms |
4948 KB |
Output is correct |
23 |
Correct |
899 ms |
45848 KB |
Output is correct |
24 |
Correct |
2 ms |
4948 KB |
Output is correct |
25 |
Correct |
4 ms |
5204 KB |
Output is correct |
26 |
Correct |
7 ms |
5460 KB |
Output is correct |
27 |
Correct |
9 ms |
5664 KB |
Output is correct |
28 |
Correct |
258 ms |
21388 KB |
Output is correct |
29 |
Correct |
412 ms |
29424 KB |
Output is correct |
30 |
Correct |
641 ms |
37804 KB |
Output is correct |
31 |
Correct |
887 ms |
45708 KB |
Output is correct |
32 |
Correct |
2 ms |
4948 KB |
Output is correct |
33 |
Correct |
2 ms |
4948 KB |
Output is correct |
34 |
Correct |
2 ms |
4948 KB |
Output is correct |
35 |
Correct |
2 ms |
4948 KB |
Output is correct |
36 |
Correct |
2 ms |
4948 KB |
Output is correct |
37 |
Correct |
2 ms |
4948 KB |
Output is correct |
38 |
Correct |
2 ms |
4948 KB |
Output is correct |
39 |
Correct |
2 ms |
4920 KB |
Output is correct |
40 |
Correct |
2 ms |
4948 KB |
Output is correct |
41 |
Correct |
2 ms |
4948 KB |
Output is correct |
42 |
Correct |
2 ms |
4948 KB |
Output is correct |
43 |
Correct |
5 ms |
5280 KB |
Output is correct |
44 |
Correct |
7 ms |
5460 KB |
Output is correct |
45 |
Correct |
322 ms |
26736 KB |
Output is correct |
46 |
Correct |
589 ms |
36636 KB |
Output is correct |
47 |
Correct |
582 ms |
36564 KB |
Output is correct |
48 |
Correct |
2 ms |
4948 KB |
Output is correct |
49 |
Correct |
2 ms |
4948 KB |
Output is correct |
50 |
Correct |
2 ms |
4948 KB |
Output is correct |
51 |
Correct |
2 ms |
4948 KB |
Output is correct |
52 |
Correct |
2 ms |
4948 KB |
Output is correct |
53 |
Correct |
3 ms |
4948 KB |
Output is correct |
54 |
Incorrect |
2 ms |
4948 KB |
Solution announced impossible, but it is possible. |
55 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4980 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
258 ms |
23080 KB |
Output is correct |
10 |
Correct |
16 ms |
7060 KB |
Output is correct |
11 |
Correct |
101 ms |
14812 KB |
Output is correct |
12 |
Correct |
25 ms |
8032 KB |
Output is correct |
13 |
Correct |
72 ms |
11424 KB |
Output is correct |
14 |
Correct |
4 ms |
5076 KB |
Output is correct |
15 |
Correct |
5 ms |
5204 KB |
Output is correct |
16 |
Correct |
261 ms |
22388 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Incorrect |
688 ms |
39388 KB |
Solution announced impossible, but it is possible. |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4980 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
258 ms |
23080 KB |
Output is correct |
10 |
Correct |
16 ms |
7060 KB |
Output is correct |
11 |
Correct |
101 ms |
14812 KB |
Output is correct |
12 |
Correct |
25 ms |
8032 KB |
Output is correct |
13 |
Correct |
72 ms |
11424 KB |
Output is correct |
14 |
Correct |
4 ms |
5076 KB |
Output is correct |
15 |
Correct |
5 ms |
5204 KB |
Output is correct |
16 |
Correct |
261 ms |
22388 KB |
Output is correct |
17 |
Correct |
774 ms |
50436 KB |
Output is correct |
18 |
Incorrect |
544 ms |
37592 KB |
Solution announced impossible, but it is possible. |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4980 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
258 ms |
23080 KB |
Output is correct |
10 |
Correct |
16 ms |
7060 KB |
Output is correct |
11 |
Correct |
101 ms |
14812 KB |
Output is correct |
12 |
Correct |
25 ms |
8032 KB |
Output is correct |
13 |
Correct |
72 ms |
11424 KB |
Output is correct |
14 |
Correct |
4 ms |
5076 KB |
Output is correct |
15 |
Correct |
5 ms |
5204 KB |
Output is correct |
16 |
Correct |
261 ms |
22388 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
2 ms |
4948 KB |
Output is correct |
23 |
Correct |
899 ms |
45848 KB |
Output is correct |
24 |
Correct |
2 ms |
4948 KB |
Output is correct |
25 |
Correct |
4 ms |
5204 KB |
Output is correct |
26 |
Correct |
7 ms |
5460 KB |
Output is correct |
27 |
Correct |
9 ms |
5664 KB |
Output is correct |
28 |
Correct |
258 ms |
21388 KB |
Output is correct |
29 |
Correct |
412 ms |
29424 KB |
Output is correct |
30 |
Correct |
641 ms |
37804 KB |
Output is correct |
31 |
Correct |
887 ms |
45708 KB |
Output is correct |
32 |
Correct |
2 ms |
4948 KB |
Output is correct |
33 |
Correct |
2 ms |
4948 KB |
Output is correct |
34 |
Correct |
2 ms |
4948 KB |
Output is correct |
35 |
Correct |
2 ms |
4948 KB |
Output is correct |
36 |
Correct |
2 ms |
4948 KB |
Output is correct |
37 |
Correct |
2 ms |
4948 KB |
Output is correct |
38 |
Correct |
2 ms |
4948 KB |
Output is correct |
39 |
Correct |
2 ms |
4920 KB |
Output is correct |
40 |
Correct |
2 ms |
4948 KB |
Output is correct |
41 |
Correct |
2 ms |
4948 KB |
Output is correct |
42 |
Correct |
2 ms |
4948 KB |
Output is correct |
43 |
Correct |
5 ms |
5280 KB |
Output is correct |
44 |
Correct |
7 ms |
5460 KB |
Output is correct |
45 |
Correct |
322 ms |
26736 KB |
Output is correct |
46 |
Correct |
589 ms |
36636 KB |
Output is correct |
47 |
Correct |
582 ms |
36564 KB |
Output is correct |
48 |
Correct |
2 ms |
4948 KB |
Output is correct |
49 |
Correct |
2 ms |
4948 KB |
Output is correct |
50 |
Correct |
2 ms |
4948 KB |
Output is correct |
51 |
Correct |
2 ms |
4948 KB |
Output is correct |
52 |
Correct |
2 ms |
4948 KB |
Output is correct |
53 |
Correct |
3 ms |
4948 KB |
Output is correct |
54 |
Incorrect |
2 ms |
4948 KB |
Solution announced impossible, but it is possible. |
55 |
Halted |
0 ms |
0 KB |
- |