Travel (data enhanced version)

topic

Travel (data enhanced version)

https://www.luogu.org/problem/P5049

The meaning of problems

Meaning of the questions is very simple, that is running while the node where the size of the current record on a graph, you can not go through the points already, but can be traced back, the final output lexicographically smallest number sequence.

analysis

  • m = n - 1

Needless to say this situation, ran directly over the DFS D F S on the line!

  • m = n

It should be very clear that a ring will make this happen figure.

That how to solve it? Did you think went on this half of the ring, the other half back to the starting point you just got through this ring, followed by the DFS D F S on the line.

Sample

For Sample 2 of Example 2:

Obviously 2--3--4--5 is the ring of FIG.

Beginning from a starting node, then 3 is our first time to the beginning of this ring.

Then we have to 2 go, then back to the starting point after 3 , 3 again 4 to go, and finally finish the entire map on the line, because then we would not need special treatment the ring.

This process should be well understood, we now know that m = n m = - n where only the processing point on the ring, or like other points m = n - . 1 as D F. S enough, and if on the ring after the back (above 2 back to 3), the rest of the process does not require special handling.

So the key now is to solve the special treatment on the ring.

We put points on the ring is divided into three cases:

A, which is the side that is the point number on a ring which is not accessible to all sides of the smallest, as shown below.

From . 1 . 1 starting after 2 reaches 3, then from 3 out of three sides of departure has access 4 , 6 , 7, where 4 is the point on the ring, but also the smallest.

figure 1


Second, the side which is on the point number is a ring which is not accessible to all sides is not the maximum nor minimum, as shown below.

At this time, 6 points on the ring, but not the maximum nor the minimum edge.

figure 2


Third, the side of which that point is the number on the sides of the largest ring, as it is not accessible to all of FIGS.

In this case 7 is the largest on the side of the ring is the point.

image 3


For the first case (see below):

由于我们现在在3这个节点,如果我们回溯的话,6和7就永远也到不了了,所以在回溯之前要先把6和7走完,相比6和7,走4显然会更优。

所以第一种情况不需要回溯,继续在环上走就行了。

figure 1


对于第二种情况(见下图):

同样还是在3节点,这次显然我们要先走4,但是走完4还是得走6节点,同样也不需要回溯。

figure 2


对于第三种情况(见下图):

这时候很明显是要回溯的了。

需要注意的是回溯之前要先把44和66先走了再回溯(原因上面刚讲过了)。

但是,如果我们换张图,还需要回溯吗?(见下下图)

image 3


这张图中,我们假设当前还是在3节点,6是其出边中最大且在环上的点。

这时候需不需要回溯呢?很显然不需要

因为回溯之后回到了2,2继续走的话是走到了7,显然走6比起7更优。

Figure 4


总结一下,我们在环上走的时候,只有当其出边中,为环上的那个点编号最大,且比回溯后第一个走的点还大,这时候才回溯,其他时候就正常跑DFS。

Therefore the code on the I flag whether the tag needs to back, with a T m of nodes that the first node bigger than an edge on the ring the current node p records in order to determine whether comparison later backtracking.


 

Guess you like

Origin www.cnblogs.com/aprincess/p/11761759.html