题目链接 BZOJ 1023
首先,圆方树是比较好想到的,维护直径,我们最方便的做法就是先让它变成一棵树,这里因为是仙人掌图,所以就用圆方树来构建。
再者,就是维护直径了,比较好想到的是非环上结点,就是简单的树形dp就可以维护了。
如果这个点是环上的一个点那又如何是好?那么,环上的两点所构成的最长直径为dp[u] + dp[v] + dis(u, v),"dp[x]"表示x点到它下面的子树的最远距离,dis(u, v)表示在环上两点u、v的最短距离。
其中,我们又可以知道环上两点的距离小于环大小的一半,所以我们可以根据这条信息来维护。
于是,我们只考虑环位置上前面的点,并且在距离上必须小于环大小的一半,然后我们可以维护一个单调队列,保证单调递减,这样队首就是我们的最大答案可能。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
//#include <unordered_map>
//#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f3f3f3f3f
#define eps 1e-8
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
#define MAX_3(a, b, c) max(a, max(b, c))
#define Rabc(x) x > 0 ? x : -x
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, M;
struct Graph
{
vector< pair<int, int> > vt[maxN];
inline void addEddge(int u, int v, int w) { vt[u].push_back(MP(v, w)); }
inline void _add(int u, int v, int w = 0) { addEddge(u, v, w); addEddge(v, u, w); }
inline void clear() { for(int i=1; i<=N; i++) vt[i].clear(); }
} Old, Now;
int dfn[maxN], low[maxN], tot, Stap[maxN], Stop, dis[maxN], len[maxN], Bcnt;
bool instack[maxN] = {false};
vector<int> B_po[maxN];
inline void work(int u, int v)
{
Bcnt++; B_po[Bcnt].clear();
int p;
len[Bcnt] = dis[Stap[Stop]] - dis[u] + 1;
do
{
p = Stap[Stop--]; instack[p] = false;
int d_1 = dis[p] - dis[u], d_2 = len[Bcnt] - d_1;
Now._add(N + Bcnt, p, min(d_1, d_2));
B_po[Bcnt].push_back(p);
} while(p ^ v);
Now._add(u, N + Bcnt);
}
void Tarjan(int u, int fa)
{
dfn[u] = low[u] = ++tot;
Stap[++Stop] = u;
instack[u] = true;
int len = (int)Old.vt[u].size();
for(int i=0, v; i<len; i++)
{
v = Old.vt[u][i].first;
if(v == fa) continue;
if(!dfn[v])
{
dis[v] = dis[u] + 1;
Tarjan(v, u);
if(low[v] > dfn[u])
{
instack[Stap[Stop--]] = false;
Now._add(u, v, 1);
}
else if(low[v] == dfn[u])
{
work(u, v);
}
low[u] = min(low[u], low[v]);
}
else if(instack[v])
{
low[u] = min(low[u], dfn[v]);
}
}
}
int dp[maxN] = {0}, ans, top, tail;
struct node
{
int id, dis;
node(int a=0, int b=0):id(a), dis(b) {}
} que[maxN];
void dfs(int u, int fa)
{
if(u > N)
{
int len = (int)Now.vt[u].size(), Bid = u - N;
for(int i=0, v, c; i<len; i++)
{
v = Now.vt[u][i].first; c = Now.vt[u][i].second;
if(v == fa) continue;
dfs(v, u);
dp[u] = max(dp[u], dp[v] + c);
}
ans = max(ans, dp[u]);
len = (int)B_po[Bid].size();
int Tsiz = (len + 1) / 2;
top = tail = 0;
for(int i=0; i<len; i++)
{
while(top < tail && i - que[top].id > Tsiz) top++;
if(top < tail) ans = max(ans, i - que[top].id + que[top].dis + dp[B_po[Bid][i]]);
while(top < tail && i - que[tail - 1].id + que[tail - 1].dis <= dp[B_po[Bid][i]]) tail--;
que[tail++] = node(i, dp[B_po[Bid][i]]);
}
for(int i=0; i<len; i++)
{
while(top < tail && len + 1 + i - que[top].id > Tsiz) top++;
if(top < tail) ans = max(ans, len + 1 + i - que[top].id + que[top].dis + dp[B_po[Bid][i]]);
while(top < tail && len + 1 + i - que[tail - 1].id + que[tail - 1].dis <= dp[B_po[Bid][i]]) tail--;
que[tail++] = node(len + 1 + i, dp[B_po[Bid][i]]);
}
}
else
{
int len = (int)Now.vt[u].size();
for(int i=0, v, c; i<len; i++)
{
v = Now.vt[u][i].first; c = Now.vt[u][i].second;
if(v == fa) continue;
dfs(v, u);
ans = max(ans, dp[u] + dp[v] + c);
dp[u] = max(dp[u], dp[v] + c);
}
}
}
inline void init()
{
tot = Stop = Bcnt = ans = 0;
// Old.clear(); Now.clear();
}
int main()
{
scanf("%d%d", &N, &M);
init();
for(int i=1, ki, las, x; i<=M; i++)
{
scanf("%d", &ki);
las = 0;
while(ki --)
{
scanf("%d", &x);
if(las) Old._add(las, x, 1);
las = x;
}
}
Tarjan(1, 0);
dfs(1, 0);
printf("%d\n", ans);
return 0;
}
/*
12 4
4 1 3 2 1
8 3 4 5 6 7 8 9 4
2 6 11
3 9 10 12
ans:6
7 1
10 1 2 3 1 4 5 1 6 7 1
ans:2
*/