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;

class UnionFind{
private:
    vector<int> p,s;
	int cnt;
public:
	UnionFind(){}
	UnionFind(int N){
		cnt = N;
		p = s = vector<int>(N+1,0);
		for(int i=1;i<=N;i++){
			p[i] = i; s[i] = 1;
		}
	}
	int find(int x){
		if(p[x]==x) return x;
		else return p[x] = find(p[x]);
	}
	void unite(int x,int y){
		x = find(x); y = find(y);
		if(x==y) return;
		if(s[x]>s[y]){
			p[y] = x;
			s[x] += s[y];
		}else{
			p[x] = y;
			s[y] += s[x];
		}
		cnt--;
	}
	bool is_same_set(int x,int y) {return find(x)==find(y);}
	int size(int x) {return s[find(x)];}
	int compnents_number(){return cnt;}
};

int N,M;

struct edge{
    int s,t,c;
    bool operator<(const edge& r)const{
        return c<r.c;
    }
};

int main(){
    cin >> N >> M;
    vector<edge> v,u;
    int s,t,c;
    for(int i=0;i<M;i++){
        cin >> s >> t >> c;
        v.push_back({s,t,c});
    }
    sort(v.begin(),v.end());
    int cost = 0;
    UnionFind uf(N);
    for(auto x:v){
        if(!uf.is_same_set(x.s,x.t)){
            u.push_back(x);
            uf.unite(x.s,x.t);
            cost += x.c;
        }
    }
    int ans = 0,cnt = 0;
    for(auto y:u){
        UnionFind uf2(N);
        int cost2 = 0;
        for(auto x:v){
            if(x.s==y.s && x.t==y.t) continue;
            if(!uf2.is_same_set(x.s,x.t)){
                uf2.unite(x.s,x.t);
                cost2 += x.c;
            }
        }
        if(uf2.size(1)!=N || cost2!=cost){
            ans += y.c;
            cnt++;
        }
    }
    cout << cnt << " " << ans << endl;
}