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;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int n,m;
vector<P1>edge[10005];
int par[505],ran[505];
void init(){ for(int i=0;i<505;i++) par[i] = i; }
int find(int x){ if(x == par[x]) return x; else return par[x] = find(par[x]); }
void unite(int x,int y){
	x = find(x); y = find(y); if(x==y) return;
	if(ran[x] < ran[y]) par[x] = y;
	else{
		par[y] = x;
		if(ran[x] == ran[y]) ran[x]++;
	}
}
bool same(int x,int y){ return find(x)==find(y); }
vector<P>G[505];
int cnt[505];
bool used[505];
int dep[505];
vector<P>H[505];

int cnty;
ll sum;

void dfs(int v,int u,int d){
	if(used[v]) return;
	dep[v] = d; used[v] = 1;
	for(int i=0;i<G[v].size();i++){
		int to = G[v][i].fi;
		if(to ==  u) continue;
		if(!used[to]){
			H[v].pb(G[v][i]);
			dfs(to,v,d+1);
			//cout<<v<<" "<<to<<endl;
		}
		else{
			if(dep[v] > dep[to]){
				cnt[v]++;
				cnt[to]--;
			}
			else{
				cnt[v]--;
				cnt[to]++;
			}
			//cout<<v<<"a "<<to<<endl;
		}
	}
}
int dfs2(int v,int u,int add){
	int x = cnt[v];
	for(int i=0;i<H[v].size();i++){
		int to = H[v][i].fi;
		int nm = H[v][i].sc;
		int ret = dfs2(to,v,add);
		if(ret == 0){
			cnty++;
			sum+=add;
		}
		x += ret;
	}
	return x;
}
int main(){
	cin>>n>>m;
	rep(i,m){
		int a,b,d; cin>>a>>b>>d;
		edge[d].pb(mp(i,mp(a,b)));
	}
	init();
	rep(i,10005){
		rep(i,505){
			G[i].clear();
			H[i].clear();
		}
		rep(j,edge[i].size()){
			int a = edge[i][j].sc.fi;
			int b = edge[i][j].sc.sc;
			a = find(a);
			b = find(b);
			if(a != b){
				G[a].pb(mp(b,edge[i][j].fi));
				G[b].pb(mp(a,edge[i][j].fi));
			}
		}
		memset(used,0,sizeof(used));
		memset(cnt,0,sizeof(cnt));
		rep(j,505){
			if(used[j]) continue;
			dfs(j,-1,0);
			dfs2(j,0,i);
		}
		rep(j,edge[i].size()){
			int a = edge[i][j].sc.fi;
			int b = edge[i][j].sc.sc;
			unite(a,b);
		}
	}
	cout<<cnty<<" "<<sum<<endl;
}