After channels are cut one by one in a fixed order, report the minimum spanning forest weight of the surviving graph before each cut, or FAIL if it is disconnected.
Medium6Union-findGraphMinimum spanning treeSortingNo attempts yetTime limit5sMemory limit512 MBTask force Alpha X has been handed one mission: wipe out every frog and every frog pusher in the country within the next three to six months. Frogs are dangerous because they attack the communication channels between people. Once a frog attacks a channel, that channel is cut and can never be used again.
The task force has N members, and there are C communication channels among them. Two members exchange messages only while a surviving channel connects them, either directly or through other members.
The same pair of members may start out with several channels between them. Communicating over a channel carries risk. The channel may not be secure enough against hackers, or it may be physically tampered with and ruin the message. This risk is written as a risk index (RI), and every channel has its own RI value.
Sometimes an important message has to reach the whole group. It starts from one member, who sends it to a few others, and each member who receives it passes it on further. A member does not have to send the message to someone a less risky route already reaches. In the end every member must receive the message. The total risk index (TRI) of one group message is the sum of the RI of every channel used to carry it.
The task force wants the smallest possible TRI for each group message, and that minimum is called the LTRI. The LTRI is the same no matter who sends the message first. The intelligence squad has already worked out the order in which the frogs will attack the channels.
For each attack, compute the LTRI over the channels that are still alive immediately before it happens.
The first line contains the number of test cases T.
The first line of each test case contains the number of members N and the number of channels alive at the start C.
The next C lines describe the channels in the order the frogs attack them. The i-th of those lines contains three integers a, b, and r, meaning a channel between member a and member b with risk index r. It is possible that a and b are equal.
Constraints
For each test case, first print a line of the form Case #X:, where X is the test case number starting from 1.
Then print C lines. The i-th of those lines holds the LTRI immediately before the i-th channel of the input is attacked. At that moment the first i−1 channels are already cut and channels i through C are still alive. If a group message cannot reach every member, print FAIL on that line instead.