This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "parks.h"
#include <bits/stdc++.h>
#define MAXN 200007
using namespace std;
map<pair<int,int>,int> ind;
std::vector<int> u, v, a, b, x, y, nd;
int dsu[MAXN];
int root(int u)
{
while(dsu[u]!=u)
{
dsu[u]=dsu[dsu[u]];
u=dsu[u];
}
return u;
}
void connect(int u,int v)
{
int a=root(u),b=root(v);
if(a==b) return;
dsu[a]=b;
}
bool cmp(int p,int q) {return x[p]+y[p]>x[q]+y[q];}
void addroad(int p,int q)
{
int x1=x[p],y1=y[p],x2=x[q],y2=y[q];
u.push_back(p); v.push_back(q);
connect(p,q);
if(x1==x2)
{
b.push_back((y1+y2)/2);
if((x1+y1)&2) a.push_back(x1-1);
else a.push_back(x1+1);
}
else
{
a.push_back((x1+x2)/2);
if((x1+y1)&2) b.push_back(y1+1);
else b.push_back(y1-1);
}
}
int construct_roads(std::vector<int> X, std::vector<int> Y) {
int n=X.size(); x=X; y=Y;
for(int i=0;i<n;i++) {ind[{x[i],y[i]}]=i+1; dsu[i]=i; nd.push_back(i);}
sort(nd.begin(),nd.end(),cmp);
for(int i=0;i<n;i++)
{
int c=nd[i]; int ea=ind[{x[c]+2,y[c]}]-1,no=ind[{x[c],y[c]+2}]-1;
if(ea==-1 && no==-1) continue;
if(ea!=-1 && no==-1) addroad(c,ea);
if(ea==-1 && no!=-1) addroad(c,no);
if(ea!=-1 && no!=-1)
{
if(root(ea)!=root(no)) {addroad(c,ea); addroad(c,no);}
else
{
if((x[c]+y[c])&2) addroad(c,no);
else addroad(c,ea);
}
}
}
if(u.size()!=n-1) return 0;
build(u, v, a, b);
return 1;
}
Compilation message (stderr)
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:62:16: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
62 | if(u.size()!=n-1) return 0;
| ~~~~~~~~^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |