#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0;i<n;++i)
#define pb push_back
#define all(x) x.begin(), x.end()
#define pi pair<int,int>
#define f first
#define s second
struct DSU {
vector<int> p,sz;
int tot;
DSU(int n) {
forn(i,n) p.pb(i), sz.pb(1);
tot=n;
}
int get(int u) {
return p[u]==u?u:get(p[u]);
}
void uni(int u, int v) {
u=get(u), v=get(v);
if (sz[u]<sz[v]) swap(u,v);
sz[u]+=sz[v];
p[v]=u;
--tot;
}
};
int p3(vector<int> x, vector<int> y) {
int n=x.size();
vector<int> u,v,a,b;
map<pi,int> m;
forn(i,n) m[{x[i],y[i]}]=i;
DSU dsu(n);
vector<pi> z={{-2,0},{2,0},{0,-2},{0,2}};
forn(i,n) {
int f=x[i], s=y[i];
for(auto&x:z) {
if (m.count({f+x.f,s+x.s})) {
if (i<m[{f+x.f,s+x.s}]) {
if (dsu.get(i)==dsu.get(m[{f+x.f,s+x.s}])) continue;
dsu.uni(i,m[{f+x.f,s+x.s}]);
u.pb(i), v.pb(m[{f+x.f,s+x.s}]);
}
}
}
}
forn(i,n-1) {
int x1=x[u[i]], y1=y[u[i]];
int x2=x[v[i]], y2=y[v[i]];
if (x1==x2) {
if (x1==2) {
a.pb(1);
b.pb(max(y1,y2)-1);
} else {
a.pb(5);
b.pb(max(y1,y2)-1);
}
} else {
a.pb(3);
b.pb(y1-1);
}
}
build(u,v,a,b);
return 1;
}
int construct_roads(vector<int> x, vector<int> y) {
int mx=0; for(auto&v:x) mx=max(mx,v);
if (mx<=4) return p3(x,y);
int n=x.size();
vector<int> u,v,a,b;
map<pi,int> m;
forn(i,n) m[{x[i],y[i]}]=i;
DSU dsu(n);
vector<pi> z={{-2,0},{2,0},{0,-2},{0,2}};
forn(i,n) {
int f=x[i], s=y[i];
for(auto&x:z) {
if (m.count({f+x.f,s+x.s})) {
if (i<m[{f+x.f,s+x.s}]) {
if (dsu.get(i)==dsu.get(m[{f+x.f,s+x.s}])) continue;
dsu.uni(i,m[{f+x.f,s+x.s}]);
u.pb(i), v.pb(m[{f+x.f,s+x.s}]);
}
}
}
}
if (u.size()!=n-1) return 0;
forn(i,n-1) {
int x1=x[u[i]], y1=y[u[i]];
int x2=x[v[i]], y2=y[v[i]];
if (x1==x2) {
if ((max(y1,y2)+x1)%4) {
a.pb(x1+1);
b.pb(max(y1,y2)-1);
} else {
a.pb(x1-1);
b.pb(max(y1,y2)-1);
}
} else {
if ((max(x1,x2)+y1)%4) {
a.pb(max(x1,x2)-1);
b.pb(y1-1);
} else {
a.pb(max(x1,x2)-1);
b.pb(y1+1);
}
}
}
build(u,v,a,b);
return 1;
}
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:95:14: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
95 | if (u.size()!=n-1) return 0;
| ~~~~~~~~^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |