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>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
struct edge{
	int a,b,c;
	edge(int x,int y,int z) : a(x),b(y),c(z) {}
};
bool comp(const edge& x,const edge& y){
	return x.c<y.c;
}

typedef vector<int> vi;
typedef pair<int,int> P;
const int MAX_V=500;
vector<int> G[MAX_V];
map<P,int> mp;
int ord[MAX_V];
bool inS[MAX_V];
stack<int> roots,S;
vector<vi> cc;
vector<P> bs;
int cnt;
int N,M;
vector<int> cans;

void init(){
	rep(i,N) G[i].clear(),ord[i]=0,inS[i]=0;
	mp.clear();
	cc.clear();
	bs.clear();
	cans.clear();
	cnt=0;
}
void add_edge(int a,int b){
	if(a>b) swap(a,b);
	G[a].pb(b);
	G[b].pb(a);
	mp[P(a,b)]++;
}
void vis(int v,int p){
	ord[v]=++cnt;
	S.push(v);
	inS[v]=true;
	roots.push(v);
	for(int u:G[v]){
		if(ord[u]==0){
			vis(u,v);
		}else if( (u!=p&&inS[u]) || (u==p&&mp[P(min(v,p),max(v,p))]>=2) ){
			while(ord[roots.top()]>ord[u]) roots.pop();
		}
	}
	if(v==roots.top()){
		bs.pb(P(p,v));
		vector<int> vc;
		while(true){
			int w=S.top();S.pop();
			inS[w]=false;
			vc.pb(w);
			if(v==w) break;
		}
		roots.pop();
		cc.pb(vc);
	}
}
void bridge(){
	rep(i,N) if(ord[i]==0){
		vis(i,-1);
		bs.pop_back();	//P(-1,hoge)
	}
}

int par[500];
int find(int x){
	if(x==par[x]) return x;
	return par[x]=find(par[x]);
}
bool same(int x,int y){
	return find(x)==find(y);
}
void unite(int x,int y){
	par[find(x)]=find(y);
}
int main(){
	cin>>N>>M;
	vector<edge> es;
	rep(i,M){
		int a,b,c;
		cin>>a>>b>>c;
		a--,b--;
		es.pb(edge(a,b,c));
	}
	sort(all(es),comp);
	int n=0,c=0;
	int now=es[0].c;
	rep(i,N) par[i]=i;
	es.pb(edge(0,0,-1));
	rep(i,M+1){
		if(es[i].c!=now){
			bridge();
			n+=bs.size(),c+=bs.size()*now;
			for(int j:cans) unite(es[j].a,es[j].b);
			now=es[i].c;
			init();
		}
		if(i==M) continue;
		if(es[i].c==now){
			if(!same(es[i].a,es[i].b)){
				int a=find(es[i].a),b=find(es[i].b);
				cans.pb(i);
				add_edge(a,b);
			}
		}
	}
	cout<<n<<" "<<c<<endl;
}