Topology No. vijos1790

Source: Internet
Author: User

The meaning is the topological sorting. The sequence number of 1 is as small as possible, and the sequence number of 2 is as small as possible, 3, 4...

 

In the beginning, it is easy to think of direct greed. Each time you choose a point with an inbound degree of 0, if there are multiple, you can select the one with the smallest number, but it is easy to find the inverse example.

After reading the answer, we should reverse the topological sorting. The largest number is obtained each time, but no one gives a proof. My proof is provided below. You are welcome to point out the error.

Ps: I have referenced a Daniel's article. poj has a similar question. I have referred to his question, but I cannot find it. So I cannot release his link. I apologize first.

 


Assume that we have obtained A topological sequence by sorting the reverse topology (forward the obtained reverse sequence) as.


Assume that the topological sequence of the optimal solution is B.


Compare AB from the back to the front, which is located at the position k. AB appears different for the first time. That is, A [k]! = B [k], A [p] = B [p].


Apparently, according to our greedy policy, "the largest number is obtained each time", with A [k]> B [k].


Then we can search for A [k] in B, that is, find B [p] = A [k].


Then, extract the Section B [p], B [p + 1]... B [k] in B and mark it as sequence C.


Because B [p] = A [k], replace B [p] with A [k], C = A [k], B [p + 1]... B [k].


Obviously, in C, A [k] is not the smallest, because at least B [k] is smaller than it. Suppose the smallest of C is B [q], then we can construct a sequence D.


D = B [p + 1], B [p + 2]... B [q], A [k], B [q + 1]... B [k]. (The essence is to move A [k] to the end of B [q)


Obviously, this sequence is better than sequence C, because B [q] ranks first. If C is changed to D, it will be better, and B is the optimal solution.


So how do we know that sequence D must be legal? Because if A [k] is exactly the precursor of B [p + 1], A [k] cannot be removed.


So we return to sequence A. In sequence A, A [k] is the first different element from the right to the left of sequence AB. In sequence, B [p + 1] must be prior to A [k], so A [k] cannot be the precursor of B [p + 1.


In summary, the answer we get is the optimal solution.

Topology No. vijos1790

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.