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<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 500;
const int MAX_M = 50000;

/* typedef */

typedef vector<int> vi;

struct UFT {
  int links[MAX_N], ranks[MAX_N], sizes[MAX_N];
  
  UFT() {}

  void clear(int n) {
    for (int i = 0; i < n; i++)
      links[i] = i, ranks[i] = sizes[i] = 1;
  }

  int root(int i) {
    int i0 = i;
    while (links[i0] != i0) i0 = links[i0];
    return (links[i] = i0);
  }

  int rank(int i) { return ranks[root(i)]; }
  int size(int i) { return sizes[root(i)]; }
  bool same(int i, int j) { return root(i) == root(j); }
  
  int merge(int i0, int i1) {
    int r0 = root(i0), r1 = root(i1);
    int mr;
    if (ranks[r0] == ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      ranks[r0]++;
      mr = r0;
    }
    else if (ranks[r0] > ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      mr = r0;
    }
    else {
      links[r0] = r1;
      sizes[r1] += sizes[r0];
      mr = r1;
    }
    return mr;
  }
};

struct Edge {
  int a, b, c;
  Edge() {}
  Edge(int _a, int _b, int _c) { a = _a, b = _b, c = _c; }
  void set(int _a, int _b, int _c) { a = _a, b = _b, c = _c; }
  bool operator<(const Edge& e) const { return c < e.c; }
};

/* global variables */

UFT uft;
Edge edges[MAX_M];

/* main */

int main() {
  int n, m;
  cin >> n >> m;
  
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    edges[i].set(a, b, c);
  }

  sort(edges, edges + m);

  uft.clear(n);
  vi ve;
  int min_csum = 0;
  
  for (int i = 0; i < m; i++) {
    Edge& ei = edges[i];

    int ra = uft.root(ei.a), rb = uft.root(ei.b);
    if (ra == rb) continue;

    int r = uft.merge(ra, rb);
    min_csum += ei.c;
    ve.push_back(i);

    if (uft.size(r) == n) break;
  }
  // cout << min_csum << ' ' << ve.size() << endl;

  int nobnum = 0, nobsum = 0;

  for (int i = 0; i < ve.size(); i++) {
    uft.clear(n);
    int csum = 0;
    bool found = false;
    
    for (int j = 0; j < m; j++) {
      if (j == ve[i]) continue;

      Edge& ej = edges[j];

      int ra = uft.root(ej.a), rb = uft.root(ej.b);
      if (ra == rb) continue;

      int r = uft.merge(ra, rb);
      csum += ej.c;

      if (csum >= min_csum || uft.size(r) == n) {
	if (csum == min_csum && uft.size(r) == n) found = true;
	break;
      }
    }

    if (! found) {
      nobnum++;
      nobsum += edges[ve[i]].c;
    }
  }

  printf("%d %d\n", nobnum, nobsum);
  
  return 0;
}