1023: [Shoi2008]cactus Cactus Chart Time limit:1 Sec Memory limit:162 MB
submit:1440 solved:582
[Submit] [Status] [Discuss] Description
If any of the edges of a non-connected graph appear at most in a simple loop, we call this image the cactus. The so-called Simple loop is a loop that does not repeat through any vertex on the graph.
For example, the first example above is a fairy figure, and the second one is not--notice that it has three simple loops: (4,3,2,1,6,5,4), (7,8,9,10,2,3,7) and (4,3,7,8,9,10,2,1,6,5,4), and (2, 3) appears in the first two simple circuits at the same time. In addition, the third picture is not a fairy figure because it is not a connected graph. Obviously, each side of the cactus, or the bridge of this fairy figure, or in a simple circuit, both must be one. Defines the distance between two points on the graph as the shortest path between the two points. Defines the distance between the diameter of a graph and the two points farthest from the graph. Now we assume that the weight of each side of the Cactus is 1, and your task is to find out the diameter of the given fairy figure.
Input
The first line of input includes two integers n and m (1≤n≤50000 and 0≤m≤10000). where n represents the number of vertices, we agree that the vertices in the graph will be numbered from 1 to N. Then there's a total of M lines. Represents the M-bar path. Each line starts with an integer k (2≤k≤1000) that represents the number of vertices on this path. Next is the K 1 to n integer that corresponds to a vertex, and the adjacent vertex represents an edge that joins the two vertices. A path may pass through a vertex several times, for example, for the first example, the first path from 3 through 8, and return from 8 to 3, but we guarantee that all the edges will appear on a path, and will not be repeated on the two path, or a path appears two times.
Output
Just output a number, this number represents the diameter length of the immortal figure.
Sample Input15 3
9 1 2 3 4 5 6 7 8 3
7 2 9 10 11 12 13 10
5 2 14 9 15 108
10 1
1 2 3 4 5 6 7 8 9Sample Output9HINT
A description of the first example: the shortest path length of 6th and 12th points is 8, so the diameter of this graph is 8.
Note: If you are using the Pascal language, please note that your program may overflow when dealing with big data. If you need to adjust the size of the stack space, you can add a sentence at the beginning of the program: {$M 5000000}, where 5000000 refers to the size of the stack space, please choose the appropriate value according to your own program.
Source
A: Mom, a chicken ... I'm going to be a dynamic cactus. The result of this silly fork DP will not = = I have not saved the heavens ... Let's leave a hole.
The puzzle is here: http://z55250825.blog.163.com/blog/static/150230809201412793151890/
First of all our idea is to DFS out a tree, and then in the DFS process to find out the answer.
Now just consider the DFS out of this tree, the original image is not the edge of the tree we call the non-tree edge. The depth of each node is then defined as the depth in the Dfs tree.
We set f[i] to indicate the point of the subtree in the Dfs tree that is the root of I and I in the inducer of the Cactus graph (The induced sub-graph is a point set of a graph V and all two The end is the length of the longest line in the shortest path of the other points in the induced sub-graph, starting with I as the graph of the edge set E formed by the edges in the set v.
This definition seems YDC God Ben not too clear (it must be too simple!) )... So I konjac Konjac at the beginning is also a variety of misunderstandings and yy ..... That's what it feels like.
First of all, assuming a non-tree edge (U,V), it is obviously to u,v on its Dfs tree path into a ring, due to the nature of the fairy graph, the path of the edge must not appear in another ring, so we can split it out, So the whole picture is actually one ring after another and the connecting edge between them (this is the bridge) or directly connected.
The above is some of the nature of YY, now let's consider the situation without the ring ....
So our answer, that is, what is the furthest-point path possible?
Let's start with a conclusion:
1. The path must have one and only one node, which is the least deep in all points of the path (we call it the highest point).
This is a good understanding, assuming that the path has two nodes u,v, they are the same depth and minimum,
So how are the two connected?? First, it cannot be because of love, they even to their father (
their fathers are smaller than they are, but what we assume is that their depth is the smallest of all points in the path, so that their father is not in the path, the contradiction), it is not possible to connect them between them (
assuming that they have an edge between them, then we DFS one point u, according to the DFS process, it is inevitable that DFS out of the other point, so that its depth is the depth of U +1, which is the same contradiction with their depth), even more unlikely to be the edges of their descendants (
assuming that their descendants are connected, and that we are DFS u first, then we can get from the descendant Dfs of U to the descendants of V and then DFS to V, that is, you can search for V in the descendants of U, so u,v cannot be the same depth, contradiction)
So we know the nature, that is, each path corresponds to the highest point of the path, the node in this path is the smallest depth. Well, for each node W, we can get a collection of such paths, both of which should be node W.
So let's just ask for the longest one in the set of paths for each node, and then take the maximum of each node's answer.
Then the f[we set up at the outset] is still useful.
First f[] transfer.
f [i] =max (f [K]) +1 (son of K∈i)
and the longest path in this set is the second largest +2 of the son's F-max + son.
This transfer has a technique, we press U's son in turn Dfs down, recursively find the sons of F, and then a son for each DFS, we use F[U]+F[V]+W[U][V] update the answer, and then use F[V]+W[U][V] Update f[u], here the title does not require w[u][v]= 1.
But the above discussion is only without the ring of the situation, not the tree side we did not consider to go in.
And by 1, we know that each non-tree edge can only be connected to its descendants by one ancestor (which should obviously be disproved). We consider a ring formed by the non-tree edge of an ancestor U and its descendant v.
Let's assume that there is only one ring ... And that is the root node to a node u, (for example, the red side is not a tree edge, it constitutes a circle of the two-way), then what should we do?
The first thing you can see is that this diagram actually looks like this ...
In other words, three points at this time in fact, the status of equality .... That is, for each point on the ring, they are in fact equal in status. We select the highest point on the ring as the representative element, put some information of this ring on the representative element, and then pass it on. This message is the highest point of f. The general idea of the algorithm is roughly like this ...
Let's see what we're going to do after the new ring is added.
First, when we search the child V of the node U, if you determine that you and V are on the same ring, and you are not the highest point, then we do not have to f[u]+f[v]+1 to update the answer, skip the search for other sons, that is, after we have searched all the sons, this time our node U F, What actually exists is the longest of all the shortest paths that are not connected to the son of you on the same ring, and you are not considered on the same ring, and the updated answer is only updated with all the longest and minor short circuits that are formed by all the sons that are not on the same ring, and there is no processing ring. So after we finish working with my son's Dfs, we'll start processing the ring. Now look at how to handle the ring.
First found that the ring when we have two things is not very reliable, one is F, obviously the shortest short of the longest, here from the ancestor U new out of a side connected to the descendants, may force the original U the longest short path chain shorter, maybe this chain is the original short chain to replace, So we have to update f when we deal with the ring, and because the ancestors of the highest point of the ring are waiting for its information to update in the DFS process, the information of the descendants of this ring is irrelevant to their change, so we just have to make sure that the F of the node on the ring is correct. According to the definition of F, the only thing that will affect the edge of this ring is the f at the highest point of the ring (because the induced sub-map of the other points does not change, so the F is not changed), so we have to update the F at the highest point.
For nodes that are not at the highest point on each ring, we save their F, and virtually every point holds the longest chain in the shortest path from which it extends. We can use this to update the highest point F.
So how to update?
Actually, the model is
One
Ring, in addition to the highest point u, the point on each ring has a chain (can not), and then to select a point v≠u, and Dist (U,V) (the minimum distance between U and V) + chain length F (v) (is a path) the largest, and then use this maximum value to update the F of U, And then this is the direct ring DP on it ...
Why is this correct? First, each decision is a path, it is guaranteed that except the vertex of the other node V is the longest path of V to its son's shortest path, then we just guarantee that u to V is also the shortest path, then you can ensure that the path used to transfer is also a shortest path, Then we can make sure that this f is legal. Then take the maximum value of which is obviously the new value of F[u].
Above we maintain the f[], then we have to update the answer. Just said that for the Dfs tree inside we enumerated the highest point and then DP on it. But the answer relates to the path that did not go to the ring.
So before processing the ring for each point on the ring, our updated answer is only the sum of the longest and the most long paths for each of the other than the rings, and we have to update the answer with the path on the loop, so we can keep the longest route for each point. Now start thinking about the ring. For the ring, in fact each point is equal, so our task is to enumerate the nodes on the ring I, for the node I, when we update the answer only used the non-tree edge of the son, and because each point on the ring is actually equal, so we use the chain of each point on the ring to update it, and then we use its The longest chain of non-loop f[i] plus the longest link to a point J on the ring, plus J's longest chain f[j] to update the answer. What we're actually asking for is Max (Dist (I,J) +f (j)) (F (i) is fixed), so this can also be done with DP ..., as for the way to do it, Dist (I,J) must be monocytogenes, so let's use the monotone queue to maintain F (j).
So is a root node attached to multiple rings as the highest point? Obviously, these rings are also independent of each other, the ring processing can be.
So there's more than one ring, not just the root node? Notice we are DFS's ... So we must first deal with the son to deal with the father, so we can certainly be handed to only one ring of the son processing, after processing, the son of the following can be regarded as a chain of ... (Ring what does not make sense, only its F makes sense), and then recursion back is another root node of a lump ring problem, the result can be.
In short, the problem of the various streams of consciousness, here konjac konjac mouth hugad various noun confusion ... If there is a problem asking for criticism ... To ask for criticism is a light ... Finally, we will summarize the algorithm flow:
1.Tarjan, record the parent of the Dfs tree.
2.Tarjan Process, a 3 method to determine whether a son and this point constitute a ring, if not then use the son to update ans.
3.Tarjan returns all adjacent points under scan, if the father of a descendant is not himself, indicating the ring, the starting point of this ring
(That is, the highest point of the ring is this point), the end is the descendant, and then from this descendant along the Dfs tree father went up all the points on the ring, we take all the points out of the DP this ring.
4. For how DP, first we update the answer, and then update the highest point F to pass to the Dfs tree above the point. The update answer is actually MAX (F[i]+f[j]+dist (i,j)) where Dist (I,J) is the shortest distance between point I and Point J on the ring, then we can fix I, for each i max (F[j]+dist (i,j)), Dist (i,j) equals Min (j-i,n-i+j), in order to lose this min good use the monotone queue, we will copy the ring again, and then each time only consider the half-loop length of the DP, which is obviously correct, each state can be calculated at the time of I or J. Then the equation can continue to be rewritten as Max (f[i]+f[j]+j-i), after fixing I is seeking Max (F[J]+J) (J∈[I+1,I+N/2]), this monotone queue is no pressure, so ans is updated.
Then there is the f[highest point] update. This direct enumeration ring on the point once, the number of rings in total only O (M) of the complexity of no pressure.
Bzoj 1023 [shoi2008]cactus cactus Diagram