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>
using namespace std;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
#define F first
#define S second
int xx[] = {0,0,1,-1};
int yy[] = {1,-1,0,0};
vector<vector<int>> g;
vector<bool> vis;
int c = 0;
void search(int i) {
vis[i] = 1; c++;
for(auto x: g[i]) if(!vis[x]) search(x);
}
int construct_roads(std::vector<int> x, std::vector<int> y) {
int n = x.size();
vis.resize(n,0);
g.resize(n);
vector<iii> p;
for(int i = 0; i < n; i++) p.push_back({{x[i],y[i]},i});
sort(p.begin(),p.end());
vector<ii> roads;
map<ii,int> rr;
for(int i = 0; i < n-1; i++) {
if(p[i].F.F == p[i+1].F.F && p[i+1].F.S-p[i].F.S == 2) {
ii a = {p[i].F.F, p[i].F.S+1};
rr[a] = roads.size();
roads.emplace_back(p[i].S, p[i+1].S);
g[p[i].S].push_back(p[i+1].S);
g[p[i+1].S].push_back(p[i].S);
}
}
for(int i = 0; i < n; i++) swap(p[i].F.F,p[i].F.S);
sort(p.begin(),p.end());
for(int i = 0; i < n; i++) swap(p[i].F.F,p[i].F.S);
for(int i = 0; i < n-1; i++) {
if(p[i].F.S == p[i+1].F.S && p[i+1].F.F-p[i].F.F == 2) {
ii a = {p[i].F.F+1,p[i].F.S};
rr[a] = roads.size();
roads.emplace_back(p[i].S, p[i+1].S);
g[p[i].S].push_back(p[i+1].S);
g[p[i+1].S].push_back(p[i].S);
}
}
int m = roads.size();
search(0);
if(c < n) return 0;
vector<ii> b;
vector<int> rb(m,-1);
for(auto x: rr) {
if(rb[x.S] == -1) {
rb[x.S] = b.size();
int f = x.S;
ii a;
if(x.F.F%2) a = {x.F.F, x.F.S+1};
else a = {x.F.F+1, x.F.S};
b.push_back(a);
//cout << a.F << " " << a.S << endl;
bool bb = true;
while(bb) {
bb = false;
for(int i = 0; i < 4; i++) {
ii po = {a.F+xx[i], a.S+yy[i]};
if(rr.count(po) && rr[po] != f) {
f = rr[po];
if(rb[f] >= 0) break;
bb = true;
a = {po.F+xx[i], po.S+yy[i]};
//cout << a.F << " " << a.S << endl;
rb[f] = b.size();
b.push_back(a);
break;
}
}
}
}
}
vector<int> u(m), v(m), ba(m), bb(m);
for(int i = 0; i < m; i++) {
u[i] = roads[i].F; v[i] = roads[i].S;
ba[i] = b[rb[i]].F; bb[i] = b[rb[i]].S;
}
build(u, v, ba, bb);
//for(auto x: rr) cout << x.F.F << " " << x.F.S << endl;
return 1;
}
# | 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... |