#include<bits/stdc++.h>
#include "parks.h"
#pragma GCC optimize("O3,unroll-loops")
using namespace std;
using ll = int;
ll n, m, q, k, x, y, a, b, c;
unordered_map< long long, vector< pair< pair<ll, ll>, pair<ll, ll> > > > G;
vector< pair< pair<ll, ll>, pair<ll, ll> > > prs;
struct dsu {
ll n;
vector<ll> par;
vector<ll> sz;
dsu(ll N) : n(N) {
par.resize(n);
sz.resize(n);
for (int i = 0; i < n; i++) {
par[i] = i;
sz[i] = 1;
}
}
ll find_p(ll x) {
if (par[x] == x) {
return x;
}
ll p = find_p(par[x]);
par[x] = p;
return p;
}
void join(ll x, ll y) {
x = find_p(x);
y = find_p(y);
if (x == y) {
return;
}
if (sz[y] > sz[x]) {
swap(x, y);
}
par[y] = x;
sz[x] += sz[y];
}
};
long long hsh(pair<ll, ll> p) {
return p.first * 10000000LL + p.second;
}
long long hsh(pair< pair<ll, ll>, pair<ll, ll> > p) {
return (p.first.first * 10000000LL + p.first.second) + (10000000LL * 10000000LL * (p.first > p.second));
}
void dfs(pair< pair<ll, ll>, pair<ll, ll> > x, unordered_set<long long> &mp, vector< pair< pair<ll, ll>, pair<ll, ll> > > &mp1) {
// cout << x.first.first << ' ';
// cout << x.first.second << '\t';
// cout << x.second.first << ' ';
// cout << x.second.second << '\n';
mp.insert(hsh(x));
mp1.push_back(x);
for (auto i : G[hsh(x)]) {
if (!mp.count(hsh(i.first))) {
dfs(i, mp, mp1);
}
}
}
int construct_roads(vector<int> x, vector<int> y) {
n = x.size();
array< vector<int>, 4 > ans;
map< pair<ll, ll>, ll > pts;
for (int i = 0; i < n; i++) {
pts[{x[i], y[i]}] = i;
}
dsu d(n);
map< pair<ll, ll>, vector< pair<ll, ll> > > vec;
for (int i = 0; i < n; i++) {
if (pts.count({x[i], y[i] - 2}) && d.find_p(i) != d.find_p(pts[{x[i], y[i] - 2}])) {
d.join(i, pts[{x[i], y[i] - 2}]);
vec[{x[i] - 1, y[i] - 1}].push_back({x[i], y[i] - 1});
vec[{x[i] + 1, y[i] - 1}].push_back({x[i], y[i] - 1});
}
if (pts.count({x[i], y[i] + 2}) && d.find_p(i) != d.find_p(pts[{x[i], y[i] + 2}])) {
d.join(i, pts[{x[i], y[i] + 2}]);
vec[{x[i] - 1, y[i] + 1}].push_back({x[i], y[i] + 1});
vec[{x[i] + 1, y[i] + 1}].push_back({x[i], y[i] + 1});
}
}
for (int i = 0; i < n; i++) {
if (pts.count({x[i] - 2, y[i]}) && d.find_p(i) != d.find_p(pts[{x[i] - 2, y[i]}])) {
d.join(i, pts[{x[i] - 2, y[i]}]);
vec[{x[i] - 1, y[i] + 1}].push_back({x[i] - 1, y[i]});
vec[{x[i] - 1, y[i] - 1}].push_back({x[i] - 1, y[i]});
}
if (pts.count({x[i] + 2, y[i]}) && d.find_p(i) != d.find_p(pts[{x[i] + 2, y[i]}])) {
d.join(i, pts[{x[i] + 2, y[i]}]);
vec[{x[i] + 1, y[i] + 1}].push_back({x[i] + 1, y[i]});
vec[{x[i] + 1, y[i] - 1}].push_back({x[i] + 1, y[i]});
}
}
if (d.sz[d.find_p(0)] != n) {
return 0;
}
for (auto &i : vec) {
// cout << i.first.first << ' ' << i.first.second << '\n';
for (auto j : i.second) {
prs.push_back({j, i.first});
// cout << '\t' << j.first << ' ' << j.second << '\n';
for (auto k : i.second) {
if (j != k) {
G[hsh({j, i.first})].push_back({k, {k.first + (k.first - i.first.first), k.second + (k.second - i.first.second)}});
// cout << "\t\t" << k.first + (k.first - i.first.first) << ' ' << k.second + (k.second - i.first.second) << '\n';
}
}
}
}
unordered_set<long long> mp;
for (auto i : prs) {
// cout << i.first.first.first << ' ' << i.first.first.second << '\n';
if (mp.count(hsh(i.first))) {
continue;
}
unordered_set<long long> mp2;
vector< pair< pair<ll, ll>, pair<ll, ll> > > mp1;
dfs(i, mp2, mp1);
for (auto j : mp1) {
if (mp.count(hsh(j.first))) {
continue;
}
if (j.first.first % 2 == 0) {
ans[0].push_back(pts[{j.first.first, j.first.second - 1}]);
ans[1].push_back(pts[{j.first.first, j.first.second + 1}]);
}
else {
ans[0].push_back(pts[{j.first.first - 1, j.first.second}]);
ans[1].push_back(pts[{j.first.first + 1, j.first.second}]);
}
ans[2].push_back(j.second.first);
ans[3].push_back(j.second.second);
mp.insert(hsh(j.first));
}
}
build(ans[0], ans[1], ans[2], ans[3]);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
311 ms |
50052 KB |
Output is correct |
10 |
Correct |
19 ms |
5336 KB |
Output is correct |
11 |
Correct |
103 ms |
26748 KB |
Output is correct |
12 |
Correct |
25 ms |
7880 KB |
Output is correct |
13 |
Correct |
56 ms |
13908 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
329 ms |
51052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
311 ms |
50052 KB |
Output is correct |
10 |
Correct |
19 ms |
5336 KB |
Output is correct |
11 |
Correct |
103 ms |
26748 KB |
Output is correct |
12 |
Correct |
25 ms |
7880 KB |
Output is correct |
13 |
Correct |
56 ms |
13908 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
329 ms |
51052 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
895 ms |
101192 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
944 KB |
Output is correct |
26 |
Correct |
4 ms |
1116 KB |
Output is correct |
27 |
Correct |
6 ms |
1420 KB |
Output is correct |
28 |
Correct |
291 ms |
41376 KB |
Output is correct |
29 |
Correct |
478 ms |
60508 KB |
Output is correct |
30 |
Correct |
660 ms |
81548 KB |
Output is correct |
31 |
Correct |
924 ms |
101200 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
0 ms |
344 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
4 ms |
904 KB |
Output is correct |
44 |
Correct |
4 ms |
1116 KB |
Output is correct |
45 |
Correct |
375 ms |
50368 KB |
Output is correct |
46 |
Correct |
647 ms |
75984 KB |
Output is correct |
47 |
Correct |
645 ms |
75728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
311 ms |
50052 KB |
Output is correct |
10 |
Correct |
19 ms |
5336 KB |
Output is correct |
11 |
Correct |
103 ms |
26748 KB |
Output is correct |
12 |
Correct |
25 ms |
7880 KB |
Output is correct |
13 |
Correct |
56 ms |
13908 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
329 ms |
51052 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
895 ms |
101192 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
944 KB |
Output is correct |
26 |
Correct |
4 ms |
1116 KB |
Output is correct |
27 |
Correct |
6 ms |
1420 KB |
Output is correct |
28 |
Correct |
291 ms |
41376 KB |
Output is correct |
29 |
Correct |
478 ms |
60508 KB |
Output is correct |
30 |
Correct |
660 ms |
81548 KB |
Output is correct |
31 |
Correct |
924 ms |
101200 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
0 ms |
344 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
4 ms |
904 KB |
Output is correct |
44 |
Correct |
4 ms |
1116 KB |
Output is correct |
45 |
Correct |
375 ms |
50368 KB |
Output is correct |
46 |
Correct |
647 ms |
75984 KB |
Output is correct |
47 |
Correct |
645 ms |
75728 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
364 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
344 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
1077 ms |
100096 KB |
Output is correct |
56 |
Correct |
1 ms |
344 KB |
Output is correct |
57 |
Correct |
4 ms |
1116 KB |
Output is correct |
58 |
Correct |
15 ms |
3308 KB |
Output is correct |
59 |
Correct |
17 ms |
2652 KB |
Output is correct |
60 |
Correct |
432 ms |
49844 KB |
Output is correct |
61 |
Correct |
602 ms |
70388 KB |
Output is correct |
62 |
Correct |
820 ms |
83856 KB |
Output is correct |
63 |
Correct |
940 ms |
101508 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
0 ms |
344 KB |
Output is correct |
67 |
Correct |
851 ms |
101152 KB |
Output is correct |
68 |
Correct |
945 ms |
100968 KB |
Output is correct |
69 |
Correct |
962 ms |
100692 KB |
Output is correct |
70 |
Correct |
7 ms |
1116 KB |
Output is correct |
71 |
Correct |
15 ms |
2256 KB |
Output is correct |
72 |
Correct |
459 ms |
51456 KB |
Output is correct |
73 |
Correct |
769 ms |
77404 KB |
Output is correct |
74 |
Correct |
1102 ms |
100720 KB |
Output is correct |
75 |
Correct |
960 ms |
102772 KB |
Output is correct |
76 |
Correct |
917 ms |
100820 KB |
Output is correct |
77 |
Correct |
7 ms |
1624 KB |
Output is correct |
78 |
Correct |
15 ms |
2396 KB |
Output is correct |
79 |
Correct |
430 ms |
49980 KB |
Output is correct |
80 |
Correct |
787 ms |
77192 KB |
Output is correct |
81 |
Correct |
1136 ms |
101488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
311 ms |
50052 KB |
Output is correct |
10 |
Correct |
19 ms |
5336 KB |
Output is correct |
11 |
Correct |
103 ms |
26748 KB |
Output is correct |
12 |
Correct |
25 ms |
7880 KB |
Output is correct |
13 |
Correct |
56 ms |
13908 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
329 ms |
51052 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Execution timed out |
3594 ms |
85700 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
311 ms |
50052 KB |
Output is correct |
10 |
Correct |
19 ms |
5336 KB |
Output is correct |
11 |
Correct |
103 ms |
26748 KB |
Output is correct |
12 |
Correct |
25 ms |
7880 KB |
Output is correct |
13 |
Correct |
56 ms |
13908 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
329 ms |
51052 KB |
Output is correct |
17 |
Correct |
879 ms |
101964 KB |
Output is correct |
18 |
Correct |
855 ms |
101376 KB |
Output is correct |
19 |
Execution timed out |
3517 ms |
114384 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
311 ms |
50052 KB |
Output is correct |
10 |
Correct |
19 ms |
5336 KB |
Output is correct |
11 |
Correct |
103 ms |
26748 KB |
Output is correct |
12 |
Correct |
25 ms |
7880 KB |
Output is correct |
13 |
Correct |
56 ms |
13908 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
329 ms |
51052 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
895 ms |
101192 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
944 KB |
Output is correct |
26 |
Correct |
4 ms |
1116 KB |
Output is correct |
27 |
Correct |
6 ms |
1420 KB |
Output is correct |
28 |
Correct |
291 ms |
41376 KB |
Output is correct |
29 |
Correct |
478 ms |
60508 KB |
Output is correct |
30 |
Correct |
660 ms |
81548 KB |
Output is correct |
31 |
Correct |
924 ms |
101200 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
0 ms |
344 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
4 ms |
904 KB |
Output is correct |
44 |
Correct |
4 ms |
1116 KB |
Output is correct |
45 |
Correct |
375 ms |
50368 KB |
Output is correct |
46 |
Correct |
647 ms |
75984 KB |
Output is correct |
47 |
Correct |
645 ms |
75728 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
364 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
344 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
1077 ms |
100096 KB |
Output is correct |
56 |
Correct |
1 ms |
344 KB |
Output is correct |
57 |
Correct |
4 ms |
1116 KB |
Output is correct |
58 |
Correct |
15 ms |
3308 KB |
Output is correct |
59 |
Correct |
17 ms |
2652 KB |
Output is correct |
60 |
Correct |
432 ms |
49844 KB |
Output is correct |
61 |
Correct |
602 ms |
70388 KB |
Output is correct |
62 |
Correct |
820 ms |
83856 KB |
Output is correct |
63 |
Correct |
940 ms |
101508 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
0 ms |
344 KB |
Output is correct |
67 |
Correct |
851 ms |
101152 KB |
Output is correct |
68 |
Correct |
945 ms |
100968 KB |
Output is correct |
69 |
Correct |
962 ms |
100692 KB |
Output is correct |
70 |
Correct |
7 ms |
1116 KB |
Output is correct |
71 |
Correct |
15 ms |
2256 KB |
Output is correct |
72 |
Correct |
459 ms |
51456 KB |
Output is correct |
73 |
Correct |
769 ms |
77404 KB |
Output is correct |
74 |
Correct |
1102 ms |
100720 KB |
Output is correct |
75 |
Correct |
960 ms |
102772 KB |
Output is correct |
76 |
Correct |
917 ms |
100820 KB |
Output is correct |
77 |
Correct |
7 ms |
1624 KB |
Output is correct |
78 |
Correct |
15 ms |
2396 KB |
Output is correct |
79 |
Correct |
430 ms |
49980 KB |
Output is correct |
80 |
Correct |
787 ms |
77192 KB |
Output is correct |
81 |
Correct |
1136 ms |
101488 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Execution timed out |
3594 ms |
85700 KB |
Time limit exceeded |
86 |
Halted |
0 ms |
0 KB |
- |