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<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(10101994);
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){
shuffle(all(tmp),rng);
for (auto v:tmp)add[v]=0;
if (sz(tmp)<=1)return;
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 {
sort(all(xd[u]),[&](int i,int j){
return ask(s,i)<ask(s,j);
});
vector<int>lft,rht;
lft.pb(xd[u][0]);
for (auto v:xd[u]){
if (v==xd[u][0])continue;
if (ask(xd[u][0],s)+ask(xd[u][0],v)==ask(s,v)){
lft.pb(v);
}
else {
rht.pb(v);
}
}
if (!lft.empty())add_edg(u,lft[0],ask(s,lft[0])-ask(s,u));
if (!rht.empty())add_edg(u,rht[0],ask(s,rht[0])-ask(s,u));
xd[u].clear();
solve(lft),solve(rht);
}
}
}
}
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 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... |