Galactic Reconstruction

시간 제한1초메모리 제한2048 MB

요약
제안된 워프 게이트를 순서대로 처리하면서 각 집단의 재산을 관리하고, 각 제안이 BUILT인지 IMPOSSIBLE인지 UNNECESSARY인지 판정한다.
난이도

보통10점 중 6점

유형
유니온 파인드, 시뮬레이션, 구현
정답자
아직 제출이 없습니다

문제

A technological disaster has struck an intergalactic civilization. For unknown reasons, all warp drives used to travel between colonies have stopped working. However, they have a new warp drive technology they can employ to connect colonies and form new clusters of colonies.

Initially, there are nn colonies numbered 11 through nn, with the ii-th colony having an initial wealth of w_iw\_i. Each colony initially belongs to its own cluster of size 11, with the wealth of the cluster being the wealth of its constituent colony. Clusters can be combined into a larger cluster by building a warp gate that connects two colonies belonging to two different clusters. That is, if cluster XX of size pp contains colonies a_1,a_2,…,a_pa\_1, a\_2, \dots, a\_p and cluster YY of size qq contains colonies b_1,b_2,…,b_qb\_1, b\_2, \dots, b\_q, then building a warp gate connecting any a_ia\_i and b_jb\_j would combine clusters XX and YY to form a single cluster ZZ of size p+qp + q, containing a_1,a_2,…,a_n,b_1,b_2,…,b_qa\_1, a\_2, \dots, a\_n, b\_1, b\_2, \dots, b\_q.

There is an ordered list of mm warp gates that have been proposed to be built. The jj-th proposal suggests building a warp gate that connects colonies a_ja\_j and b_jb\_j at a cost of c_jc\_j. If colonies a_ja\_j and b_jb\_j belong to different clusters, and both of the clusters they belong to individually have a total wealth of least c_jc\_j, then the warp gate between a_ja\_j and b_jb\_j will be built. Before building the warp gate, the clusters that a_ja\_j and b_jb\_j belong to will both spend c_jc\_j to build the warp gate. This means that both clusters each decrease their total wealth by c_jc\_j before the two clusters are combined. If a_ja\_j and b_jb\_j already belong to the same cluster, or one or both of the clusters containing a_ja\_j and b_jb\_j don't have a total wealth of at least c_jc\_j, the warp gate will not be built, and both clusters will maintain their wealth.

The proposals are reviewed in the order they appear in the list, and the next proposal is not reviewed until the previous warp gate is either built, or determined not to be built. Your task is to determine which warp gates will be BUILT, which are IMPOSSIBLE because one or both of the colonies lack the necessary wealth, and which are UNNECESSARY because the two colonies already belong to the same cluster. If it is both impossible and unnecessary to build a proposed warp gate, you should only report UNNECESSARY.

For example, consider a scenario where colonies 11 and 22 are connected by a warp gate (they form a cluster) with a total wealth of 55, and 33 and 44 also make up a cluster with a wealth of 99. If building a warp gate connecting 22 and 33 were proposed at a cost of 55 wealth, the cost of this warp gate would have to be paid by both the cluster made up of 11 and 22 as well as the cluster made up of 33 and 44, if it were to be built. Since both clusters have at least 55 wealth, the warp gate is built and now colonies 1,2,31, 2, 3 and 44 form a cluster with a total wealth of (5−5)+(9−5)=4(5-5) + (9-5) = 4. Afterward, if it were proposed to build a warp gate between colonies 11 and 44, it would be reported unnecessary because colonies 11 and 44 already belong to the same cluster. Finally, if it were proposed to build a warp gate between colonies 11 and 55 at a cost of 55 wealth, regardless of 55's cluster's wealth, it would be impossible because 11's cluster only has 44 wealth.

입력

The first line contains two integers 2≤n≤1062 \leq n \leq 10^6 and 1≤m≤1061 \leq m \leq 10^6, the number of colonies and the number of warp gate proposals, respectively.

The second line contains nn space separated integers where the ii-th integer is the wealth of the ii-th colony, 0≤w_i≤1090 \leq w\_i \leq 10^9.

Then follows mm lines, the jj-th of which contains three integers 1≤a_j,b_j≤n1 \leq a\_j,b\_j \leq n, and 0≤c_j≤1090 \leq c\_j \leq 10^9 where a_ja\_j and b_jb\_j denote the colony numbers to connect with the jj-th proposed warp gate (where a_j≠b_ja\_j \neq b\_j) and c_jc\_j being the cost.

출력

For each of the mm proposed warp gates, output a line containing:

  • BUILT if the warp gate can and should be built.
  • UNNECESSARY if the colonies that would be connected by the warp gate already belong to the same cluster.
  • IMPOSSIBLE if it cannot be built because one or both of the clusters lack the necessary wealth to built the warp gate.

Note: If it is both unnecessary and impossible to build a warp gate, you should only report UNNECESSARY.

예제1

  1. 예제 1

    입력
    5 5
    2 3 4 5 7
    1 2 0
    3 4 0
    2 3 5
    1 4 1
    1 5 5
    
    예상 출력
    BUILT
    BUILT
    BUILT
    UNNECESSARY
    IMPOSSIBLE