Heavy Cycle Edges

Given an undirected graph with edge weights, a minimum spanning tree is a subset of edges of minimum total weight such that any two nodes are connected by some path containing only these edges. A popular algorithm for finding the mini- mum spanning tree T in a graph proceeds as follows: • let T be initially empty • consider the edges e1,...,em in in- creasing order of weight – add ei to T if the endpoints of ei are not connected by a path in T An alternative algorithm is the follow- ing: • let T be initially the set of all edges • while there is some cycle C in T – remove edge e from T where e has the heaviest weight in C Your task is to implement a function related to this algorithm. Given an undirected graph G with edge weights, your task is to output all edges that are the heaviest edge in some cycle of G. Input The first input of each case begins with integers n and m with 1 ≤ n ≤ 1,000 and 0 ≤ m ≤ 25,000 where n is the number of nodes and m is the number of edges in the graph. Following this are m lines containing three integers u, v, and w describing a weight w edge connecting nodes u and v where 0≤u,v<nand0≤w<231. Inputisterminatedwithalinecontainingn=m=0;thiscaseshould not be processed. You may assume no two edges have the same weight and no two nodes are directly connected by more than one edge. Output Output for an input case consists of a single line containing the weights of all edges that are the heaviest edge in some cycle of the input graph. These weights should appear in increasing order and consecutive weights should be separated by a space. If there are no cycles in the graph then output the text ‘forest’ instead of numbers.

2/2 Sample Input 33 011 122 203 45 011 122 233 314 020 31 011 00 Sample Output 3 24 forest