Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Problem F:
There is No Alternative

ICPC (Isles of Coral Park City) consist of several beautiful islands.

The citizens requested construction of bridges between islands to resolve inconveniences of using boats between islands, and they demand that all the islands should be reachable from any other islands via one or more bridges.

The city mayor selected a number of pairs of islands, and ordered a building company to estimate the costs to build bridges between the pairs. With this estimate, the mayor has to decide the set of bridges to build, minimizing the total construction cost.

However, it is difficult for him to select the most cost-efficient set of bridges among those connecting all the islands. For example, three sets of bridges connect all the islands for the Sample Input 1. The bridges in each set are expressed by bold edges in Figure F.1.

Figure F.1. Three sets of bridges connecting all the islands for Sample Input 1

As the first step, he decided to build only those bridges which are contained in all the sets of bridges to connect all the islands and minimize the cost. We refer to such bridges as no alternative bridges. In Figure F.2, no alternative bridges are drawn as thick edges for the Sample Input 1, 2 and 3.

Figure F.2. No alternative bridges for Sample Input 1, 2 and 3

Write a program that advises the mayor which bridges are no alternative bridges for the given input.

Input

The input consists of a single test case.

$N$ $M$

$S_1$ $D_1$ $C_1$

.

.

.

$S_M$ $D_M$ $C_M$

The first line contains two positive integers $N$ and $M$. $N$ represents the number of islands and each island is identified by an integer 1 through $N$. $M$ represents the number of the pairs of islands between which a bridge may be built.

Each line of the next $M$ lines contains three integers $S_i$, $D_i$ and $C_i$ ($1 \leq i \leq M$) which represent that it will cost $C_i$ to build the bridge between islands $S_i$ and $D_i$. You may assume $3 \leq N \leq 500$, $N − 1 \leq M \leq min(50000, N(N − 1)/2)$, $1 \leq S_i < D_i \leq N$, and $1 \leq C_i \leq 10000$. No two bridges connect the same pair of two islands, that is, if $i \ne j$ and $S_i = S_j$, then $D_i \ne D_j$ . If all the candidate bridges are built, all the islands are reachable from any other islands via one or more bridges.

Output

Output two integers, which mean the number of no alternative bridges and the sum of their construction cost, separated by a space.

Sample Input 1

4 4
1 2 3
1 3 3
2 3 3
2 4 3

Sample Output 1

1 3

Sample Input 2

4 4
1 2 3
1 3 5
2 3 3
2 4 3

Sample Output 2

3 9

Sample Input 3

4 4
1 2 3
1 3 1
2 3 3
2 4 3

Sample Output 3

2 4

Sample Input 4

3 3
1 2 1
2 3 1
1 3 1

Sample Output 4

0 0

Solution in C++ (judge verdict: Accepted)

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
#include <complex>
 
using namespace std;
 
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P > PP;

typedef complex<double> Point;
const static double EPS = 1e-12;
const static int MAX_V = 501;

class UnionFindTree {
  int _par[501];
  int _rank[501];
public:
  UnionFindTree(int N){
    for(int i = 0; i < N; i++){
      _par[i] = i;
      _rank[i] = 0;
    }
  }

  UnionFindTree(int par[501]){
    memcpy(_par,par,sizeof(int)*501);
    memset(_rank,0,sizeof(_rank));
  }

  int find(int u){
    if(_par[u] == u) return u;
    return find(_par[u]);
  }

  bool unite(int u,int v){
    u = find(u);
    v = find(v);
    if(u == v) return false;
    
    if(_rank[u] < _rank[v]){
      _par[u] = v;
    }
    else{
      _par[v] = u;
      if(_rank[u] == _rank[v]) _rank[u]++;
    }
    return true;
  }
};

class Edge {
public:
  int from;
  int to;
  int cost;
  int id;
  Edge(int from,int to,int cost,int id) 
    : from(from), to(to), cost(cost), id(id) {}
  bool operator<(const Edge& e) const {
    return cost < e.cost;
  }
  bool operator>(const Edge& e) const {
    return cost > e.cost;
  }
};

int num_of_islands;
int num_of_pairs;
vector<int> graph[501];

bool bfs(int from,int to){
  bool visited[501];
  memset(visited,false,sizeof(visited));
  queue<int> que;
  que.push(from);
  while(!que.empty()){
    int current = que.front();
    visited[current] = true;
    if(visited[to]) return false;

    que.pop();
    for(int i = 0; i < graph[current].size(); i++){
      if(visited[graph[current][i]]) continue;
      if((current == from && graph[current][i] == to)
         || (current == to && graph[current][i] == from)) continue;
      visited[graph[current][i]] = true;
      que.push(graph[current][i]);
    }
  }

  return true;
}

int main(){
  while(~scanf("%d %d",&num_of_islands,&num_of_pairs)){
    priority_queue<Edge,vector<Edge>, greater<Edge> > que;
    vector<Edge> orig;
    int par[501];
    for(int i = 0; i <= 500; i++){
      graph[i].clear();
      par[i] = i;
    }

    for(int i = 0; i < num_of_pairs; i++){
      int from,to;
      int cost;
      scanf("%d %d %d",&from,&to,&cost);
      from--; to--;
      que.push(Edge(from,to,cost,i));
      orig.push_back(Edge(from,to,cost,i));
      graph[from].push_back(to);
      graph[to].push_back(from);
    }

    UnionFindTree uft(par);

    vector<Edge> candidates;
    int sum = 0;
    while(!que.empty()){
      Edge e = que.top();
      que.pop();
      if(uft.unite(e.from,e.to)){
        sum += e.cost;
        candidates.push_back(e);
      }
    }
    int res_cost = 0;
    int res_count = 0;
    sort(orig.begin(),orig.end());

    for(int candidate_i = 0; candidate_i < candidates.size(); candidate_i++){
      int from = candidates[candidate_i].from;
      int to = candidates[candidate_i].to;
      int id = candidates[candidate_i].id;

      UnionFindTree uft2(par);
      int sum2 = 0;
      for(int i = 0; i < orig.size(); i++){
        if(id == orig[i].id) continue;
        if(uft2.unite(orig[i].from,orig[i].to)){
          sum2 += orig[i].cost;
        }
      }
      if(sum2 != sum || bfs(from,to)){
        res_cost += candidates[candidate_i].cost;
        res_count++;
      }
    }
    printf("%d %d\n",res_count,res_cost);
  }
}