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;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

struct Node{
	int s,t,c;
	bool operator< (Node a) const { return c < a.c; }
};

Node node[50010];
bool used[50010];

vector<pii> tree[510];
int n,m;

class UF{
	public:
		void init(int n){
			par.resize(n);
			rank.resize(n);
			rep(i,n) par[i]=i,rank[i]=0;
			return;
		}
		int find(int x){
			if(par[x]==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(rank[x]<rank[y]){
				par[x]=y;
			}else{
				par[y]=x;
				if(rank[x]==rank[y]) rank[x]++;
			}
		}
		bool same(int x,int y){
			return find(x)==find(y);
		}
		private:	
			vi par,rank;
};

bool dfs(int v,int p,int c,int r){
	if(v==r)
		return true;
	bool ok=false;
	rep(i,tree[v].size()){
		int v2=tree[v][i].first;
		if(v2!=p){
			bool ok2=dfs(v2,v,c,r);
			if(ok2&&node[tree[v][i].second].c==c)
				used[tree[v][i].second]=false;
			ok|=ok2;
		}
	}
	return ok;
}

int main(void){
	cin >> n >> m;
	
	rep(i,m) {
		cin >> node[i].s >> node[i].t >> node[i].c;
		node[i].s--;node[i].t--;
		if(node[i].t < node[i].s) swap(node[i].s,node[i].t);
	}

	sort(node,node+m);

	UF uf;
	uf.init(n);

	rep(i,m){
		Node e=node[i];
		if(uf.same(e.s,e.t)){
			dfs(e.t,-1,e.c,e.s);
		}else{
			uf.unite(e.s,e.t);
			pii in; 
			in.first=e.t; in.second=i;
			tree[e.s].pb(in);
			in.first=e.s; 
			tree[e.t].pb(in);
			used[i]=true;
		}
	}
	int ai=0;
	int ac=0;
	rep(i,m){
		if(used[i]){
			ai++;
			ac+=node[i].c;
		}
	}
	cout << ai <<" "<< ac << endl;
	return 0;
}