//Challenge: Accepted
#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 200005
#define pii pair<ll, ll>
#define ff first
#define ss second
vector<pii> xv[maxn], yv[maxn];
unordered_map<ll, vector<pii> > adj;
unordered_map<ll, bool> vis;
ll h(int x, int y) {
return (ll)x * maxn + y;
}
struct DSU{
int par[maxn];
int merges = 0;
void init(int n) {
for (int i = 0;i < n;i++) par[i] = i;
}
int find(int a) {
return a == par[a] ? a : (par[a] = find(par[a]));
}
void Union(int a, int b) {
a = find(a), b = find(b);
if (a == b) return;
merges++;
par[a] = b;
}
} dsu;
vector<int> u, v, a, b;
void dfs(ll n, ll prv) {
vis[n] = 1;
for (auto p:adj[n]) {
if (p.ff != prv) {
u.push_back(p.ss / maxn);
v.push_back(p.ss % maxn);
a.push_back(n / maxn);
b.push_back(n % maxn);
if (!vis[p.ff]) dfs(p.ff, n);
}
}
}
int construct_roads(std::vector<int> x, std::vector<int> y) {
int n = x.size();
dsu.init(n);
for (int i = 0;i < n;i++) {
xv[x[i]].push_back({y[i], i});
yv[y[i]].push_back({x[i], i});
}
for (int i = 0;i < maxn;i += 2) {
sort(xv[i].begin(), xv[i].end());
sort(yv[i].begin(), yv[i].end());
for (int j = 0;j < (int)xv[i].size() - 1;j++) {
if (xv[i][j].ff + 2 == xv[i][j+1].ff) {
dsu.Union(xv[i][j].ss, xv[i][j+1].ss);
ll ind = h(xv[i][j].ss, xv[i][j+1].ss), v1 = h(i-1, xv[i][j].ff + 1), v2 = h(i+1, xv[i][j].ff + 1);
debug(v1, v2);
adj[v1].push_back({v2, ind});
adj[v2].push_back({v1, ind});
}
}
for (int j = 0;j < (int)yv[i].size() - 1;j++) {
if (yv[i][j].ff + 2 == yv[i][j+1].ff) {
dsu.Union(yv[i][j].ss, yv[i][j+1].ss);
ll ind = h(yv[i][j].ss, yv[i][j+1].ss), v1 = h(yv[i][j].ff + 1, i-1), v2 = h(yv[i][j].ff + 1, i+1);
adj[v1].push_back({v2, ind});
adj[v2].push_back({v1, ind});
}
}
}
if (dsu.merges != n - 1) return 0;
for (auto i:adj) {
if (i.ss.size() == 1 && !vis[i.ff]) dfs(i.ff, -1);
}
for (auto i:adj) {
if (!vis[i.ff]) dfs(i.ff, i.ss[0].ff);
}
build(u, v, a, b);
return 1;
}
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:13:20: warning: statement has no effect [-Wunused-value]
13 | #define debug(...) 0
| ^
parks.cpp:72:5: note: in expansion of macro 'debug'
72 | debug(v1, v2);
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
7 ms |
9684 KB |
Output is correct |
4 |
Correct |
8 ms |
9696 KB |
Output is correct |
5 |
Correct |
7 ms |
9684 KB |
Output is correct |
6 |
Correct |
8 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9700 KB |
Output is correct |
9 |
Correct |
158 ms |
50784 KB |
Output is correct |
10 |
Correct |
18 ms |
13648 KB |
Output is correct |
11 |
Correct |
84 ms |
31360 KB |
Output is correct |
12 |
Correct |
27 ms |
15708 KB |
Output is correct |
13 |
Correct |
41 ms |
21616 KB |
Output is correct |
14 |
Correct |
8 ms |
9940 KB |
Output is correct |
15 |
Correct |
8 ms |
10096 KB |
Output is correct |
16 |
Correct |
159 ms |
50692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
7 ms |
9684 KB |
Output is correct |
4 |
Correct |
8 ms |
9696 KB |
Output is correct |
5 |
Correct |
7 ms |
9684 KB |
Output is correct |
6 |
Correct |
8 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9700 KB |
Output is correct |
9 |
Correct |
158 ms |
50784 KB |
Output is correct |
10 |
Correct |
18 ms |
13648 KB |
Output is correct |
11 |
Correct |
84 ms |
31360 KB |
Output is correct |
12 |
Correct |
27 ms |
15708 KB |
Output is correct |
13 |
Correct |
41 ms |
21616 KB |
Output is correct |
14 |
Correct |
8 ms |
9940 KB |
Output is correct |
15 |
Correct |
8 ms |
10096 KB |
Output is correct |
16 |
Correct |
159 ms |
50692 KB |
Output is correct |
17 |
Incorrect |
8 ms |
9684 KB |
Tree @(3, 3) appears more than once: for edges on positions 1 and 2 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
7 ms |
9684 KB |
Output is correct |
4 |
Correct |
8 ms |
9696 KB |
Output is correct |
5 |
Correct |
7 ms |
9684 KB |
Output is correct |
6 |
Correct |
8 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9700 KB |
Output is correct |
9 |
Correct |
158 ms |
50784 KB |
Output is correct |
10 |
Correct |
18 ms |
13648 KB |
Output is correct |
11 |
Correct |
84 ms |
31360 KB |
Output is correct |
12 |
Correct |
27 ms |
15708 KB |
Output is correct |
13 |
Correct |
41 ms |
21616 KB |
Output is correct |
14 |
Correct |
8 ms |
9940 KB |
Output is correct |
15 |
Correct |
8 ms |
10096 KB |
Output is correct |
16 |
Correct |
159 ms |
50692 KB |
Output is correct |
17 |
Incorrect |
8 ms |
9684 KB |
Tree @(3, 3) appears more than once: for edges on positions 1 and 2 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
7 ms |
9684 KB |
Output is correct |
4 |
Correct |
8 ms |
9696 KB |
Output is correct |
5 |
Correct |
7 ms |
9684 KB |
Output is correct |
6 |
Correct |
8 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9700 KB |
Output is correct |
9 |
Correct |
158 ms |
50784 KB |
Output is correct |
10 |
Correct |
18 ms |
13648 KB |
Output is correct |
11 |
Correct |
84 ms |
31360 KB |
Output is correct |
12 |
Correct |
27 ms |
15708 KB |
Output is correct |
13 |
Correct |
41 ms |
21616 KB |
Output is correct |
14 |
Correct |
8 ms |
9940 KB |
Output is correct |
15 |
Correct |
8 ms |
10096 KB |
Output is correct |
16 |
Correct |
159 ms |
50692 KB |
Output is correct |
17 |
Correct |
9 ms |
9596 KB |
Output is correct |
18 |
Correct |
7 ms |
9684 KB |
Output is correct |
19 |
Correct |
7 ms |
9684 KB |
Output is correct |
20 |
Correct |
554 ms |
92772 KB |
Output is correct |
21 |
Correct |
423 ms |
79052 KB |
Output is correct |
22 |
Correct |
484 ms |
79496 KB |
Output is correct |
23 |
Correct |
401 ms |
76136 KB |
Output is correct |
24 |
Correct |
66 ms |
23348 KB |
Output is correct |
25 |
Correct |
176 ms |
59240 KB |
Output is correct |
26 |
Correct |
153 ms |
59240 KB |
Output is correct |
27 |
Correct |
312 ms |
90120 KB |
Output is correct |
28 |
Correct |
336 ms |
90124 KB |
Output is correct |
29 |
Correct |
366 ms |
90164 KB |
Output is correct |
30 |
Correct |
336 ms |
90380 KB |
Output is correct |
31 |
Correct |
7 ms |
9684 KB |
Output is correct |
32 |
Correct |
27 ms |
14216 KB |
Output is correct |
33 |
Correct |
66 ms |
19048 KB |
Output is correct |
34 |
Correct |
344 ms |
92280 KB |
Output is correct |
35 |
Correct |
13 ms |
11820 KB |
Output is correct |
36 |
Correct |
54 ms |
20080 KB |
Output is correct |
37 |
Correct |
111 ms |
30200 KB |
Output is correct |
38 |
Correct |
181 ms |
35272 KB |
Output is correct |
39 |
Correct |
258 ms |
44208 KB |
Output is correct |
40 |
Correct |
356 ms |
54072 KB |
Output is correct |
41 |
Correct |
423 ms |
64072 KB |
Output is correct |
42 |
Correct |
536 ms |
73392 KB |
Output is correct |
43 |
Correct |
6 ms |
9684 KB |
Output is correct |
44 |
Correct |
6 ms |
9684 KB |
Output is correct |
45 |
Correct |
6 ms |
9684 KB |
Output is correct |
46 |
Correct |
6 ms |
9700 KB |
Output is correct |
47 |
Correct |
6 ms |
9684 KB |
Output is correct |
48 |
Correct |
6 ms |
9684 KB |
Output is correct |
49 |
Correct |
6 ms |
9684 KB |
Output is correct |
50 |
Correct |
6 ms |
9684 KB |
Output is correct |
51 |
Correct |
6 ms |
9704 KB |
Output is correct |
52 |
Correct |
6 ms |
9696 KB |
Output is correct |
53 |
Correct |
7 ms |
9692 KB |
Output is correct |
54 |
Correct |
7 ms |
10096 KB |
Output is correct |
55 |
Correct |
8 ms |
10352 KB |
Output is correct |
56 |
Correct |
182 ms |
41600 KB |
Output is correct |
57 |
Correct |
341 ms |
58740 KB |
Output is correct |
58 |
Correct |
338 ms |
58752 KB |
Output is correct |
59 |
Correct |
6 ms |
9684 KB |
Output is correct |
60 |
Correct |
7 ms |
9684 KB |
Output is correct |
61 |
Correct |
6 ms |
9684 KB |
Output is correct |
62 |
Correct |
296 ms |
91120 KB |
Output is correct |
63 |
Correct |
307 ms |
92868 KB |
Output is correct |
64 |
Correct |
303 ms |
91664 KB |
Output is correct |
65 |
Correct |
9 ms |
10580 KB |
Output is correct |
66 |
Correct |
12 ms |
11504 KB |
Output is correct |
67 |
Correct |
196 ms |
40960 KB |
Output is correct |
68 |
Correct |
337 ms |
59624 KB |
Output is correct |
69 |
Correct |
413 ms |
72936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
7 ms |
9684 KB |
Output is correct |
4 |
Correct |
8 ms |
9696 KB |
Output is correct |
5 |
Correct |
7 ms |
9684 KB |
Output is correct |
6 |
Correct |
8 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9700 KB |
Output is correct |
9 |
Correct |
158 ms |
50784 KB |
Output is correct |
10 |
Correct |
18 ms |
13648 KB |
Output is correct |
11 |
Correct |
84 ms |
31360 KB |
Output is correct |
12 |
Correct |
27 ms |
15708 KB |
Output is correct |
13 |
Correct |
41 ms |
21616 KB |
Output is correct |
14 |
Correct |
8 ms |
9940 KB |
Output is correct |
15 |
Correct |
8 ms |
10096 KB |
Output is correct |
16 |
Correct |
159 ms |
50692 KB |
Output is correct |
17 |
Correct |
514 ms |
93448 KB |
Output is correct |
18 |
Correct |
538 ms |
93484 KB |
Output is correct |
19 |
Correct |
475 ms |
89724 KB |
Output is correct |
20 |
Correct |
354 ms |
75660 KB |
Output is correct |
21 |
Correct |
396 ms |
76272 KB |
Output is correct |
22 |
Correct |
7 ms |
9684 KB |
Output is correct |
23 |
Correct |
66 ms |
20548 KB |
Output is correct |
24 |
Correct |
23 ms |
14512 KB |
Output is correct |
25 |
Correct |
81 ms |
25832 KB |
Output is correct |
26 |
Correct |
130 ms |
37644 KB |
Output is correct |
27 |
Correct |
232 ms |
44804 KB |
Output is correct |
28 |
Correct |
308 ms |
53616 KB |
Output is correct |
29 |
Correct |
358 ms |
64484 KB |
Output is correct |
30 |
Correct |
481 ms |
70916 KB |
Output is correct |
31 |
Correct |
509 ms |
79176 KB |
Output is correct |
32 |
Correct |
352 ms |
82744 KB |
Output is correct |
33 |
Correct |
303 ms |
92596 KB |
Output is correct |
34 |
Correct |
10 ms |
10844 KB |
Output is correct |
35 |
Correct |
14 ms |
11812 KB |
Output is correct |
36 |
Correct |
227 ms |
42796 KB |
Output is correct |
37 |
Correct |
314 ms |
61972 KB |
Output is correct |
38 |
Correct |
446 ms |
76792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
7 ms |
9684 KB |
Output is correct |
4 |
Correct |
8 ms |
9696 KB |
Output is correct |
5 |
Correct |
7 ms |
9684 KB |
Output is correct |
6 |
Correct |
8 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9700 KB |
Output is correct |
9 |
Correct |
158 ms |
50784 KB |
Output is correct |
10 |
Correct |
18 ms |
13648 KB |
Output is correct |
11 |
Correct |
84 ms |
31360 KB |
Output is correct |
12 |
Correct |
27 ms |
15708 KB |
Output is correct |
13 |
Correct |
41 ms |
21616 KB |
Output is correct |
14 |
Correct |
8 ms |
9940 KB |
Output is correct |
15 |
Correct |
8 ms |
10096 KB |
Output is correct |
16 |
Correct |
159 ms |
50692 KB |
Output is correct |
17 |
Incorrect |
8 ms |
9684 KB |
Tree @(3, 3) appears more than once: for edges on positions 1 and 2 |
18 |
Halted |
0 ms |
0 KB |
- |