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 <iostream>
#include <vector>
using namespace std;

struct UnionFind {
    vector<int> data;
    int __size;
    UnionFind(int size) : data(size, -1), __size(size) { }
    bool unionSet(int x, int y) {
        if ((x = root(x)) != (y = root(y))) {
            if (data[y] < data[x]) swap(x, y);
            data[x] += data[y]; data[y] = x; __size--;
        }
        return x != y;
    }
    bool findSet(int x, int y) { return root(x) == root(y); }
    int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
    int size(int x) { return -data[root(x)]; }
    int size() { return __size; }
};

template <typename T>
struct Edge { int src, dst; T cost;
    Edge(int dst, T cost) : src(-1), dst(dst), cost(cost) { }
    Edge(int src, int dst, T cost) : src(src), dst(dst), cost(cost) { }
};
template <typename T> using Edges = vector<Edge<T>>;

Edges<int> edges;
vector<vector<pair<int, int>>> mst;
vector<int> noAlt;
int dfs(int u, int p, int goal, int tgt_c) {
    if (u == goal) return true;
    for (auto e: mst[u]) if (e.first != p) {
        int v = e.first;
        Edge<int> edge = edges[e.second];
        if (dfs(v, u, goal, tgt_c)) {
            if (edge.cost == tgt_c) noAlt[e.second] = false;
            return true;
        }
    }
    return false;
}

template<typename T>
void kruskal(Edges<T> &edges, int v) {
    mst.resize(v);
    UnionFind uf(v);
    for (int i = 0; i < edges.size(); i++) {
        Edge<T> e = edges[i];
        int u = e.src, v = e.dst, c = e.cost;
        if (uf.unionSet(u, v)) {
            mst[u].emplace_back(v, i);
            mst[v].emplace_back(u, i);
            noAlt[i] = true;
        } else {
            dfs(u, -1, v, c);
        }
    }
}

int main() {
    int n, m; cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int s, d, c; cin >> s >> d >> c; s--, d--;
        edges.emplace_back(s, d, c);
    }
    sort(begin(edges), end(edges), [](const Edge<int> &a, const Edge<int> &b) {
        return a.cost < b.cost;
    });
    noAlt.resize(m);
    kruskal(edges, n);
    int cnt = 0, cost = 0;
    for (int i = 0; i < m; i++) if (noAlt[i]) {
        cnt++; cost += edges[i].cost;
    }
    cout << cnt << " " << cost << endl;
    return 0;
}