/**
* Task: parks
* Author: Kian Mirjalali
* Solution: Solution based on chessboard coloring of cells (trees)
* Time: O(n) on average
* It uses a hash map to find the index of a vertex by its coordinate.
* The solution is correct but it will become slow on some inputs due to the bad hash function.
*/
#include "parks.h"
#include <complex>
#include <unordered_map>
using namespace std;
#define tpc(C) template<class C>
#define allOf(c) ((c).begin()), ((c).end())
#define fori(i, a, b) for (int i = (a); i < int(b); i++)
#define forn(i, n) fori(i, 0, (n))
#define dbg(x) #x << "=" << (x)
#define show(x) cerr << dbg(x) << endl
tpc(C) inline int sz(const C& c) { return c.size(); }
typedef vector<int> VI;
typedef complex<int> Point;
#define X real()
#define Y imag()
/////////////////////////
const Point MOVES[] = {
{-2, 0},
{+2, 0},
{0, -2},
{0, +2},
};
const int LEFT = 0;
const int RIGHT = 1;
const int UP = 3;
/////////////////////////
struct PointHash {
inline size_t operator()(const Point &a) const {
auto h = hash<int>();
return h(a.X) ^ h(a.Y); // XOR is not a good hash function
}
};
class Point2Index {
unordered_map<Point, int, PointHash> p2i;
public:
inline Point2Index() {
}
inline void put(const Point& p, int i) {
p2i[p] = i;
}
inline int get(const Point& p) const {
auto it = p2i.find(p);
return (it == p2i.end()) ? -1 : it->second;
}
inline bool contains(const Point& p) const {
return p2i.find(p) != p2i.end();
}
};
/////////////////////////
VI q, mark;
inline void enq(int x) {
if (mark[x])
return;
mark[x] = true;
q.push_back(x);
}
/////////////////////////
int construct_roads(VI x, VI y) {
int n = x.size();
vector<Point> vertex(n);
Point2Index v2i;
forn(i, n) {
vertex[i] = Point(x[i], y[i]);
v2i.put(vertex[i], i);
}
{// Check connectivity
q.clear();
q.reserve(n);
mark.assign(n, false);
enq(0);
for (int tail = 0; tail < sz(q); tail++) {
Point p = vertex[q[tail]];
for (auto& mov : MOVES) {
int nei = v2i.get(p+mov);
if (nei >= 0)
enq(nei);
}
}
if (sz(q) != n)
return 0;
}
VI u, v, a, b;
forn(i, n) {
Point pi = vertex[i];
bool is_white_cell = (((pi.X+pi.Y)&2) == 0);
{
Point pj = pi + MOVES[RIGHT];
int j = v2i.get(pj);
if ((j >= 0) && !(v2i.contains(pi+MOVES[UP]) && v2i.contains(pj+MOVES[UP]) && is_white_cell)) {
u.push_back(i);
v.push_back(j);
a.push_back(pi.X+1);
b.push_back(pi.Y+(is_white_cell?+1:-1));
}
}
{
Point pj = pi + MOVES[UP];
int j = v2i.get(pj);
if ((j >= 0) && !(v2i.contains(pi+MOVES[LEFT]) && v2i.contains(pj+MOVES[LEFT]) && is_white_cell)) {
u.push_back(i);
v.push_back(j);
a.push_back(pi.X+(is_white_cell?-1:+1));
b.push_back(pi.Y+1);
}
}
}
build(u, v, a, b);
return 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
145 ms |
14496 KB |
Output is correct |
10 |
Correct |
10 ms |
1868 KB |
Output is correct |
11 |
Correct |
47 ms |
7808 KB |
Output is correct |
12 |
Correct |
11 ms |
2472 KB |
Output is correct |
13 |
Correct |
22 ms |
4344 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
154 ms |
14436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
145 ms |
14496 KB |
Output is correct |
10 |
Correct |
10 ms |
1868 KB |
Output is correct |
11 |
Correct |
47 ms |
7808 KB |
Output is correct |
12 |
Correct |
11 ms |
2472 KB |
Output is correct |
13 |
Correct |
22 ms |
4344 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
154 ms |
14436 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
506 ms |
28708 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
3 ms |
516 KB |
Output is correct |
27 |
Correct |
3 ms |
588 KB |
Output is correct |
28 |
Correct |
109 ms |
11316 KB |
Output is correct |
29 |
Correct |
185 ms |
16752 KB |
Output is correct |
30 |
Correct |
369 ms |
22460 KB |
Output is correct |
31 |
Correct |
398 ms |
28788 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
2 ms |
460 KB |
Output is correct |
45 |
Correct |
130 ms |
14424 KB |
Output is correct |
46 |
Correct |
321 ms |
20612 KB |
Output is correct |
47 |
Correct |
293 ms |
20608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
145 ms |
14496 KB |
Output is correct |
10 |
Correct |
10 ms |
1868 KB |
Output is correct |
11 |
Correct |
47 ms |
7808 KB |
Output is correct |
12 |
Correct |
11 ms |
2472 KB |
Output is correct |
13 |
Correct |
22 ms |
4344 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
154 ms |
14436 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
506 ms |
28708 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
3 ms |
516 KB |
Output is correct |
27 |
Correct |
3 ms |
588 KB |
Output is correct |
28 |
Correct |
109 ms |
11316 KB |
Output is correct |
29 |
Correct |
185 ms |
16752 KB |
Output is correct |
30 |
Correct |
369 ms |
22460 KB |
Output is correct |
31 |
Correct |
398 ms |
28788 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
2 ms |
460 KB |
Output is correct |
45 |
Correct |
130 ms |
14424 KB |
Output is correct |
46 |
Correct |
321 ms |
20612 KB |
Output is correct |
47 |
Correct |
293 ms |
20608 KB |
Output is correct |
48 |
Correct |
1 ms |
244 KB |
Output is correct |
49 |
Correct |
1 ms |
204 KB |
Output is correct |
50 |
Correct |
1 ms |
204 KB |
Output is correct |
51 |
Correct |
1 ms |
204 KB |
Output is correct |
52 |
Correct |
1 ms |
204 KB |
Output is correct |
53 |
Correct |
1 ms |
204 KB |
Output is correct |
54 |
Correct |
1 ms |
204 KB |
Output is correct |
55 |
Correct |
533 ms |
28772 KB |
Output is correct |
56 |
Correct |
1 ms |
204 KB |
Output is correct |
57 |
Correct |
3 ms |
528 KB |
Output is correct |
58 |
Correct |
6 ms |
1248 KB |
Output is correct |
59 |
Correct |
7 ms |
1100 KB |
Output is correct |
60 |
Correct |
191 ms |
14528 KB |
Output is correct |
61 |
Correct |
308 ms |
19528 KB |
Output is correct |
62 |
Correct |
403 ms |
23056 KB |
Output is correct |
63 |
Correct |
533 ms |
28812 KB |
Output is correct |
64 |
Correct |
1 ms |
204 KB |
Output is correct |
65 |
Correct |
1 ms |
204 KB |
Output is correct |
66 |
Correct |
1 ms |
204 KB |
Output is correct |
67 |
Correct |
493 ms |
28748 KB |
Output is correct |
68 |
Correct |
419 ms |
28796 KB |
Output is correct |
69 |
Correct |
536 ms |
28656 KB |
Output is correct |
70 |
Correct |
2 ms |
588 KB |
Output is correct |
71 |
Correct |
3 ms |
844 KB |
Output is correct |
72 |
Correct |
132 ms |
14464 KB |
Output is correct |
73 |
Correct |
396 ms |
21224 KB |
Output is correct |
74 |
Correct |
413 ms |
28756 KB |
Output is correct |
75 |
Correct |
474 ms |
30208 KB |
Output is correct |
76 |
Correct |
491 ms |
28876 KB |
Output is correct |
77 |
Correct |
3 ms |
588 KB |
Output is correct |
78 |
Correct |
5 ms |
1032 KB |
Output is correct |
79 |
Correct |
189 ms |
14840 KB |
Output is correct |
80 |
Correct |
455 ms |
21816 KB |
Output is correct |
81 |
Correct |
441 ms |
29552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
145 ms |
14496 KB |
Output is correct |
10 |
Correct |
10 ms |
1868 KB |
Output is correct |
11 |
Correct |
47 ms |
7808 KB |
Output is correct |
12 |
Correct |
11 ms |
2472 KB |
Output is correct |
13 |
Correct |
22 ms |
4344 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
154 ms |
14436 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Execution timed out |
4075 ms |
18444 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
145 ms |
14496 KB |
Output is correct |
10 |
Correct |
10 ms |
1868 KB |
Output is correct |
11 |
Correct |
47 ms |
7808 KB |
Output is correct |
12 |
Correct |
11 ms |
2472 KB |
Output is correct |
13 |
Correct |
22 ms |
4344 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
154 ms |
14436 KB |
Output is correct |
17 |
Correct |
593 ms |
28732 KB |
Output is correct |
18 |
Correct |
593 ms |
28704 KB |
Output is correct |
19 |
Execution timed out |
4053 ms |
19396 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
145 ms |
14496 KB |
Output is correct |
10 |
Correct |
10 ms |
1868 KB |
Output is correct |
11 |
Correct |
47 ms |
7808 KB |
Output is correct |
12 |
Correct |
11 ms |
2472 KB |
Output is correct |
13 |
Correct |
22 ms |
4344 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
154 ms |
14436 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
506 ms |
28708 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
3 ms |
516 KB |
Output is correct |
27 |
Correct |
3 ms |
588 KB |
Output is correct |
28 |
Correct |
109 ms |
11316 KB |
Output is correct |
29 |
Correct |
185 ms |
16752 KB |
Output is correct |
30 |
Correct |
369 ms |
22460 KB |
Output is correct |
31 |
Correct |
398 ms |
28788 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
2 ms |
460 KB |
Output is correct |
45 |
Correct |
130 ms |
14424 KB |
Output is correct |
46 |
Correct |
321 ms |
20612 KB |
Output is correct |
47 |
Correct |
293 ms |
20608 KB |
Output is correct |
48 |
Correct |
1 ms |
244 KB |
Output is correct |
49 |
Correct |
1 ms |
204 KB |
Output is correct |
50 |
Correct |
1 ms |
204 KB |
Output is correct |
51 |
Correct |
1 ms |
204 KB |
Output is correct |
52 |
Correct |
1 ms |
204 KB |
Output is correct |
53 |
Correct |
1 ms |
204 KB |
Output is correct |
54 |
Correct |
1 ms |
204 KB |
Output is correct |
55 |
Correct |
533 ms |
28772 KB |
Output is correct |
56 |
Correct |
1 ms |
204 KB |
Output is correct |
57 |
Correct |
3 ms |
528 KB |
Output is correct |
58 |
Correct |
6 ms |
1248 KB |
Output is correct |
59 |
Correct |
7 ms |
1100 KB |
Output is correct |
60 |
Correct |
191 ms |
14528 KB |
Output is correct |
61 |
Correct |
308 ms |
19528 KB |
Output is correct |
62 |
Correct |
403 ms |
23056 KB |
Output is correct |
63 |
Correct |
533 ms |
28812 KB |
Output is correct |
64 |
Correct |
1 ms |
204 KB |
Output is correct |
65 |
Correct |
1 ms |
204 KB |
Output is correct |
66 |
Correct |
1 ms |
204 KB |
Output is correct |
67 |
Correct |
493 ms |
28748 KB |
Output is correct |
68 |
Correct |
419 ms |
28796 KB |
Output is correct |
69 |
Correct |
536 ms |
28656 KB |
Output is correct |
70 |
Correct |
2 ms |
588 KB |
Output is correct |
71 |
Correct |
3 ms |
844 KB |
Output is correct |
72 |
Correct |
132 ms |
14464 KB |
Output is correct |
73 |
Correct |
396 ms |
21224 KB |
Output is correct |
74 |
Correct |
413 ms |
28756 KB |
Output is correct |
75 |
Correct |
474 ms |
30208 KB |
Output is correct |
76 |
Correct |
491 ms |
28876 KB |
Output is correct |
77 |
Correct |
3 ms |
588 KB |
Output is correct |
78 |
Correct |
5 ms |
1032 KB |
Output is correct |
79 |
Correct |
189 ms |
14840 KB |
Output is correct |
80 |
Correct |
455 ms |
21816 KB |
Output is correct |
81 |
Correct |
441 ms |
29552 KB |
Output is correct |
82 |
Correct |
1 ms |
204 KB |
Output is correct |
83 |
Correct |
1 ms |
204 KB |
Output is correct |
84 |
Correct |
1 ms |
204 KB |
Output is correct |
85 |
Execution timed out |
4075 ms |
18444 KB |
Time limit exceeded |
86 |
Halted |
0 ms |
0 KB |
- |