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;

struct UnionFind{
	int count;
	vector<int> data;
	UnionFind(int n):count(n),data(n,-1){}
	int Find(int i){
		return data[i]<0?i:(data[i]=Find(data[i]));
	}
	bool Unite(int a,int b){
		a=Find(a),b=Find(b);
		if(a==b) return false;
		if(-data[a]<-data[b]) swap(a,b);
		data[a]+=data[b];
		data[b]=a;
		count--;
		return true;
	}
	int Count(){
		return count;
	}
};

typedef tuple<int,int,int> tiii;

int main()
{
	for(int n,m;cin>>n>>m && n|m;){
		vector<tiii> es;
		for(int i=0;i<m;i++){
			int u,v,w; cin>>u>>v>>w; u--,v--;
			es.emplace_back(u,v,w);
		}
		sort(begin(es),end(es),[&](const tiii& a,const tiii& b){return get<2>(a)<get<2>(b);});
		
		vector<int> used(m);
		int weight=0;
		{
			UnionFind uf(n);
			for(int i=0;i<m;i++){
				int u,v,w; tie(u,v,w)=es[i];
				if(uf.Unite(u,v)){
					weight+=w;
					used[i]=1;
				}
			}
		}
		
		int count=0,sum=0;
		for(int i=0;i<m;i++) if(used[i]){
			auto cand=es[i];
			es.erase(begin(es)+i);
			int tmp=0;
			UnionFind uf(n);
			for(auto e:es){
				int u,v,w; tie(u,v,w)=e;
				tmp+=uf.Unite(u,v)*w;
			}
			if(uf.Count()>1 || tmp>weight){
				count++;
				sum+=get<2>(cand);
			}
			es.insert(begin(es)+i,cand);
		}
		cout<<count<<' '<<sum<<endl;
	}
}