Problem: Example

Input

4 4
1 2 3
1 3 3
2 3 3
2 4 3


Output

1 3

Solution (in CPP):

Solution:
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cstring>
#include <climits>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; int i; bool used; };
 
int MOD = 1000000007;

struct union_find {
	vector<int> v;
	union_find(int n) : v(n, -1) {}
	int find(int x) { return (v[x] < 0) ? x : (v[x] = find(v[x])); }
	void unite(int x, int y) {
		x = find(x); y = find(y);
		if (x != y) {
			if (-v[x] < -v[y]) swap(x, y);
			v[x] += v[y]; v[y] = x;
		}
	}
	bool same(int x, int y) { return find(x) == find(y); }
	int size(int x) { return -v[find(x)]; }
};

bool kr_cmp(const edge& l, const edge& r) { return ll_i(l.w, l.i) < ll_i(r.w, r.i); }

ll kruskal(int n, vector<edge>& E) {
	sort(E.begin(), E.end(), kr_cmp);
	union_find uf(n);
	ll res = 0;
	for (int i = 0; i < E.size(); i++) {
		edge& e = E[i];
		if (uf.same(e.u, e.v)) continue;
		e.used = true;
		uf.unite(e.u, e.v);
		res += e.w;
	}
	return res;
}

int main() {
	int N, M; cin >> N >> M;
	vector<int> S(M), D(M), C(M);
	for (int i = 0; i < M; i++)
		cin >> S[i] >> D[i] >> C[i];
	vector<bool> a(M, true);
	for (;;) {
		vector<edge> E(M);
		for (int i = 0; i < M; i++) {
			E[i].i = (a[i] ? i : -1);
			E[i].u = S[i] - 1;
			E[i].v = D[i] - 1;
			E[i].w = C[i];
			E[i].used = false;
		}
		kruskal(N, E);
		bool updated = false;
		for (int i = 0; i < M; i++)
			if (E[i].i != -1 && !E[i].used) {
				a[E[i].i] = false;
				updated = true;
			}
		if (!updated) break;
	}
	int cnt = 0, sum = 0;
	for (int i = 0; i < M; i++)
		if (a[i]) {
			cnt++;
			sum += C[i];
		}
	cout << cnt << ' ' << sum << endl;
}