#include <bits/stdc++.h>
#include "parks.h"
#include <random>
#include <chrono>
using namespace std;
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> void _do(T x){cerr<<x<<"\n";}
template<typename T,typename ...U> void _do(T x,U ...y){cerr<<x<<", ";_do(y...);}
#define dbg(...) cerr<<#__VA_ARGS__<<" = ";_do(__VA_ARGS__);
const int MOD1=1e9+7;
const int MOD2=998244353;
const ll INF=3e18;
const ld PI=3.14159265358979323846;
ll gcd(ll a,ll b){if(b==0) return a; return gcd(b,a%b);}
ll fpow(ll a,ll b,ll m)
{
if(!b) return 1;
ll ans=fpow(a*a%m,b/2,m);
return (b%2?ans*a%m:ans);
}
ll inv(ll a,ll m) {return fpow(a,m-2,m);}
#define MottoHayaku ios::sync_with_stdio(false);cin.tie(0);
//#define int ll
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define repk(i,m,n) for(int i=m;i<n;i++)
#define F first
#define S second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define uni(c) c.resize(distance(c.begin(),unique(c.begin(),c.end())))
#define unisort(c) sort(c.begin(),c.end()),uni(c)
const int N=2e5+1;
bool vis[N<<2];
vector<int> x,y;
vector<int> retu,retv,a,b;
vector<pii> ed[N<<2];
pii point[N],pos[N<<2];
set<pii> edges;
map<pii,int> id,id2;
int n,tn=0,cnt=0;
int par[N];
int fp(int u){return u==par[u]?u:par[u]=fp(par[u]);}
int unite(int u,int v)
{
if(fp(u)==fp(v)) return 0;
par[fp(v)]=fp(u);
return 1;
}
/*void build(vector<int> v1,vector<int> v2,vector<int> v3,vector<int> v4)
{
for(ll u:v1) cout<<u<<" ";
cout<<"\n";
for(ll u:v2) cout<<u<<" ";
cout<<"\n";
for(ll u:v3) cout<<u<<" ";
cout<<"\n";
for(ll u:v4) cout<<u<<" ";
cout<<"\n";
}
*/
void solve()
{
for(pii tmp:edges)
{
int u=tmp.F,v=tmp.S;
int can=unite(u,v);
if(can)
{
cnt++;
retu.pb(u);
retv.pb(v);
if(y[u]==y[v])
{
int mid=(x[u]+x[v])>>1;
if((mid+y[u]+1)%4==0)
{
a.pb(mid);
b.pb(y[u]+1);
}
else
{
a.pb(mid);
b.pb(y[u]-1);
}
}
else
{
int mid=(y[u]+y[v])>>1;
if((mid+x[u]+1)%4==2)
{
a.pb(x[u]+1);
b.pb(mid);
}
else
{
a.pb(x[u]-1);
b.pb(mid);
}
}
//if(b.back()<2) {dbg(u,v,b.back());}
}
}
}
void dfs(int u)
{
vis[u]=1;
for(pii tmp:ed[u])
{
if(vis[tmp.F]) continue;
pii p=pos[tmp.F];
if(tmp.S==1)
{
edges.erase({id[{p.F+1,p.S-1}],id[{p.F+1,p.S+1}]});
edges.erase({id[{p.F+1,p.S+1}],id[{p.F+1,p.S-1}]});
}
else if(tmp.S==2)
{
edges.erase({id[{p.F-1,p.S-1}],id[{p.F-1,p.S+1}]});
edges.erase({id[{p.F-1,p.S+1}],id[{p.F-1,p.S-1}]});
}
else if(tmp.S==3)
{
edges.erase({id[{p.F-1,p.S-1}],id[{p.F+1,p.S-1}]});
edges.erase({id[{p.F+1,p.S-1}],id[{p.F-1,p.S-1}]});
}
else if(tmp.S==4)
{
edges.erase({id[{p.F-1,p.S+1}],id[{p.F+1,p.S+1}]});
edges.erase({id[{p.F+1,p.S+1}],id[{p.F-1,p.S+1}]});
}
dfs(tmp.F);
}
}
int construct_roads(vector<int> X,vector<int> Y) {
x=X;
y=Y;
n=x.size();
if(n==1)
{
build(retu,retv,a,b);
return 1;
}
rep(i,n)
{
point[i]={x[i],y[i]};
id[point[i]]=i;
}
rep(i,n)
{
int tmp=0;
if(id.count({x[i]+2,y[i]}))
{
edges.insert({id[{x[i]+2,y[i]}],i});
tmp++;
}
if(id.count({x[i],y[i]+2}))
{
edges.insert({id[{x[i],y[i]+2}],i});
tmp++;
}
if(tmp==2)
{
if(id.count({x[i]+2,y[i]+2}))
{
tn++;
pos[tn]={x[i]+1,y[i]+1};
id2[{x[i]+1,y[i]+1}]=tn;
}
}
}
rep1(i,tn)
{
pii tmp=pos[i];
if((tmp.F+tmp.S)%4==0)
{
if(id2.count({tmp.F,tmp.S-2}))
{
int u=id2[{tmp.F,tmp.S-2}];
ed[i].pb({u,4});
ed[u].pb({i,3});
}
else ed[0].pb({i,3});
if(id2.count({tmp.F,tmp.S+2}))
{
int u=id2[{tmp.F,tmp.S+2}];
ed[i].pb({u,3});
ed[u].pb({i,4});
}
else ed[0].pb({i,4});
}
else
{
if(id2.count({tmp.F-2,tmp.S}))
{
int u=id2[{tmp.F-2,tmp.S}];
ed[i].pb({u,1});
ed[u].pb({i,2});
}
else ed[0].pb({i,2});
if(id2.count({tmp.F+2,tmp.S}))
{
int u=id2[{tmp.F+2,tmp.S}];
ed[i].pb({u,2});
ed[u].pb({i,1});
}
else ed[0].pb({i,1});
}
}
if(tn>0)
{
dfs(0);
}
rep(i,n) par[i]=i;
solve();
if(cnt<n-1) return 0;
else
{
build(retu,retv,a,b);
return 1;
}
}
/*
for blocks:
if (x+y)%4==0 then ud
if (x+y)%4==2 then lr
direction:
1: left
2: right
3: up
4: down
*/
/*
signed main()
{
MottoHayaku
vector<int> X,Y;
int tx,ty;
while(cin>>tx>>ty)
{
X.pb(tx);
Y.pb(ty);
}
ll res=construct_roads(X,Y);
cout<<res<<"\n";
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19020 KB |
Output is correct |
2 |
Correct |
9 ms |
19020 KB |
Output is correct |
3 |
Correct |
9 ms |
19020 KB |
Output is correct |
4 |
Correct |
9 ms |
19020 KB |
Output is correct |
5 |
Correct |
9 ms |
19020 KB |
Output is correct |
6 |
Correct |
9 ms |
19020 KB |
Output is correct |
7 |
Correct |
9 ms |
19020 KB |
Output is correct |
8 |
Correct |
9 ms |
19020 KB |
Output is correct |
9 |
Correct |
175 ms |
38384 KB |
Output is correct |
10 |
Correct |
19 ms |
21196 KB |
Output is correct |
11 |
Correct |
69 ms |
29568 KB |
Output is correct |
12 |
Correct |
24 ms |
22192 KB |
Output is correct |
13 |
Correct |
47 ms |
26424 KB |
Output is correct |
14 |
Correct |
10 ms |
19276 KB |
Output is correct |
15 |
Correct |
11 ms |
19404 KB |
Output is correct |
16 |
Correct |
185 ms |
38424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19020 KB |
Output is correct |
2 |
Correct |
9 ms |
19020 KB |
Output is correct |
3 |
Correct |
9 ms |
19020 KB |
Output is correct |
4 |
Correct |
9 ms |
19020 KB |
Output is correct |
5 |
Correct |
9 ms |
19020 KB |
Output is correct |
6 |
Correct |
9 ms |
19020 KB |
Output is correct |
7 |
Correct |
9 ms |
19020 KB |
Output is correct |
8 |
Correct |
9 ms |
19020 KB |
Output is correct |
9 |
Correct |
175 ms |
38384 KB |
Output is correct |
10 |
Correct |
19 ms |
21196 KB |
Output is correct |
11 |
Correct |
69 ms |
29568 KB |
Output is correct |
12 |
Correct |
24 ms |
22192 KB |
Output is correct |
13 |
Correct |
47 ms |
26424 KB |
Output is correct |
14 |
Correct |
10 ms |
19276 KB |
Output is correct |
15 |
Correct |
11 ms |
19404 KB |
Output is correct |
16 |
Correct |
185 ms |
38424 KB |
Output is correct |
17 |
Correct |
9 ms |
19084 KB |
Output is correct |
18 |
Correct |
9 ms |
19020 KB |
Output is correct |
19 |
Incorrect |
9 ms |
19108 KB |
Tree @(3, 3) appears more than once: for edges on positions 1 and 3 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19020 KB |
Output is correct |
2 |
Correct |
9 ms |
19020 KB |
Output is correct |
3 |
Correct |
9 ms |
19020 KB |
Output is correct |
4 |
Correct |
9 ms |
19020 KB |
Output is correct |
5 |
Correct |
9 ms |
19020 KB |
Output is correct |
6 |
Correct |
9 ms |
19020 KB |
Output is correct |
7 |
Correct |
9 ms |
19020 KB |
Output is correct |
8 |
Correct |
9 ms |
19020 KB |
Output is correct |
9 |
Correct |
175 ms |
38384 KB |
Output is correct |
10 |
Correct |
19 ms |
21196 KB |
Output is correct |
11 |
Correct |
69 ms |
29568 KB |
Output is correct |
12 |
Correct |
24 ms |
22192 KB |
Output is correct |
13 |
Correct |
47 ms |
26424 KB |
Output is correct |
14 |
Correct |
10 ms |
19276 KB |
Output is correct |
15 |
Correct |
11 ms |
19404 KB |
Output is correct |
16 |
Correct |
185 ms |
38424 KB |
Output is correct |
17 |
Correct |
9 ms |
19084 KB |
Output is correct |
18 |
Correct |
9 ms |
19020 KB |
Output is correct |
19 |
Incorrect |
9 ms |
19108 KB |
Tree @(3, 3) appears more than once: for edges on positions 1 and 3 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19020 KB |
Output is correct |
2 |
Correct |
9 ms |
19020 KB |
Output is correct |
3 |
Correct |
9 ms |
19020 KB |
Output is correct |
4 |
Correct |
9 ms |
19020 KB |
Output is correct |
5 |
Correct |
9 ms |
19020 KB |
Output is correct |
6 |
Correct |
9 ms |
19020 KB |
Output is correct |
7 |
Correct |
9 ms |
19020 KB |
Output is correct |
8 |
Correct |
9 ms |
19020 KB |
Output is correct |
9 |
Correct |
175 ms |
38384 KB |
Output is correct |
10 |
Correct |
19 ms |
21196 KB |
Output is correct |
11 |
Correct |
69 ms |
29568 KB |
Output is correct |
12 |
Correct |
24 ms |
22192 KB |
Output is correct |
13 |
Correct |
47 ms |
26424 KB |
Output is correct |
14 |
Correct |
10 ms |
19276 KB |
Output is correct |
15 |
Correct |
11 ms |
19404 KB |
Output is correct |
16 |
Correct |
185 ms |
38424 KB |
Output is correct |
17 |
Correct |
9 ms |
19020 KB |
Output is correct |
18 |
Correct |
9 ms |
19020 KB |
Output is correct |
19 |
Correct |
10 ms |
19020 KB |
Output is correct |
20 |
Correct |
576 ms |
58132 KB |
Output is correct |
21 |
Correct |
526 ms |
57972 KB |
Output is correct |
22 |
Correct |
523 ms |
58028 KB |
Output is correct |
23 |
Correct |
382 ms |
52032 KB |
Output is correct |
24 |
Correct |
233 ms |
38628 KB |
Output is correct |
25 |
Correct |
518 ms |
51292 KB |
Output is correct |
26 |
Correct |
460 ms |
51228 KB |
Output is correct |
27 |
Correct |
494 ms |
57508 KB |
Output is correct |
28 |
Correct |
487 ms |
57600 KB |
Output is correct |
29 |
Correct |
556 ms |
57668 KB |
Output is correct |
30 |
Correct |
525 ms |
57596 KB |
Output is correct |
31 |
Correct |
9 ms |
19068 KB |
Output is correct |
32 |
Correct |
29 ms |
22156 KB |
Output is correct |
33 |
Correct |
92 ms |
28860 KB |
Output is correct |
34 |
Correct |
474 ms |
58056 KB |
Output is correct |
35 |
Correct |
20 ms |
20812 KB |
Output is correct |
36 |
Correct |
80 ms |
27252 KB |
Output is correct |
37 |
Correct |
190 ms |
35224 KB |
Output is correct |
38 |
Correct |
175 ms |
34976 KB |
Output is correct |
39 |
Correct |
248 ms |
40532 KB |
Output is correct |
40 |
Correct |
349 ms |
46368 KB |
Output is correct |
41 |
Correct |
450 ms |
52396 KB |
Output is correct |
42 |
Correct |
583 ms |
58324 KB |
Output is correct |
43 |
Correct |
9 ms |
19020 KB |
Output is correct |
44 |
Correct |
9 ms |
19020 KB |
Output is correct |
45 |
Correct |
9 ms |
19020 KB |
Output is correct |
46 |
Correct |
9 ms |
19060 KB |
Output is correct |
47 |
Correct |
9 ms |
18992 KB |
Output is correct |
48 |
Correct |
9 ms |
19020 KB |
Output is correct |
49 |
Correct |
9 ms |
19020 KB |
Output is correct |
50 |
Correct |
9 ms |
19020 KB |
Output is correct |
51 |
Correct |
9 ms |
19020 KB |
Output is correct |
52 |
Correct |
10 ms |
19020 KB |
Output is correct |
53 |
Correct |
9 ms |
19020 KB |
Output is correct |
54 |
Correct |
10 ms |
19404 KB |
Output is correct |
55 |
Correct |
11 ms |
19532 KB |
Output is correct |
56 |
Correct |
188 ms |
38384 KB |
Output is correct |
57 |
Correct |
325 ms |
46952 KB |
Output is correct |
58 |
Correct |
333 ms |
47008 KB |
Output is correct |
59 |
Correct |
10 ms |
19020 KB |
Output is correct |
60 |
Correct |
9 ms |
19104 KB |
Output is correct |
61 |
Correct |
9 ms |
19020 KB |
Output is correct |
62 |
Correct |
440 ms |
57496 KB |
Output is correct |
63 |
Correct |
432 ms |
57528 KB |
Output is correct |
64 |
Correct |
444 ms |
57348 KB |
Output is correct |
65 |
Correct |
12 ms |
19660 KB |
Output is correct |
66 |
Correct |
21 ms |
20376 KB |
Output is correct |
67 |
Correct |
201 ms |
38424 KB |
Output is correct |
68 |
Correct |
345 ms |
48020 KB |
Output is correct |
69 |
Correct |
506 ms |
57512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19020 KB |
Output is correct |
2 |
Correct |
9 ms |
19020 KB |
Output is correct |
3 |
Correct |
9 ms |
19020 KB |
Output is correct |
4 |
Correct |
9 ms |
19020 KB |
Output is correct |
5 |
Correct |
9 ms |
19020 KB |
Output is correct |
6 |
Correct |
9 ms |
19020 KB |
Output is correct |
7 |
Correct |
9 ms |
19020 KB |
Output is correct |
8 |
Correct |
9 ms |
19020 KB |
Output is correct |
9 |
Correct |
175 ms |
38384 KB |
Output is correct |
10 |
Correct |
19 ms |
21196 KB |
Output is correct |
11 |
Correct |
69 ms |
29568 KB |
Output is correct |
12 |
Correct |
24 ms |
22192 KB |
Output is correct |
13 |
Correct |
47 ms |
26424 KB |
Output is correct |
14 |
Correct |
10 ms |
19276 KB |
Output is correct |
15 |
Correct |
11 ms |
19404 KB |
Output is correct |
16 |
Correct |
185 ms |
38424 KB |
Output is correct |
17 |
Correct |
486 ms |
57584 KB |
Output is correct |
18 |
Correct |
474 ms |
57660 KB |
Output is correct |
19 |
Correct |
524 ms |
57764 KB |
Output is correct |
20 |
Correct |
627 ms |
59804 KB |
Output is correct |
21 |
Correct |
475 ms |
53720 KB |
Output is correct |
22 |
Correct |
9 ms |
19020 KB |
Output is correct |
23 |
Correct |
62 ms |
25916 KB |
Output is correct |
24 |
Correct |
40 ms |
22784 KB |
Output is correct |
25 |
Correct |
128 ms |
31208 KB |
Output is correct |
26 |
Correct |
280 ms |
39480 KB |
Output is correct |
27 |
Correct |
230 ms |
39380 KB |
Output is correct |
28 |
Correct |
330 ms |
44724 KB |
Output is correct |
29 |
Correct |
407 ms |
49748 KB |
Output is correct |
30 |
Correct |
507 ms |
54616 KB |
Output is correct |
31 |
Correct |
609 ms |
59960 KB |
Output is correct |
32 |
Correct |
544 ms |
59412 KB |
Output is correct |
33 |
Correct |
428 ms |
57552 KB |
Output is correct |
34 |
Correct |
14 ms |
19980 KB |
Output is correct |
35 |
Correct |
19 ms |
20764 KB |
Output is correct |
36 |
Correct |
207 ms |
38812 KB |
Output is correct |
37 |
Correct |
393 ms |
48768 KB |
Output is correct |
38 |
Correct |
555 ms |
58640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19020 KB |
Output is correct |
2 |
Correct |
9 ms |
19020 KB |
Output is correct |
3 |
Correct |
9 ms |
19020 KB |
Output is correct |
4 |
Correct |
9 ms |
19020 KB |
Output is correct |
5 |
Correct |
9 ms |
19020 KB |
Output is correct |
6 |
Correct |
9 ms |
19020 KB |
Output is correct |
7 |
Correct |
9 ms |
19020 KB |
Output is correct |
8 |
Correct |
9 ms |
19020 KB |
Output is correct |
9 |
Correct |
175 ms |
38384 KB |
Output is correct |
10 |
Correct |
19 ms |
21196 KB |
Output is correct |
11 |
Correct |
69 ms |
29568 KB |
Output is correct |
12 |
Correct |
24 ms |
22192 KB |
Output is correct |
13 |
Correct |
47 ms |
26424 KB |
Output is correct |
14 |
Correct |
10 ms |
19276 KB |
Output is correct |
15 |
Correct |
11 ms |
19404 KB |
Output is correct |
16 |
Correct |
185 ms |
38424 KB |
Output is correct |
17 |
Correct |
9 ms |
19084 KB |
Output is correct |
18 |
Correct |
9 ms |
19020 KB |
Output is correct |
19 |
Incorrect |
9 ms |
19108 KB |
Tree @(3, 3) appears more than once: for edges on positions 1 and 3 |
20 |
Halted |
0 ms |
0 KB |
- |