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;
int N,M;
struct edge{
  int to,from,c;
  bool operator < (const edge &e) const {
    return c < e.c;
  }
};

struct uni{
  int d[555];
  void init(){ memset(d,-1,sizeof(d)); }
  int find(int id){ return d[id] < 0 ? id : (d[id] = find(d[id])); }
  void marge(int a,int b){
    a = find(a); b = find(b);
    if( a==b ) return;
    if( d[a] > d[b] ) swap(a,b);
    d[a]+=d[b]; d[b] = a;
  }
  bool same(int a,int b){ return find(a) == find(b); }
};

vector<edge> E;
vector<edge> use;
vector<int> G[501];
uni U;

void dfs(int *A, int id, int from){
  A[id] = 1;
  for(int i=0;i<(int)G[id].size();i++){
    if( G[id][i] == from ) continue;
    dfs(A,G[id][i],id);
  }
}

int main(){
  cin >> N >> M;
  U.init();
  for(int i=0;i<M;i++){
    int a,b,c; cin >> a >> b >> c;
    E.push_back( (edge){a,b,c} );    
  }
  sort(E.begin(),E.end());
  for(int i=0;i<(int)E.size();i++){
    int a = E[i].from, b = E[i].to;
    if( !U.same(a,b) ){
      U.marge(a,b);
      use.push_back( E[i] );
      G[a].push_back( b );
      G[b].push_back( a );
    }
  }

  int res1 = 0, res2 = 0;
  for(int i=0;i<(int)use.size();i++){
    bool flag = true;
    int A[501]={};
    int B[501]={};
    int a = use[i].from, b = use[i].to;
    dfs(A,a,b);
    dfs(B,b,a);
    for(int j=0;j<(int)E.size();j++){
      int t = E[j].to, f = E[j].from, c = E[j].c;
      if( A[t] && B[f] ){
        if( t == a && f == b ) continue;
        if( c == use[i].c ) flag = false;
      }
      if( B[t] && A[f] ){
        if( t == b && f == a ) continue;
        if( c == use[i].c ) flag = false;
      }
      if(!flag) break;
    }
    if( flag ){
      res1++; res2 += use[i].c;
    }
  }
  cout << res1 << " " << res2 << endl;

}