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 minimum spanning tree T in a graph proceeds as follows:
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 < n and 0 ≤ w < 231. Input is terminated with a line containing n = m = 0; this case should 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 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.
範例輸入 1
3 3 0 1 1 1 2 2 2 0 3 4 5 0 1 1 1 2 2 2 3 3 3 1 4 0 2 0 3 1 0 1 1 0 0
範例輸出 1
3 2 4 forest
Pro 專屬功能: 查看這題在歷屆 CPE 出現過幾次 — 升級以解鎖.