Process alliance and war records between groups, merging by sum for alliances and subtracting troops for wars, then report surviving groups sorted by troop count.
Medium5Union-findImplementationSortingSimulationInterviewNo attempts yetTime limit1sMemory limit128 MBThere are N nations, numbered from 1 to N. Each nation holds a number of troops that represents its strength.
M records are given in order. Each record is either an alliance or a war.
Every nation keeps its promises, so an ally of my ally is also my ally, and when one member of a group starts a war, the whole group fights together. A subordinate group is treated like an ally, so every nation on the losing side becomes a subordinate of the winning group. Nations tied by alliance or subordination count as a single nation.
After all records are processed, find the number of surviving nations and their remaining troops, and print the troop counts in ascending order.
The first line contains the number of nations N and the number of records M. (1≤N≤100,000, 1≤M≤100,000)
Each of the next N lines contains the troops Ai of nation i as a positive integer. (1≤Ai≤10,000)
Each of the next M lines contains a record as three integers O, P, and Q. If O=1, nations P and Q form an alliance, and if O=2, nations P and Q go to war. (1≤P,Q≤N)
No record pairs two nations that already belong to the same group, and no record contains a nation that has already perished.
Print the number of surviving nations on the first line.
If at least one nation survives, print the remaining troops of each surviving nation on the next line in ascending order, separated by spaces. If no nation survives, print only 0 on the first line.