#include "parks.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int> p;
int findp(int a){
if (p[a] == a) return a;
else return p[a] = findp(p[a]);
}
void merge(int a, int b){
a = findp(a);
b = findp(b);
if (a == b) return;
p[a] = b;
}
int construct_roads(vector<int> x, vector<int> y) {
int n = x.size();
int mx = 0;
unordered_map<int,vector<pair<int,int>>> r, c;
for (int i=0; i<n; i++){
mx = max(mx, x[i]);
r[y[i]].push_back({x[i], i});
c[x[i]].push_back({y[i], i});
}
vector<pair<int,int>> el;
//cout << "a\n";
for (int i=2; i<=200000; i++){
if (c[i].size() > 1){
sort(c[i].begin(), c[i].end());
for (int j=0; j<c[i].size()-1; j++){
if (c[i][j+1].first-c[i][j].first == 2) el.push_back({c[i][j].second, c[i][j+1].second});
}
}
if (r[i].size() > 1){
sort(r[i].begin(), r[i].end());
for (int j=0; j<r[i].size()-1; j++){
if (r[i][j+1].first-r[i][j].first == 2) el.push_back({r[i][j].second, r[i][j+1].second});
}
}
}
p.resize(n);
for (int i=0; i<n; i++) p[i] = i;
//cout << "b\n";
vector<int> u, v, a, b;
int done = 0;
int cur = -1;
unordered_set<ll> benches;
while (done < n-1){
cur++;
//cout << cur << " " << done << "\n";
if (cur == el.size()) return 0;
if (findp(el[cur].first) == findp(el[cur].second)) continue;
//cout << "a\n";
merge(el[cur].first, el[cur].second);
done++;
u.push_back(el[cur].first);
v.push_back(el[cur].second);
//cout << "b\n";
if (x[el[cur].first] == x[el[cur].second]){
b.push_back((y[el[cur].first]+y[el[cur].second])/2);
if (x[el[cur].first] == 2) a.push_back(1);
else if (x[el[cur].first] == mx) a.push_back(x[el[cur].first]+1);
else {
if (min(y[el[cur].first], y[el[cur].second]) % 4) a.push_back(x[el[cur].first]-1);
else a.push_back(x[el[cur].first]+1);
}
}
else {
a.push_back((x[el[cur].first]+x[el[cur].second])/2);
if (mx == 4) b.push_back(y[el[cur].first]-1);
else {
int q = y[el[cur].first]+1, r = y[el[cur].first]-1;
if (benches.find((ll) a.back()*100000+ (ll) q) != benches.end() && benches.find((ll) a.back()*100000+ (ll) r) != benches.end()) return 0;
if (benches.find((ll) a.back()*100000+ (ll) q) != benches.end()) b.push_back(r);
else if (benches.find((ll) a.back()*100000+ (ll) r) != benches.end()) b.push_back(q);
else if (min(x[el[cur].first], x[el[cur].second]) % 4) b.push_back(q);
else b.push_back(r);
}
}
benches.insert((ll) a.back()*100000+ (ll) b.back());
//cout << "c\n";
}
//cout << "c\n";
build(u, v, a, b);
return 1;
}
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:35:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (int j=0; j<c[i].size()-1; j++){
| ~^~~~~~~~~~~~~~
parks.cpp:41:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for (int j=0; j<r[i].size()-1; j++){
| ~^~~~~~~~~~~~~~
parks.cpp:56:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | if (cur == el.size()) return 0;
| ~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
24496 KB |
Output is correct |
2 |
Correct |
24 ms |
24500 KB |
Output is correct |
3 |
Correct |
28 ms |
24508 KB |
Output is correct |
4 |
Correct |
26 ms |
24492 KB |
Output is correct |
5 |
Correct |
22 ms |
24500 KB |
Output is correct |
6 |
Correct |
23 ms |
24560 KB |
Output is correct |
7 |
Correct |
24 ms |
24496 KB |
Output is correct |
8 |
Correct |
25 ms |
24684 KB |
Output is correct |
9 |
Correct |
80 ms |
40876 KB |
Output is correct |
10 |
Correct |
29 ms |
26072 KB |
Output is correct |
11 |
Correct |
44 ms |
33052 KB |
Output is correct |
12 |
Correct |
29 ms |
26900 KB |
Output is correct |
13 |
Correct |
35 ms |
30692 KB |
Output is correct |
14 |
Correct |
23 ms |
24756 KB |
Output is correct |
15 |
Correct |
25 ms |
24756 KB |
Output is correct |
16 |
Correct |
87 ms |
40876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
24496 KB |
Output is correct |
2 |
Correct |
24 ms |
24500 KB |
Output is correct |
3 |
Correct |
28 ms |
24508 KB |
Output is correct |
4 |
Correct |
26 ms |
24492 KB |
Output is correct |
5 |
Correct |
22 ms |
24500 KB |
Output is correct |
6 |
Correct |
23 ms |
24560 KB |
Output is correct |
7 |
Correct |
24 ms |
24496 KB |
Output is correct |
8 |
Correct |
25 ms |
24684 KB |
Output is correct |
9 |
Correct |
80 ms |
40876 KB |
Output is correct |
10 |
Correct |
29 ms |
26072 KB |
Output is correct |
11 |
Correct |
44 ms |
33052 KB |
Output is correct |
12 |
Correct |
29 ms |
26900 KB |
Output is correct |
13 |
Correct |
35 ms |
30692 KB |
Output is correct |
14 |
Correct |
23 ms |
24756 KB |
Output is correct |
15 |
Correct |
25 ms |
24756 KB |
Output is correct |
16 |
Correct |
87 ms |
40876 KB |
Output is correct |
17 |
Correct |
25 ms |
24496 KB |
Output is correct |
18 |
Correct |
22 ms |
24496 KB |
Output is correct |
19 |
Correct |
23 ms |
24500 KB |
Output is correct |
20 |
Correct |
27 ms |
24488 KB |
Output is correct |
21 |
Correct |
24 ms |
24500 KB |
Output is correct |
22 |
Correct |
26 ms |
24500 KB |
Output is correct |
23 |
Correct |
173 ms |
55828 KB |
Output is correct |
24 |
Correct |
23 ms |
24500 KB |
Output is correct |
25 |
Correct |
24 ms |
24756 KB |
Output is correct |
26 |
Correct |
32 ms |
24972 KB |
Output is correct |
27 |
Correct |
30 ms |
25012 KB |
Output is correct |
28 |
Correct |
76 ms |
36616 KB |
Output is correct |
29 |
Correct |
116 ms |
42564 KB |
Output is correct |
30 |
Correct |
129 ms |
48552 KB |
Output is correct |
31 |
Correct |
176 ms |
56596 KB |
Output is correct |
32 |
Correct |
23 ms |
24496 KB |
Output is correct |
33 |
Correct |
23 ms |
24500 KB |
Output is correct |
34 |
Correct |
24 ms |
24588 KB |
Output is correct |
35 |
Correct |
24 ms |
24500 KB |
Output is correct |
36 |
Correct |
23 ms |
24732 KB |
Output is correct |
37 |
Correct |
24 ms |
24500 KB |
Output is correct |
38 |
Correct |
24 ms |
24488 KB |
Output is correct |
39 |
Correct |
26 ms |
24496 KB |
Output is correct |
40 |
Correct |
24 ms |
24500 KB |
Output is correct |
41 |
Correct |
25 ms |
24492 KB |
Output is correct |
42 |
Correct |
22 ms |
24500 KB |
Output is correct |
43 |
Correct |
27 ms |
24756 KB |
Output is correct |
44 |
Correct |
27 ms |
25008 KB |
Output is correct |
45 |
Correct |
93 ms |
39824 KB |
Output is correct |
46 |
Correct |
138 ms |
46620 KB |
Output is correct |
47 |
Correct |
137 ms |
46616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
24496 KB |
Output is correct |
2 |
Correct |
24 ms |
24500 KB |
Output is correct |
3 |
Correct |
28 ms |
24508 KB |
Output is correct |
4 |
Correct |
26 ms |
24492 KB |
Output is correct |
5 |
Correct |
22 ms |
24500 KB |
Output is correct |
6 |
Correct |
23 ms |
24560 KB |
Output is correct |
7 |
Correct |
24 ms |
24496 KB |
Output is correct |
8 |
Correct |
25 ms |
24684 KB |
Output is correct |
9 |
Correct |
80 ms |
40876 KB |
Output is correct |
10 |
Correct |
29 ms |
26072 KB |
Output is correct |
11 |
Correct |
44 ms |
33052 KB |
Output is correct |
12 |
Correct |
29 ms |
26900 KB |
Output is correct |
13 |
Correct |
35 ms |
30692 KB |
Output is correct |
14 |
Correct |
23 ms |
24756 KB |
Output is correct |
15 |
Correct |
25 ms |
24756 KB |
Output is correct |
16 |
Correct |
87 ms |
40876 KB |
Output is correct |
17 |
Correct |
25 ms |
24496 KB |
Output is correct |
18 |
Correct |
22 ms |
24496 KB |
Output is correct |
19 |
Correct |
23 ms |
24500 KB |
Output is correct |
20 |
Correct |
27 ms |
24488 KB |
Output is correct |
21 |
Correct |
24 ms |
24500 KB |
Output is correct |
22 |
Correct |
26 ms |
24500 KB |
Output is correct |
23 |
Correct |
173 ms |
55828 KB |
Output is correct |
24 |
Correct |
23 ms |
24500 KB |
Output is correct |
25 |
Correct |
24 ms |
24756 KB |
Output is correct |
26 |
Correct |
32 ms |
24972 KB |
Output is correct |
27 |
Correct |
30 ms |
25012 KB |
Output is correct |
28 |
Correct |
76 ms |
36616 KB |
Output is correct |
29 |
Correct |
116 ms |
42564 KB |
Output is correct |
30 |
Correct |
129 ms |
48552 KB |
Output is correct |
31 |
Correct |
176 ms |
56596 KB |
Output is correct |
32 |
Correct |
23 ms |
24496 KB |
Output is correct |
33 |
Correct |
23 ms |
24500 KB |
Output is correct |
34 |
Correct |
24 ms |
24588 KB |
Output is correct |
35 |
Correct |
24 ms |
24500 KB |
Output is correct |
36 |
Correct |
23 ms |
24732 KB |
Output is correct |
37 |
Correct |
24 ms |
24500 KB |
Output is correct |
38 |
Correct |
24 ms |
24488 KB |
Output is correct |
39 |
Correct |
26 ms |
24496 KB |
Output is correct |
40 |
Correct |
24 ms |
24500 KB |
Output is correct |
41 |
Correct |
25 ms |
24492 KB |
Output is correct |
42 |
Correct |
22 ms |
24500 KB |
Output is correct |
43 |
Correct |
27 ms |
24756 KB |
Output is correct |
44 |
Correct |
27 ms |
25008 KB |
Output is correct |
45 |
Correct |
93 ms |
39824 KB |
Output is correct |
46 |
Correct |
138 ms |
46620 KB |
Output is correct |
47 |
Correct |
137 ms |
46616 KB |
Output is correct |
48 |
Correct |
27 ms |
24500 KB |
Output is correct |
49 |
Correct |
26 ms |
24496 KB |
Output is correct |
50 |
Incorrect |
27 ms |
24496 KB |
Tree @(3, 3) appears more than once: for edges on positions 1 and 3 |
51 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
24496 KB |
Output is correct |
2 |
Correct |
24 ms |
24500 KB |
Output is correct |
3 |
Correct |
28 ms |
24508 KB |
Output is correct |
4 |
Correct |
26 ms |
24492 KB |
Output is correct |
5 |
Correct |
22 ms |
24500 KB |
Output is correct |
6 |
Correct |
23 ms |
24560 KB |
Output is correct |
7 |
Correct |
24 ms |
24496 KB |
Output is correct |
8 |
Correct |
25 ms |
24684 KB |
Output is correct |
9 |
Correct |
80 ms |
40876 KB |
Output is correct |
10 |
Correct |
29 ms |
26072 KB |
Output is correct |
11 |
Correct |
44 ms |
33052 KB |
Output is correct |
12 |
Correct |
29 ms |
26900 KB |
Output is correct |
13 |
Correct |
35 ms |
30692 KB |
Output is correct |
14 |
Correct |
23 ms |
24756 KB |
Output is correct |
15 |
Correct |
25 ms |
24756 KB |
Output is correct |
16 |
Correct |
87 ms |
40876 KB |
Output is correct |
17 |
Correct |
26 ms |
24496 KB |
Output is correct |
18 |
Correct |
25 ms |
24500 KB |
Output is correct |
19 |
Correct |
33 ms |
24688 KB |
Output is correct |
20 |
Incorrect |
173 ms |
42652 KB |
Solution announced impossible, but it is possible. |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
24496 KB |
Output is correct |
2 |
Correct |
24 ms |
24500 KB |
Output is correct |
3 |
Correct |
28 ms |
24508 KB |
Output is correct |
4 |
Correct |
26 ms |
24492 KB |
Output is correct |
5 |
Correct |
22 ms |
24500 KB |
Output is correct |
6 |
Correct |
23 ms |
24560 KB |
Output is correct |
7 |
Correct |
24 ms |
24496 KB |
Output is correct |
8 |
Correct |
25 ms |
24684 KB |
Output is correct |
9 |
Correct |
80 ms |
40876 KB |
Output is correct |
10 |
Correct |
29 ms |
26072 KB |
Output is correct |
11 |
Correct |
44 ms |
33052 KB |
Output is correct |
12 |
Correct |
29 ms |
26900 KB |
Output is correct |
13 |
Correct |
35 ms |
30692 KB |
Output is correct |
14 |
Correct |
23 ms |
24756 KB |
Output is correct |
15 |
Correct |
25 ms |
24756 KB |
Output is correct |
16 |
Correct |
87 ms |
40876 KB |
Output is correct |
17 |
Correct |
181 ms |
54908 KB |
Output is correct |
18 |
Correct |
184 ms |
54812 KB |
Output is correct |
19 |
Incorrect |
124 ms |
38820 KB |
Solution announced impossible, but it is possible. |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
24496 KB |
Output is correct |
2 |
Correct |
24 ms |
24500 KB |
Output is correct |
3 |
Correct |
28 ms |
24508 KB |
Output is correct |
4 |
Correct |
26 ms |
24492 KB |
Output is correct |
5 |
Correct |
22 ms |
24500 KB |
Output is correct |
6 |
Correct |
23 ms |
24560 KB |
Output is correct |
7 |
Correct |
24 ms |
24496 KB |
Output is correct |
8 |
Correct |
25 ms |
24684 KB |
Output is correct |
9 |
Correct |
80 ms |
40876 KB |
Output is correct |
10 |
Correct |
29 ms |
26072 KB |
Output is correct |
11 |
Correct |
44 ms |
33052 KB |
Output is correct |
12 |
Correct |
29 ms |
26900 KB |
Output is correct |
13 |
Correct |
35 ms |
30692 KB |
Output is correct |
14 |
Correct |
23 ms |
24756 KB |
Output is correct |
15 |
Correct |
25 ms |
24756 KB |
Output is correct |
16 |
Correct |
87 ms |
40876 KB |
Output is correct |
17 |
Correct |
25 ms |
24496 KB |
Output is correct |
18 |
Correct |
22 ms |
24496 KB |
Output is correct |
19 |
Correct |
23 ms |
24500 KB |
Output is correct |
20 |
Correct |
27 ms |
24488 KB |
Output is correct |
21 |
Correct |
24 ms |
24500 KB |
Output is correct |
22 |
Correct |
26 ms |
24500 KB |
Output is correct |
23 |
Correct |
173 ms |
55828 KB |
Output is correct |
24 |
Correct |
23 ms |
24500 KB |
Output is correct |
25 |
Correct |
24 ms |
24756 KB |
Output is correct |
26 |
Correct |
32 ms |
24972 KB |
Output is correct |
27 |
Correct |
30 ms |
25012 KB |
Output is correct |
28 |
Correct |
76 ms |
36616 KB |
Output is correct |
29 |
Correct |
116 ms |
42564 KB |
Output is correct |
30 |
Correct |
129 ms |
48552 KB |
Output is correct |
31 |
Correct |
176 ms |
56596 KB |
Output is correct |
32 |
Correct |
23 ms |
24496 KB |
Output is correct |
33 |
Correct |
23 ms |
24500 KB |
Output is correct |
34 |
Correct |
24 ms |
24588 KB |
Output is correct |
35 |
Correct |
24 ms |
24500 KB |
Output is correct |
36 |
Correct |
23 ms |
24732 KB |
Output is correct |
37 |
Correct |
24 ms |
24500 KB |
Output is correct |
38 |
Correct |
24 ms |
24488 KB |
Output is correct |
39 |
Correct |
26 ms |
24496 KB |
Output is correct |
40 |
Correct |
24 ms |
24500 KB |
Output is correct |
41 |
Correct |
25 ms |
24492 KB |
Output is correct |
42 |
Correct |
22 ms |
24500 KB |
Output is correct |
43 |
Correct |
27 ms |
24756 KB |
Output is correct |
44 |
Correct |
27 ms |
25008 KB |
Output is correct |
45 |
Correct |
93 ms |
39824 KB |
Output is correct |
46 |
Correct |
138 ms |
46620 KB |
Output is correct |
47 |
Correct |
137 ms |
46616 KB |
Output is correct |
48 |
Correct |
27 ms |
24500 KB |
Output is correct |
49 |
Correct |
26 ms |
24496 KB |
Output is correct |
50 |
Incorrect |
27 ms |
24496 KB |
Tree @(3, 3) appears more than once: for edges on positions 1 and 3 |
51 |
Halted |
0 ms |
0 KB |
- |