"Bzoj 1023" [Shoi2008]cactus Cactus Chart

Source: Internet
Author: User

1023: [Shoi2008]cactus Cactus Chart Time limit: 1 Sec Memory Limit: 162 MB
Submit: 1235 Solved: 482
[Submit] [Status] 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 10 8
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.


This question is still looking at the Lyd code to write.


Cacti can be seen as multiple base ring trees connected together.


The problem is to use Tarjan to find the method of connecting components, the diameter set in Tarjan to do.


For the first traversal of a ring, we call it the highest point.

F[x] indicates the longest chain length of a subtree with x as its root


Because in the cactus one side is not cut edge is the edge of the ring, so there are two kinds of situations:

(1) is cut Edge:

i.e. Dfn[x]<low[y].

So before using f[y] to update f[x], first use f[x]+f[y]+1 (at this time f[y] has not updated x,f[x] must be updated with other sons) update the answer, and then use F[y]+1 update f[x]


(2) Not cutting edges:

That is dfn[x]>=low[y], do not control the first




When all the sons of X are counted, we begin to process the ring:

If x is not the highest point on the ring, we don't care, just the X is the highest point (X is not the highest point of the ring indicates there are no finished points on this ring).


Judge X on the ring and is the highest point of the ring:

Fa[y]!=x,dfn[x]<dfn[y].


Then split the chain, using the queue optimization DP to find the longest chain update answer, do not forget the last point on the ring update f[x], because before said not cut edge there is no update!


#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include < Cstdlib> #define M 200005using namespace Std;int num=0,ans=0,n,m,tot,h[m],f[m],low[m],dfn[m],q[m],a[m],fa[m]; struct Edge{int Y,ne;} E[m];void addedge (int x,int y) {tot++;e[tot].y=y;e[tot].ne=h[x];h[x]=tot;} void dp (int x,int y) {int m=0;while (y!=x) {a[++m]=f[y];y=fa[y];} a[++m]=f[x];for (int i=1;i<m;i++) a[m+i]=a[i];int l,r;int p=m/2;q[l=r=1]=1;for (int i=2;i<=m+p;i++) {while (l< =r&&i-q[l]>p) L++;ans=max (Ans,a[q[l]]+a[i]+i-q[l]); while (L<=r&&a[q[r]]+i-q[r]<=a[i]) R--;q[++r]=i;} for (int i=1;i<m;i++) F[x]=max (F[x],a[i]+min (I,m-i));} void Tarjan (int x) {dfn[x]=low[x]=++num;for (int i=h[x];i;i=e[i].ne) if (FA[X]!=E[I].Y) {if (!dfn[e[i].y]) {int y=e[i].y; Fa[y]=x;tarjan (y); Low[x]=min (Low[x],low[y]); if (Dfn[x]<low[y]) {Ans=max (ans,f[x]+f[y]+1); F[x]=max (f[x],f[y]+1        );}} else Low[x]=min (Low[x],dfn[e[i].y]);} for (int i=h[x];i;i=e[i].ne) if (FA[E[I].Y]!=X&AMP;&AMP;DFN[X]&LT;DFN[E[I].Y]) DP (X,E[I].Y);} int main () {scanf ("%d%d", &n,&m), for (int i=1;i<=m;i++) {int k,x,y;scanf ("%d%d", &k,&x), and for (int j= 1;j<k;j++) {scanf ("%d", &y); Addedge (x, y), Addedge (y,x); x=y;}} Tarjan (1); Cout<<ans<<endl;return 0;}



Sentiment:

1. The Tarjan to find the connected component has a more in-depth understanding of the-point of the two connected components equivalent to seek the ring, through the son's low to determine whether the current point is a cut point.


2.lyd Blog



"Bzoj 1023" [Shoi2008]cactus Cactus Chart

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.