Submission #939263

#TimeUsernameProblemLanguageResultExecution timeMemory
939263winter0101City Mapping (NOI18_citymapping)C++14
85.33 / 100
4 ms8792 KiB
#include<bits/stdc++.h> #include "citymapping.h" using namespace std; #define all(fl) fl.begin(),fl.end() #define pb push_back #define fi first #define se second #define for1(i,j,k) for(int i=j;i<=k;i++) #define for2(i,j,k) for(int i=j;i>=k;i--) #define for3(i,j,k,l) for(int i=j;i<=k;i+=l) #define lb lower_bound #define ub upper_bound #define sz(a) (int)a.size() #define pii pair<int,int> #define pli pair<long long,int> #define gcd __gcd #define lcm(x,y) x*y/__gcd(x,y) #define pil pair<int,long long> vector<pii>aaa[1009]; mt19937_64 rng(310197); long long d[1009][1009]; long long ask(int x,int y){ if (d[x][y]!=-1)return d[x][y]; return d[x][y]=d[y][x]=get_distance(x,y); } bool add[1009]; vector<int>xd[1009]; void solve(vector<int>&tmp){ if (sz(tmp)<=1)return; shuffle(all(tmp),rng); for (auto v:tmp)add[v]=0; shuffle(all(tmp),rng); int s=tmp[0],t=tmp.back(); ask(s,t); vector<int>vertice; for (auto v:tmp){ if (ask(s,v)+ask(t,v)==ask(s,t)){ vertice.pb(v); add[v]=1; } } //cout<<s<<" "<<t<<'\n'; sort(all(vertice),[&](int i,int j){ return ask(s,i)<ask(s,j); }); auto add_edg=[&](int u,int v,int w){ aaa[u].pb({v,w}); aaa[v].pb({u,w}); //cerr<<u<<" "<<v<<" "<<w<<'\n'; return; }; for1(i,1,sz(vertice)-1){ add_edg(vertice[i],vertice[i-1],ask(vertice[i],s)-ask(vertice[i-1],s)); } for(auto v:tmp){ if (add[v])continue; for (auto u:vertice){ if (ask(s,v)-ask(s,u)==ask(t,v)-ask(t,u)&&ask(s,v)>ask(s,u)){ xd[u].pb(v); //cerr<<u<<" "<<v<<'\n'; } } } for (auto u:vertice){ if (!xd[u].empty()){ if ((u!=s&&u!=t)||sz(aaa[u])>=2){ vector<int>cop; cop.swap(xd[u]); int mn=cop[0]; for (auto v:cop){ if (ask(s,v)<ask(s,mn)){ mn=v; } } add_edg(u,mn,ask(s,mn)-ask(s,u)); solve(cop); } else { vector<int>cop; cop.swap(xd[u]); cop.pb(u); solve(cop); } } } } void find_roads(int N, int Q, int A[], int B[], int W[]) { for1(i,1,N){ for1(j,1,N){ d[i][j]=-1; } d[i][i]=0; } vector<int>tree; for1(i,1,N)tree.pb(i); solve(tree); int cnt=0; for1(i,1,N){ for (auto v:aaa[i]){ if (v.fi>i){ A[cnt]=v.fi,B[cnt]=i,W[cnt]=v.se; cnt++; } } } }
#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...