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<iostream>
#include<vector>
#include<algorithm>
using namespace std;

#define int long long

#define f first
#define s second

int setnum;
vector<int> par, siz;
vector<int> MST;

void init(int x){
  par.resize(x);
  siz.resize(x);
  setnum = x;
  
  for(int i = 0; i < x; i++){
    par[i] = i;
    siz[i] = 1;
  }
}

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

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

  if(x == y) return;

  setnum--;

  if(siz[x] > siz[y]){
    siz[x] += siz[y];
    par[y] = x;
  } else {
    siz[y] += siz[x];
    par[x] = y;
  }
}

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

signed main(){
  int N, M;
  int cost = 0, con = 0, mstc = 0;
  vector<pair<int,pair<int,int>>> edge, mst, edge2;

  cin>>N>>M;

  for(int i = 0; i < M; i++){
    int S, D, C;

    cin>>S>>D>>C;
    S--, D--;

    edge.push_back({C,{S,D}});
  }

  sort(edge.begin(), edge.end());

  init(N);

  for(int i = 0; i < edge.size(); i++){
    if(!same(edge[i].s.f, edge[i].s.s)){
      unite(edge[i].s.f, edge[i].s.s);
      mst.push_back(edge[i]);
      mstc += edge[i].f;
    }
  }

  con = N-1;
  cost = mstc;
  
  for(int i = 0; i < mst.size(); i++){//
    int mstc2 = 0;
    edge2.clear();
    for(int j = 0; j < edge.size(); j++){
      if(mst[i] != edge[j]) edge2.push_back(edge[j]);
    }
    
    init(N);
    
    sort(edge2.begin(), edge2.end());
    
    for(int j = 0; j < edge2.size(); j++){
      if(!same(edge2[j].s.f, edge2[j].s.s)){
        unite(edge2[j].s.f, edge2[j].s.s);
        mstc2 += edge2[j].f;
      }
    }

    if(setnum == 1 && mstc2 == mstc){
      con--;
      cost -= mst[i].f;
    }

  }

  cout<<con<<" "<<cost<<endl;
  
  return 0;
}