#include using namespace std; using ll = long long; using pll = pair; #define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i) #define rep(i, n) drep(i, 0, n - 1) #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); const ll MOD1000000007 = 1000000007; const ll MOD998244353 = 998244353; const ll MOD[3] = {999727999, 1070777777, 1000000007}; const ll LINF = 1LL << 60LL; const int IINF = (1 << 30) - 1; template struct edge{ int from, to; T cost; int id; edge(){} edge(int to, T cost=1) : from(-1), to(to), cost(cost){} edge(int to, T cost, int id) : from(-1), to(to), cost(cost), id(id){} edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id){} }; template struct redge{ int from, to; T cap, cost; int rev; redge(int to, T cap, T cost=(T)(1)) : from(-1), to(to), cap(cap), cost(cost){} redge(int to, T cap, T cost, int rev) : from(-1), to(to), cap(cap), cost(cost), rev(rev){} }; template using Edges = vector>; template using weighted_graph = vector>; template using tree = vector>; using unweighted_graph = vector>; template using residual_graph = vector>>; #include namespace suisen { namespace internal::kruscal { // CostType: a type represents weights of edges i.e. (unsigned) int, (unsigned) long long, ... template struct KruscalMST { using cost_type = CostType; using edge_type = std::tuple; using comparator_type = ComparatorType; KruscalMST() : KruscalMST(0) {} explicit KruscalMST(const int n) : _n(n) {} void add_edge(const int u, const int v, const cost_type& cost) { _built = false; _edges.emplace_back(u, v, cost); } void add_edge(const edge_type& e) { _built = false; _edges.push_back(e); } /** * constructs the MST in O(ElogE) time using Kruskal's algprithm (E is the number of edges). * return: whether there exists MST or not (i.e. the graph is connected or not) */ bool build() { _built = true; _weight_sum = 0; if (_n == 0) return true; atcoder::dsu uf(_n); std::sort(_edges.begin(), _edges.end(), [this](const auto& e1, const auto& e2) { return _comp(std::get<2>(e1), std::get<2>(e2)); }); for (auto& [u, v, w] : _edges) { if (uf.same(u, v)) { u = v = _n; } else { uf.merge(u, v); _weight_sum += w; } } _edges.erase(std::remove_if(_edges.begin(), _edges.end(), [this](auto& e) { return std::get<0>(e) == _n; }), _edges.end()); return int(_edges.size()) == _n - 1; } /** * ! This must not be called before calling `solve()`. * return: * 1. connected: sum of weights of edges in the minimum spanning tree * 2. otherwise: sum of weights of edges in the minimum spanning forest */ cost_type get_weight() const { assert(_built); return _weight_sum; } /** * ! This must not be called before calling `solve()`. * return: * 1. connected: edges in the minimum spanning tree * 2. otherwise: edges in the minimum spanning forest * It is guaranteed that edges[i] <= edges[j] iff i <= j. */ const std::vector& get_mst() const { assert(_built); return _edges; } private: int _n; cost_type _weight_sum; std::vector _edges; bool _built = false; comparator_type _comp{}; }; } // namespace internal::kruscal template using KruscalMinimumSpanningTree = internal::kruscal::KruscalMST>; template using KruscalMaximumSpanningTree = internal::kruscal::KruscalMST>; } // namespace suisen namespace suisen { template KruscalMinimumSpanningTree manhattan_mst(std::vector> points) { const int n = points.size(); std::vector p(n); std::iota(p.begin(), p.end(), 0); auto make_edges = [&](KruscalMinimumSpanningTree &mst) { std::sort( p.begin(), p.end(), [&points](int i, int j) { const auto &[xi, yi] = points[i]; const auto &[xj, yj] = points[j]; return yi - xi == yj - xj ? xi < xj : yi - xi < yj - xj; } ); std::vector comp_x(n); for (int i = 0; i < n; ++i) comp_x[i] = points[i].first; std::sort(comp_x.begin(), comp_x.end()); comp_x.erase(std::unique(comp_x.begin(), comp_x.end()), comp_x.end()); const int m = comp_x.size(); auto compress = [&](const T& x) { return std::lower_bound(comp_x.begin(), comp_x.end(), x) - comp_x.begin(); }; struct PrefixMaxQuery { const std::pair _neg_inf{ std::numeric_limits::min(), -1 }; int _n; std::vector> _dat; PrefixMaxQuery(int n) : _n(n), _dat(_n + 1, _neg_inf) {} void chmax(int i, const std::pair& val) { for (++i; i <= _n; i += -i & i) if (_dat[i].first < val.first) _dat[i] = val; } std::pair prefix_max(int r) const { std::pair res = _neg_inf; for (; r; r -= -r & r) if (res.first < _dat[r].first) res = _dat[r]; return res; } } pmq{ m }; for (int i : p) { const auto& [x, y] = points[i]; const int cx = compress(x); if (const auto p = pmq.prefix_max(cx + 1); p != pmq._neg_inf) { const auto& [v, j] = p; mst.add_edge(i, j, x + y - v); } pmq.chmax(cx, { x + y, i }); } }; KruscalMinimumSpanningTree mst(n); for (int x_rev = 0; x_rev < 2; ++x_rev) { for (int y_rev = 0; y_rev < 2; ++y_rev) { for (int xy_rev = 0; xy_rev < 2; ++xy_rev) { make_edges(mst); for (auto& [x, y] : points) std::swap(x, y); } for (auto& [x, _] : points) x = -x; } for (auto& [_, y] : points) y = -y; } assert(mst.build()); return mst; } } // namespace suisen void solve(){ int n, l; cin >> n >> l; vector> points(n); for(int i=0; i> points[i].fi >> points[i].se; auto mst = suisen::manhattan_mst(points); tree T(n); for(auto [u, v, _] : mst.get_mst()){ T[u].pb(edge(v)); T[v].pb(edge(u)); } vector ans; function dfs = [&](int v, int p){ ans.pb(v); for(edge e : T[v]) if(e.to!=p) dfs(e.to, v); }; dfs(0, -1); cout << n << '\n'; for(int i=0; i> T; while(T--) solve(); }