SHOI 2007 仙人掌图(BZOJ 1023)

1023: [SHOI2008]cactus仙人掌图

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 2564  Solved: 1062

Description

  如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人掌
图(cactus)。所谓简单回路就是指在图上不重复经过任何一个顶点的回路。

 

  举例来说,上面的第一个例子是一张仙人图,而第二个不是——注意到它有三条简单回路:(4,3,2,1,6
,5,4)、(7,8,9,10,2,3,7)以及(4,3,7,8,9,10,2,1,6,5,4),而(2,3)同时出现在前两
个的简单回路里。另外,第三张图也不是仙人图,因为它并不是连通图。显然,仙人图上的每条边,或者是这张仙
人图的桥(bridge),或者在且仅在一个简单回路里,两者必居其一。定义在图上两点之间的距离为这两点之间最
短路径的距离。定义一个图的直径为这张图相距最远的两个点的距离。现在我们假定仙人图的每条边的权值都是1
,你的任务是求出给定的仙人图的直径。

 

Input

  输入的第一行包括两个整数n和m(1≤n≤50000以及0≤m≤10000)。其中n代表顶点个数,我们约定图中的顶
点将从1到n编号。接下来一共有m行。代表m条路径。每行的开始有一个整数k(2≤k≤1000),代表在这条路径上
的顶点个数。接下来是k个1到n之间的整数,分别对应了一个顶点,相邻的顶点表示存在一条连接这两个顶点的边
。一条路径上可能通过一个顶点好几次,比如对于第一个样例,第一条路径从3经过8,又从8返回到了3,但是我们
保证所有的边都会出现在某条路径上,而且不会重复出现在两条路径上,或者在一条路径上出现两次。

Output

  只需输出一个数,这个数表示仙人图的直径长度。

Sample Input

15 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
10 1 2 3 4 5 6 7 8 9 10

Sample Output

8
9

HINT

 

对第一个样例的说明:如图,6号点和12号点的最短路径长度为8,所以这张图的直径为8。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 #define N 2222222
 7 int head[N],to[N],next[N];
 8 int n,m,tot,cnt,ans;
 9 int dis[N],cir[N];
10 int dfn[N],low[N],visx,fa[N];
11 inline void Add_Edge(int u,int v){to[cnt]=v;next[cnt]=head[u];head[u]=cnt++;}
12 struct Data{
13     int p,w;
14 }q[N];
15 inline void read(){
16     memset(head,-1,sizeof head );
17     scanf("%d%d",&n,&m);
18     for(int i=1,a,b,c;i<=m;i++){
19         scanf("%d%d",&a,&b);
20         for(int j=2;j<=a;j++){
21             scanf("%d",&c);
22             Add_Edge(b,c);Add_Edge(c,b);b=c;
23         }
24     }
25 }
26 inline void GetCircle(){
27     int h=1,t=1;
28     for(int i=1;i<=tot;i++) cir[tot+i]=cir[i];//展链成环
29     for(int i=1;i<=(tot<<1);i++){
30         while(h<t&&i-q[h].p>(tot>>1)) h++;
31         while(h<t&&q[t].w<=dis[cir[i]]-i) t--;
32         q[++t].p=i; q[t].w=dis[cir[i]]-i;
33         ans=max(ans,dis[cir[i]]+i+q[h].w);
34     }
35 }
36 inline void DFS(int u){
37     low[u]=dfn[u];
38     for(int i=head[u];~i;i=next[i]){
39         int v=to[i];
40         if(fa[v]!=0&&v!=fa[u]) low[u]=min(low[u],dfn[v]);
41         if(fa[v]==0){
42             fa[v]=u; dfn[v]=dfn[u]+1; DFS(v);
43             low[u]=min(low[u],low[v]);
44         }
45     }
46     for(int i=head[u];~i;i=next[i]){
47         int v=to[i];
48         if(fa[v]==u&&low[v]>dfn[u]){//Bridge
49             ans=max(ans,dis[v]+1+dis[u]);
50             dis[u]=max(dis[u],dis[v]+1);
51         }
52         if(fa[v]!=u&&dfn[u]<dfn[v]){//Circle
53             tot=0;
54             while(v!=fa[u]) cir[++tot]=v,v=fa[v];//cir暂时存储环上的点
55             GetCircle();//接着处理环上的点
56             for(int j=1;j<tot;j++)
57                 dis[u]=max(dis[u],dis[cir[j]]+min(j,tot-j));
58         }
59     }
60 }
61 inline void Solve(){
62     fa[1]=-1;
63     DFS(1);
64     cout<<ans<<endl;
65 }
66 int main(){
67     read();
68     Solve();
69     return 0;
70 }

 

posted @ 2017-05-05 08:31  浮华的终成空  阅读(257)  评论(0编辑  收藏  举报

Contact with me