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,你的任务是求出给定的仙人图的直径。


发现一个写的特别好的题解:http://z55250825.blog.163.com/blog/static/150230809201412793151890/

DFS出一棵树,然后在DFS的过程中求出答案。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#define inf 1000000000
#define ll long long 
using namespace std;
inline int read()
{
	int x = 0, f = 1; char ch = getchar();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
int n,m,cnt,ind,ans;
int last[50005], deep[50005], f[50005], low[50005], dfn[50005], fa[50005], a[100005], q[100005], l, r;
struct edge
{
	int to, next;
}e[20000005];
void insert(int u,int v)
{
	e[++ cnt].to = v;
	e[cnt].next = last[u];
	last[u] = cnt;
	e[++ cnt].to = u;
	e[cnt].next = last[v];
	last[v] = cnt;
}
void dp(int root,int x)
{
	int tot = deep[x] - deep[root] + 1;
	for(int i = x; i != root; i = fa[i])
		a[tot --] = f[i];
	a[tot] = f[root];
	tot = deep[x] - deep[root] + 1;
	for(int i = 1; i <= tot; i ++)
		a[i + tot] = a[i];
	q[1] = 1;
	l = r = 1;
	for(int i = 2; i <= 2 * tot; i ++)
	{
		while(l <= r && i - q[l] > tot / 2) l ++;
		ans = max(ans, a[i] + i + a[q[l]] - q[l]);
		while(l <= r && a[q[r]] - q[r] <= a[i] - i) r --;
		q[++ r] = i;
	}
	for(int i = 2; i <= tot; i ++)
		f[root] = max(f[root], a[i] + min(i - 1, tot - i + 1));
}
void dfs(int x)
{
	low[x] = dfn[x] = ++ ind;
	for(int i = last[x]; i; i = e[i].next)
		if(e[i].to != fa[x])
		{
			if(!dfn[e[i].to])
			{
				fa[e[i].to] = x;
				deep[e[i].to] = deep[x] + 1;
				dfs(e[i].to);
				low[x] = min(low[x], low[e[i].to]);
			}
			else low[x] = min(low[x], dfn[e[i].to]);
			if(dfn[x] < low[e[i].to])
			{
				ans = max(ans, f[x] + f[e[i].to] + 1);
				f[x] = max(f[x], f[e[i].to] + 1);
			}
		}
	for(int i = last[x]; i; i = e[i].next)
		if(fa[e[i].to] != x && dfn[x] < dfn[e[i].to])
			dp(x,e[i].to);
}
int main()
{
	n = read();
	m = read();
	for(int i = 1; i <= m; i ++)
	{
		int k = read(), a = read();
		for(int i = 2; i <= k; i ++)
		{
			int b = read();
			insert(a,b);
			a = b;
		}
	}
	dfs(1);
	printf("%d\n",ans);
	return 0;
}


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。


 


【注意】使用Pascal语言的选手请注意:你的程序在处理大数据的时候可能会出现栈溢出。如果需要调整栈空间的大小,可以在程序的开头填加一句:{$M 5000000},其中5000000即指代栈空间的大小,请根据自己的程序选择适当的数值。