#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
#define SF scanf
#define PF printf
using namespace std;
typedef long long LL;
const int MAXN = 50000;
vector <int> G[MAXN+10];
int low[MAXN+10], dfn[MAXN+10], fa[MAXN+10];
int A[MAXN*2+10], Q[MAXN*2+10], F, R;
int d[MAXN+10], L[MAXN+10];
int n, m, ans, dcnt;
void add(int u, int v) { G[u].push_back(v); }
void dp(int rt, int p) {
int T = L[p] - L[rt] + 1;
for(int i = p; i != rt; i = fa[i]) A[T--] = d[i];
A[T] = d[rt];
T = L[p] - L[rt] + 1;
for(int i = 1; i <= T; i++) A[i+T] = A[i];
F = 1, R = 2, Q[F] = 1;
for(int i = 2; i <= T * 2; i++) {
while(F < R && i - Q[F] > T / 2) F++;
ans = max(ans, A[i] + A[Q[F]] + i - Q[F]);
while(F < R && A[Q[R-1]] - Q[R-1] <= A[i] - i) R--;
Q[R++] = i;
}
for(int i = 2; i <= T; i++) d[rt] = max(d[rt], A[i] + min(i - 1, T - i + 1));
}
void dfs(int u) {
low[u] = dfn[u] = ++dcnt;
for(int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if(v == fa[u]) continue ;
if(!dfn[v]) {
fa[v] = u; L[v] = L[u]+1;
dfs(v);
low[u] = min(low[u], low[v]);
}
else low[u] = min(low[u], dfn[v]);
if(dfn[u] < low[v]) {
ans = max(ans, d[u] + d[v] + 1);
d[u] = max(d[u], d[v] + 1);
}
}
for(int v, i = 0; i < G[u].size(); i++)
if(fa[v=G[u][i]] != u && dfn[u] < dfn[v])
dp(u, v);
}
int main()
{
int u, v;
SF("%d%d", &n, &m);
for(int i = 1; i <= m; i++) {
int k; SF("%d%d", &k, &u);
for(int j = 2; j <= k; j++) {
SF("%d", &v);
add(u, v); add(v, u);
u = v;
}
}
dfs(1);
PF("%d", ans);
}
[BZOJ1023] [SHOI2008]cactus仙人掌图 && 仙人掌DP+单调队列
最新推荐文章于 2021-08-12 18:09:57 发布