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;
typedef vector<int> vi;

class UF{
  vi p,r;

public:
  UF(int n=1):p(n,-1),r(n,0){ }

  int find(int x){ return (p[x]<0)?x:(p[x] = find(p[x])); }

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

    if(r[x] < r[y])p[x] = y;
    else p[y] = x;
    if(r[x] == r[y])r[x]++;
  }

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

struct edge{
  int a,b,c;
  edge(int x,int y,int z):a(x),b(y),c(z){}
  bool operator<(const edge e)const{ return c<e.c; };
};

vector<edge> ve;
vector<int> use;
UF uf;

int Kruskal(int del){
  int res = 0;
  for(int i=0;i<ve.size();i++){
    if(del == i)continue;
    if(uf.same(ve[i].a,ve[i].b))continue;
    uf.unite(ve[i].a,ve[i].b);
    res += ve[i].c;
    if(del<0)use[i] = 1;
  }
  return res;
}

int main(){
  int n,m;
  cin >> n >> m;

  ve.clear();
  for(int i=0;i<m;i++){
    int s,d,c;
    cin >> s >> d >> c; s--; d--;
    ve.push_back(edge(s,d,c));
  }
  sort(ve.begin(),ve.end());

  use.resize(m,0); uf = UF(n);
  int minv = Kruskal(-1);

  int ans = 0, ansv = 0;
  for(int i=0;i<m;i++){
    if(use[i]==0)continue;
    uf = UF(n);
    if(Kruskal(i) != minv){
      ans++; ansv += ve[i].c;
    }
  }
  cout << ans << " " << ansv << endl;
}