bzoj1023: [SHOI2008]cactus仙人掌图
Posted dsrdsr
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bzoj1023: [SHOI2008]cactus仙人掌图相关的知识,希望对你有一定的参考价值。
1023: [SHOI2008]cactus仙人掌图
链接
思路
仙人掌求两点间最短路最大。姑且叫他仙人掌的直径。
f[u]表示u为端点的最长的最短路
当他是圆圆边,和树上一样dp转移。
圆方边,取出环了进行别的dp。
转化成带权的环上更新两端距离的最短路的最大值,单调队列。
最后别忘了更新f[u]
也许表达的不大清楚
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int read()
int x = 0, f = 1; char s = getchar();
for (; s > '9' || s < '0'; s = getchar()) if (s == '-') f = -1;
for (; s >= '0' && s <= '9'; s = getchar()) x = x * 10 + s - '0';
return x * f;
int n, m, ans;
struct node
int v, nxt;
e[N << 1];
int head[N << 1], tot;
void add(int u, int v)
e[++tot].v = v ;
e[tot].nxt = head[u];
head[u] = tot;
int low[N], dfn[N], cnt, fa[N], f[N];
int a[N], q[N], h, d, Siz, dep[N];
void work(int u, int y)
Siz = 0;
for (int i = y; i != fa[u]; i = fa[i]) a[++Siz] = i;
for (int i = y; i != fa[u]; i = fa[i]) a[++Siz] = i;
h = 1, d = 0;
for (int i = 1; i <= Siz; ++i)
while(h <= d && i - q[h] > (Siz/2) / 2) h++;
int x = a[i], y = a[q[h]];
if(h<=d) ans = max(ans, f[x] + i - q[h] + f[y]);
while(h <= d && f[a[q[d]]] + i - q[d] <= f[a[i]]) d--;
q[++d] = i;
for (int i = 1; i < Siz / 2; ++i)
f[u] = max(f[u], f[a[i]] + min(Siz / 2 - i, i));
void tarjan(int u, int father)
fa[u] = father;
low[u] = dfn[u] = ++cnt;
dep[u] = dep[father] + 1;
for (int i = head[u]; i; i = e[i].nxt)
int v = e[i].v;
if (!dfn[v])
tarjan(v, u);
low[u] = min(low[u], low[v]);
else if (v != father) low[u] = min(low[u], dfn[v]);
if (dfn[u] < low[v])
ans = max(ans, f[u] + f[v] + 1);
f[u] = max(f[u], f[v] + 1);
for (int i = head[u]; i; i = e[i].nxt)
int v = e[i].v;
if(u != fa[v] && dfn[v] > dfn[u])
work(u, v);
int main()
n = read(), m = read();
tot = cnt = ans = 0;
for (int i = 0; i < m; ++i)
int k = read(), las = read(), x;
for (int j = 1; j < k; ++j)
x = read();
add(las, x), add(x, las);
las = x;
tarjan(1, 0);
printf("%d\n", ans);
return 0;
以上是关于bzoj1023: [SHOI2008]cactus仙人掌图的主要内容,如果未能解决你的问题,请参考以下文章
bzoj1023: [SHOI2008]cactus仙人掌图
bzoj千题计划224:bzoj1023: [SHOI2008]cactus仙人掌图