Submission #1047987

#TimeUsernameProblemLanguageResultExecution timeMemory
1047987UnforgettableplFountain Parks (IOI21_parks)C++17
30 / 100
731 ms108076 KiB
#include "parks.h" #include <bits/stdc++.h> using namespace std; int construct_roads(vector<int> x,vector<int> y){ int n = x.size(); map<pair<int,int>,int> lookup; for(int i=0;i<n;i++)lookup[{x[i],y[i]}]=i; vector<int> u,v,a,b; vector<vector<int>> adj(1); vector<vector<int>> adjsepar(n); vector<tuple<int,int,int,int>> edges; edges.emplace_back(0,0,0,0); int S = 0; for(int i=0;i<n;i++){ if(lookup.count({x[i]+2,y[i]})){ if(lookup.count({x[i]+2,y[i]+2}) and lookup.count({x[i],y[i]+2}))goto next; adjsepar[i].emplace_back(lookup[{x[i]+2,y[i]}]); adjsepar[lookup[{x[i]+2,y[i]}]].emplace_back(i); S++; adj.emplace_back(); edges.emplace_back(i,lookup[{x[i]+2,y[i]}],x[i]+1,y[i]-1); if(lookup.count({x[i]+1,y[i]-1})){ adj[S].emplace_back(lookup[{x[i]+1,y[i]-1}]); adj[lookup[{x[i]+1,y[i]-1}]].emplace_back(S); } else lookup[{x[i]+1,y[i]-1}]=S; S++; adj.emplace_back(); edges.emplace_back(i,lookup[{x[i]+2,y[i]}],x[i]+1,y[i]+1); if(lookup.count({x[i]+1,y[i]+1})){ adj[S].emplace_back(lookup[{x[i]+1,y[i]+1}]); adj[lookup[{x[i]+1,y[i]+1}]].emplace_back(S); } else lookup[{x[i]+1,y[i]+1}]=S; adj[S].emplace_back(S-1); adj[S-1].emplace_back(S); } next: if(lookup.count({x[i],y[i]-2})){ adjsepar[i].emplace_back(lookup[{x[i],y[i]-2}]); adjsepar[lookup[{x[i],y[i]-2}]].emplace_back(i); if(x[i]==2){ u.emplace_back(i); v.emplace_back(lookup[{x[i],y[i]-2}]); a.emplace_back(x[i]-1); b.emplace_back(y[i]-1); continue; } else if(x[i]==6){ u.emplace_back(i); v.emplace_back(lookup[{x[i],y[i]-2}]); a.emplace_back(x[i]+1); b.emplace_back(y[i]-1); continue; } S++; adj.emplace_back(); edges.emplace_back(i,lookup[{x[i],y[i]-2}],x[i]-1,y[i]-1); if(lookup.count({x[i]-1,y[i]-1})){ adj[S].emplace_back(lookup[{x[i]-1,y[i]-1}]); adj[lookup[{x[i]-1,y[i]-1}]].emplace_back(S); } else lookup[{x[i]-1,y[i]-1}]=S; S++; adj.emplace_back(); edges.emplace_back(i,lookup[{x[i],y[i]-2}],x[i]+1,y[i]-1); if(lookup.count({x[i]+1,y[i]-1})){ adj[S].emplace_back(lookup[{x[i]+1,y[i]-1}]); adj[lookup[{x[i]+1,y[i]-1}]].emplace_back(S); } else lookup[{x[i]+1,y[i]-1}]=S; adj[S].emplace_back(S-1); adj[S-1].emplace_back(S); } } vector<bool> visited(n); int vis = 0; function<void(int)> dfshelp = [&](int x){ if(visited[x])return; visited[x]=true; vis++; for(int&i:adjsepar[x])dfshelp(i); }; dfshelp(0); vector<int> colour(S+1,-1); bool works = true; function<void(int,int)> dfs = [&](int x,int col){ if(colour[x]!=-1){ if(colour[x]!=col)works=false; return; } colour[x]=col; for(int&i:adj[x])dfs(i,col^1); }; for(int i=1;i<=S;i++)if(colour[i]==-1)dfs(i,0); if(!works or vis!=n)return 0; for(int i=1;i<=S;i++){ if(colour[i]==0)continue; auto [c,d,e,f] = edges[i]; u.emplace_back(c); v.emplace_back(d); a.emplace_back(e); b.emplace_back(f); } build(u,v,a,b); return 1; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...