bzoj1023: [SHOI2008]cactus仙人掌图

1023: [SHOI2008]cactus仙人掌图

链接

bzoj

思路

仙人掌求两点间最短路最大。姑且叫他仙人掌的直径。
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;
}
全部评论

相关推荐

一名愚蠢的人类:多少games小鬼留下了羡慕的泪水
投递荣耀等公司10个岗位
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务