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<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
//#define int  long long 
using namespace std;
int dis[505], n, m, num, ans, k;
int find(int x)
{
	return  x == dis[x] ? x : dis[x] = find(dis[x]);
}
void uni(int x, int y)
{
	int xx = find(x), yy = find(y);
	if (xx != yy)
		dis[xx] = yy;
}
struct node{
	int x, y, c;
	friend bool operator < (node a, node b)
	{
		return a.c < b.c;
	}
}s[50005];
map<int, vector<int> > mp;
vector<int> vec;
signed main()
{
	while (~scanf("%d%d", &n, &m))
	{
		vec.clear();
		mp.clear();
		k = num = ans = 0;
		for (int i = 1; i <= n; i++)	dis[i] = i;
		for (int i = 0; i < m; i++)
		{
			scanf("%d%d%d", &s[i].x, &s[i].y, &s[i].c);
		}
		sort(s, s + m);
		for (int i = 0; i < m; i++)
		{
			if (k == n - 1)	break;
			if (find(s[i].x) != find(s[i].y))
			{
				uni(s[i].x, s[i].y);
				k++;
				vec.push_back(i);
			}
		}
		for (int i = 0; i < m; i++)
		{
			mp[s[i].c].push_back(i);
		}
		for (int i = 0; i < vec.size(); i++)
		{
			for (int j = 1; j <= n; j++)	dis[j] = j;
			for (int j = 0; j < vec.size(); j++)
			{
					if (j != i)
						uni(s[vec[j]].x, s[vec[j]].y);
			}
			int ct = s[vec[i]].c;
			k = 0;
			for (int j = 0; j < mp[ct].size(); j++)
			{
				if (find(s[mp[ct][j]].x) != find(s[mp[ct][j]].y))
				{
					k++;
				}
			}
			if (k == 1)	
			{
				num++;
				ans += ct;
			}
		}
		printf("%d %d\n", num, ans);
	}
}