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 <bits/stdc++.h>

using namespace std;

#define int long long

constexpr int INF = 1LL << 60;

constexpr int MAX_N = 505;
constexpr int MAX_M = 50005;

typedef pair<int, int> pii;
typedef pair<int, pii> pipii;

struct UF
{
    int n, par[MAX_N], rank[MAX_N];

    void init(int pn)
    {
        n = pn;
        for (int i = 1; i <= n; i++)
        {
            par[i] = i;
            rank[i] = 0;
        }
    }

    void unite(int x, int y)
    {
        x = find(x);
        y = find(y);
        if (x == y) return;

        if (rank[x] < rank[y])
        {
            par[x] = y;
        }
        else
        {
            par[y] = x;
            if (rank[x] == rank[y]) rank[x]++;
        }
    }

    int find(int x)
    {
        if (par[x] == x) return x;
        return par[x] = find(par[x]);
    }

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

int n, m;
pipii edges[MAX_M];
vector<int> nomi;
UF uf;

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> m;
    uf.init(n);
    for (int i = 0; i < m; i++)
    {
        int s, d, c;
        cin >> s >> d >> c;
        edges[i] = {c, {s, d}};
    }

    sort(edges, edges + m);

    int cost = 0;
    for (int i = 0; i < m; i++)
    {
        const auto p = edges[i].second;
        if (uf.same(p.first, p.second)) continue;
        uf.unite(p.first, p.second);
        nomi.emplace_back(i);
        cost += edges[i].first;
    }

    int ansCount = 0, ansCost = 0;
    for (const auto idx : nomi)
    {
        uf.init(n);
        int tmpCost = 0;
        for (int i = 0; i < m; i++)
        {
            if (idx == i) continue;
            const auto p = edges[i].second;
            if (uf.same(p.first, p.second)) continue;
            uf.unite(p.first, p.second);
            tmpCost += edges[i].first;
        }
        if (cost != tmpCost)
        {
            ansCount++;
            ansCost += edges[idx].first;
        }
    }

    cout << ansCount << " " << ansCost << endl;
}