Language: C++

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

#define MAX 10000
#define INFTY (1<<29)

class DisjointSet {
public:
	vector<int> rank, p;

	DisjointSet() {}
	DisjointSet(int size) {
		rank.resize(size, 0);
		p.resize(size, 0);
		for (int i = 0; i < size; i++)makeSet(i);
	}

	void makeSet(int x) {
		p[x] = x;
		rank[x] = 0;
	}

	bool same(int x, int y) {
		return findSet(x) == findSet(y);
	}

	void unnite(int x, int y) {
		link(findSet(x), findSet(y));
	}

	void link(int x, int y) {
		if (rank[x] > rank[y]) {
			p[y] = x;
		}
		else {
			p[x] = y;
			if (rank[x] == rank[y]) {
				rank[y]++;
			}
		}
	}

	int findSet(int x) {
		if (x != p[x]) {
			p[x] = findSet(p[x]);
		}
		return p[x];
	}
};

class Edge {
public:
	int source, target, cost;
	Edge(int source = 0, int target = 0, int cost = 0);
	source(source),target(target),cost(cost){}
	bool operator<(const Edge &e)const {
		return cost < e.cost;
	}
};

int kruskal(int N, vector<Edge> edges) {
	int totalCost = 0;
	sort(edges.begin(), edges.end());

	DisjointSet dset = DisjointSet(N + 1);

	for (int i = 0; i < N; i++)dset.makeSet(i);

	int source, target;
	for (int i = 0; i < edges.size(); i++) {
		Edge e = edges[i];
		if (!dset.same(e.source, e.target)) {
			totalCost += e.cost;
			dset.unnite(e.source, e.target);
		}
	}
	return totalCost;
}

int main() {
	int N, M, cost;
	int source, target;

	cin >> N >> M;

	vector<Edge> edges;
	for (int i = 0; i < M; i++) {
		cin >> source >> target >> cost;
		edges.push_back(Edge(source, target, cost));
	}

	cout << kruskal(N, edges) << endl;

	return 0;
}


Compiler output:
a.cc:58:16: error: 'source' is not a type
   58 |         source(source),target(target),cost(cost){}
      |                ^~~~~~
a.cc:58:9: error: ISO C++ forbids declaration of 'source' with no type [-fpermissive]
   58 |         source(source),target(target),cost(cost){}
      |         ^~~~~~
a.cc:58:23: error: 'int Edge::source(int)' conflicts with a previous declaration
   58 |         source(source),target(target),cost(cost){}
      |                       ^
a.cc:56:13: note: previous declaration 'int Edge::source'
   56 |         int source, target, cost;
      |             ^~~~~~
a.cc:58:31: error: 'target' is not a type
   58 |         source(source),target(target),cost(cost){}
      |                               ^~~~~~
a.cc:58:24: error: ISO C++ forbids declaration of 'target' with no type [-fpermissive]
   58 |         source(source),target(target),cost(cost){}
      |                        ^~~~~~
a.cc:58:38: error: 'int Edge::target(int)' conflicts with a previous declaration
   58 |         source(source),target(target),cost(cost){}
      |                                      ^
a.cc:56:21: note: previous declaration 'int Edge::target'
   56 |         int source, target, cost;
      |                     ^~~~~~
a.cc:58:44: error: 'cost' is not a type
   58 |         source(source),target(target),cost(cost){}
      |                                            ^~~~
a.cc:58:39: error: ISO C++ forbids declaration of 'cost' with no type [-fpermissive]
   58 |         source(source),target(target),cost(cost){}
      |                                       ^~~~
a.cc:58:50: error: 'int Edge::cost(int)' conflicts with a previous declaration
   58 |         source(source),target(target),cost(cost){}
      |                                                  ^
a.cc:56:29: note: previous declaration 'int Edge::cost'
   56 |         int source, target, cost;
      |                             ^~~~
a.cc: In member function 'int Edge::cost(int)':
a.cc:58:50: warning: no return statement in function returning non-void [-Wreturn-type]
   58 |         source(source),target(target),cost(cost){}
      |                                                  ^