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

const int M=5e4+5;
const int N=505;

namespace ufs{
	int fa[N];
	inline void reset(int n) {
		for(register int i=1;i<=n;i++)
			fa[i]=i;
		return;
	}
	int find(int x) {
		return (x==fa[x])?x:fa[x]=find(fa[x]);
	}
	inline bool merge(int x,int y) {
		int fx=find(x),fy=find(y);
		if(fx==fy) return false;
		return fa[fx]=fy,true;
	}
}

int n,m;
struct Edge{
	int u,v,w;
	bool operator <(const Edge &t)const{
		return w<t.w;
	}
}e[M];
int used[M];

signed main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(register int i=1;i<=m;i++)
			scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
		
		sort(e+1,e+1+m);
		ufs::reset(n);
		
		int sum=0;
		int ans1=0,ans2=0;
		
		for(register int cnt=0,i=1;cnt!=n-1&&i<=m;i++)
			if(ufs::merge(e[i].u,e[i].v))
				sum+=e[i].w,used[++cnt]=i;
		
		for(register int c=1;c<n;c++)
		{
			ufs::reset(n);
			int tmp=0,cnt=0;
			for(register int i=1;cnt!=n-1&&i<=m;i++)
				if(i!=used[c]&&ufs::merge(e[i].u,e[i].v))
					cnt++,tmp+=e[i].w;
			if(tmp!=sum||cnt!=n-1)
				ans1++,ans2+=e[used[c]].w;
		}
		
		printf("%d %d\n",ans1,ans2);

	}
}