8.29 meaning of life

The meaning of problems

To a \ (n-\) points \ (m \) free edges of the graph, an operation for the undirected graph, i.e., removal of a figure generating forest (i.e. spanning all connected blocks set ). Continue operating until the edges in the graph are deleted the undirected graph. For each side, the output he was removed in the first few times.

If there are multiple solutions to the output of one


solution

First, we have a good think \ (O (MN) \) violence

Traversing each edge, each spanning tree operation: Specifically, it is a disjoint-set each time to open, if the two end points of the side of the block is not the same communication, are put Unicom, and from side to side of the deletes; otherwise skip the edge

We consider how to optimize the algorithm for violence

If we open several disjoint-set, one for each side, if the \ (1 \) number and check the focal point of its ends connected in a communication block, we just \ (2 \) number and the investigation focused insert, and so on ...

By the above method, we can find the spanning forest formed after each operation

But we will find that this algorithm and compared to the original, not only does not change the time complexity, space complexity but has become a more inferior

But the idea of ​​the algorithm there are some enlightening

We can find a property: For one side, if it can be at the \ (I \) resolution and concentrating insert check, then it is also possible in the (i + 1 ... n \) \ number and check concentrating insert

This can be proved by contradiction, it can also be tacit understanding

Found this nature in the future, we can separate the first two can be inserted into a number of disjoint-set

Thus by the time complexity \ (O (MN) \) optimized to \ (O (MlogM) \)

But still can not afford the space complexity

At this point we can optimize the space with a hash table

We disjoint-set number of rows together, can be seen as a plurality of lines \ (n-\) columns of the matrix

The coordinates of each matrix \ ((x, y) \ ) mapped to a certain number of

In the course of the operation up to us insert \ (2M \) different points

So disjoint-set only need to open \ (O (2M) \) space to

There is also a Sao operation: pointer to open the dynamic point? ? ! But not very good pointer on the first muttered it


Code

Guess you like

Origin www.cnblogs.com/VeniVidiVici/p/11432037.html