Fast Spanning Tree
Time limit5sMemory limit512 MB
Given weighted vertices and m conditional edges (a, b, s), find the indices of the edges added in order, where an edge is added if it joins two components whose total weight is at least s.
- Level
Hard8 of 10
- Topics
- Union-find, Divide and conquer, Sorting
- Solved
- No attempts yet
Statement
Wang Xiuhan has an initially empty undirected graph on vertices.
Each vertex has a weight, which is a non-negative integer.
He also has tuples , where , , and is a non-negative integer.
After that, he starts the following process:
- If there is no such that and lie in different connected components and (total weight of the component of ) + (total weight of the component of ) , the process ends.
- Otherwise, he chooses the smallest such , adds an edge between and , writes in the notepad, and repeats the process on the larger graph.
The process is finished, and then a misfortune happens: someone stole his notepad! Can you help him restore all the numbers efficiently?
Input
The first line contains two integers and : the number of vertices in the graph and the number of tuples ().
The second line contains space-separated integers , the weights of the vertices ().
Each of the next lines contains three integers , , (, , ).
Output
On the first line, print the number of integers written in the notepad. On the next line, print all these integers in the order he wrote them.