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;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')';
  return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'[';
  REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';}
  out<<']';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

class UnionFind {
public:
  const static int MAX_N = 505;
  int par[MAX_N];
  int s[MAX_N];
  UnionFind() { init(); }
  UnionFind(int n) { init(n); }
  void init() { for(int i=0; i<MAX_N; ++i) par[i] = i, s[i] = 1; }
  void init(int n) { for(int i=0; i<n; ++i) par[i] = i, s[i] = 1; }
  int find(int x) {
    if(par[x] == x) return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if(x == y) return;
    if(s[x] < s[y]) par[x] = y, s[y] = s[x] + s[y];
    else par[y] = x, s[x] = s[x] + s[y];
  }
  bool same(int x, int y) { return find(x) == find(y);}
};
UnionFind uf;

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

  int n, m;
  cin >> n >> m;
  VVI edge(m, VI(3, 0));
  REP(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    edge[i] = {c, a, b};
  }

  VVI g(500, VI());
  VVI tree;
  sort(ALL(edge));
  REP(i, m) {
    if(uf.same(edge[i][1], edge[i][2])) continue;
    uf.unite(edge[i][1], edge[i][2]);
    g[edge[i][1]].PB(edge[i][2]);
    g[edge[i][2]].PB(edge[i][1]);
    tree.PB(edge[i]);
  }

  int cnt = 0, ret = 0;
  int type[505] = {};
  bool used[505] = {};

  // cout << tree << endl;
  REP(i, tree.size()) {
    function<void(int,int)> dfs = [&](int v, int t) {
      type[v] = t;
      used[v] = true;
      for(auto e: g[v]) {
        bool cond = tree[i][1] == v && tree[i][2] == e;
        cond |= tree[i][2] == v && tree[i][1] == e;
        if(!used[e] && !cond) dfs(e, t);
      }
    };
    // tree[i] を削除したときの頂点の分割
    int t = 0;
    memset(used, false, sizeof(used));
    REP(j, n) {
      if(used[j]) continue;
      dfs(j, t);
      t++;
    }
    bool ok = true;
    REP(j, m) {
      // cout << j << " " << edge[j] << " " << type[j] << endl;
      // j番目のedgeの両端のtypeが異なる
      if(tree[i] == edge[j]) continue;
      if(type[edge[j][1]] != type[edge[j][2]] && tree[i][0] == edge[j][0]) {
        ok = false;
        break;
      }
    }
    if(ok) {
      cnt++;
      ret += tree[i][0];
    }
  }

  cout << cnt << " " << ret << endl;

  return 0;
}