码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj1023: [SHOI2008]cactus仙人掌图

时间:2019-05-31 21:16:20      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:cpp   思路   转移   bit   names   php   tchar   const   表达   

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;
}

bzoj1023: [SHOI2008]cactus仙人掌图

标签:cpp   思路   转移   bit   names   php   tchar   const   表达   

原文地址:https://www.cnblogs.com/dsrdsr/p/10957466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!