-
Notifications
You must be signed in to change notification settings - Fork 0
/
manhattanmst.hpp
42 lines (40 loc) · 1.08 KB
/
manhattanmst.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#pragma once
#include "DataStructure/unionfind.hpp"
template<typename T>pair<T,vector<pair<int,int>>> ManhattanMST(vector<T>& X,vector<T>& Y){
using Q=pair<T,pair<int,int>>;
int n=X.size();
vector<int> ord(n);
iota(ALL(ord),0);
vector<Q> cand;
rep(_,0,2){
rep(__,0,2){
sort(ALL(ord),[&](int i,int j){return X[i]+Y[i]<X[j]+Y[j];});
map<T,int> sweep;
for(auto& i:ord){
for(auto it=sweep.lower_bound(-Y[i]);it!=sweep.end();it=sweep.erase(it)){
int j=it->second;
if(X[i]-X[j]<Y[i]-Y[j])break;
cand.push_back({abs(X[i]-X[j])+abs(Y[i]-Y[j]),{i,j}});
}
sweep[-Y[i]]=i;
}
swap(X,Y);
}
for(auto& x:X)x=-x;
}
sort(ALL(cand));
UnionFind uni(n);
T ret=0;
vector<pair<int,int>> es;
for(auto& [cost,uv]:cand){
auto [u,v]=uv;
if(uni.unite(u,v)){
ret+=cost;
es.push_back(uv);
}
}
return {ret,es};
}
/**
* @brief Manhattan MST
*/