Language: C++

// http://abc065.contest.atcoder.jp/tasks/arc076_b

#include <algorithm>
#include <cstdio>
#include <cstdlib>

class UnionFind {
	int n;
	int * parent;
	int * depth;
public:
	UnionFind (int n) {
		this->n = n;
		parent = new int[n];
		depth = new int[n];
		for (int i = 0; i < n; ++i) {
			parent[i] = i;
			depth[i] = 1;
		}
	}
	~UnionFind () {
		delete[] parent;
		delete[] depth;
	}
	int find (int node) {
		while (parent[node] != parent[parent[node]]) {
			parent[node] = parent[parent[node]];
		}
		return parent[node];
	}
	void union_ (int nodeA, int nodeB) {
		int rootA = find(nodeA), rootB = find(nodeB);
		if (rootA != rootB) {
			if (depth[rootA] < depth[rootB]) {
				std::swap(rootA, rootB);
			}
			parent[rootB] = rootA;
			depth[rootA] = std::max(depth[rootA], depth[rootB] + 1);
		}
	}
};

struct Town { int x, y; };

struct Road {
	int a, b, cost;
	Road (int a, int b, int cost) : a(a), b(b), cost(cost) {}
};

int main () {

	int n;
	scanf("%d", &n);

	std::vector<Town> towns(n);
	std::vector<Road> roads;

	UnionFind unionFind(n);

	int totalCost = 0;

	auto getCost = [&towns] (int a, int b) -> int {
		return std::min(abs(towns[a].x - towns[b].x), abs(towns[a].y - towns[b].y));
	};

	for (int i = 0; i < n; ++i) {
		// towns[i] = Town();
		scanf("%d%d", &towns[i].x, &towns[i].y);
	}

	for (int i = 0; i < n; ++i) {
		for (int j = i + 1; j < n; ++j) {
			roads.push_back(Road(i, j, getCost(i, j)));
		}
	}

	std::sort(roads.begin(), roads.end(), [] (
		const Road & a,
		const Road & b
	) -> bool {
		return a.cost < b.cost;
	});

	for (Road road : roads) {
		if (unionFind.find(road.a) != unionFind.find(road.b)) {
			totalCost += road.cost;
			unionFind.union_(road.a, road.b);
		}
	}

	printf("%d\n", totalCost);

	return 0;

}


Compiler output:
a.cc: In function 'int main()':
a.cc:55:14: error: 'vector' is not a member of 'std'
   55 |         std::vector<Town> towns(n);
      |              ^~~~~~
a.cc:6:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
    5 | #include <cstdlib>
  +++ |+#include <vector>
    6 | 
a.cc:55:25: error: expected primary-expression before '>' token
   55 |         std::vector<Town> towns(n);
      |                         ^
a.cc:55:27: error: 'towns' was not declared in this scope
   55 |         std::vector<Town> towns(n);
      |                           ^~~~~
a.cc:56:14: error: 'vector' is not a member of 'std'
   56 |         std::vector<Road> roads;
      |              ^~~~~~
a.cc:56:14: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
a.cc:56:25: error: expected primary-expression before '>' token
   56 |         std::vector<Road> roads;
      |                         ^
a.cc:56:27: error: 'roads' was not declared in this scope
   56 |         std::vector<Road> roads;
      |                           ^~~~~
a.cc: In lambda function:
a.cc:63:37: error: 'towns' is not captured
   63 |                 return std::min(abs(towns[a].x - towns[b].x), abs(towns[a].y - towns[b].y));
      |                                     ^~~~~
a.cc:62:31: note: the lambda has no capture-default
   62 |         auto getCost = [&towns] (int a, int b) -> int {
      |                               ^
a.cc:55:27: note: '<typeprefixerror>towns' declared here
   55 |         std::vector<Town> towns(n);
      |                           ^~~~~
a.cc:63:50: error: 'towns' is not captured
   63 |                 return std::min(abs(towns[a].x - towns[b].x), abs(towns[a].y - towns[b].y));
      |                                                  ^~~~~
a.cc:62:31: note: the lambda has no capture-default
   62 |         auto getCost = [&towns] (int a, int b) -> int {
      |                               ^
a.cc:55:27: note: '<typeprefixerror>towns' declared here
   55 |         std::vector<Town> towns(n);
      |                           ^~~~~
a.cc:63:67: error: 'towns' is not captured
   63 |                 return std::min(abs(towns[a].x - towns[b].x), abs(towns[a].y - towns[b].y));
      |                                                                   ^~~~~
a.cc:62:31: note: the lambda has no capture-default
   62 |         auto getCost = [&towns] (int a, int b) -> int {
      |                               ^
a.cc:55:27: note: '<typeprefixerror>towns' declared here
   55 |         std::vector<Town> towns(n);
      |                           ^~~~~
a.cc:63:80: error: 'towns' is not captured
   63 |                 return std::min(abs(towns[a].x - towns[b].x), abs(towns[a].y - towns[b].y));
      |                                                                                ^~~~~
a.cc:62:31: note: the lambda has no capture-default
   62 |         auto getCost = [&towns] (int a, int b) -> int {
      |                               ^
a.cc:55:27: note: '<typeprefixerror>towns' declared here
   55 |         std::vector<Town> towns(n);
      |                           ^~~~~