「BZOJ 1023」「SHOI2008」cactus仙人掌图

BZOJ 1023

题意

你有一棵\(n\)个点的仙人掌,边权为\(1\)

定义两点的距离是最短路的长度

定义仙人掌的直径是距离最远的两点的距离

求直径

\(n\le5*10^4\)


分析

建圆方树,在LCA处处理最长的路径

\(f_i\)表示\(i\)子树中的点到\(i\)的最大距离

在圆点处直接按照树的方式处理,找出儿子中\(f\)的最大和次大值

为了不改变圆点的处理方式,对于方点\(u\)\(f_u\)定义为子树中到父亲节点的最大距离\(-1\),这直接考虑环上的两种路径即可

主要是LCA是方点的情况,把环复制两遍,用单调队列维护一下\(\le\frac{1}{2}\)环长的最优转移。

复杂度\(\mathcal O(n)\)


代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<cstdio>
#include<algorithm>
#include<ctype.h>
#include<string.h>
#include<math.h>

using namespace std;
#define ll long long

inline char read() {
static const int IN_LEN = 1000000;
static char buf[IN_LEN], *s, *t;
return (s==t?t=(s=buf)+fread(buf,1,IN_LEN,stdin),(s==t?-1:*s++):*s++);
}
template<class T>
inline void read(T &x) {
static bool iosig;
static char c;
for (iosig=false, c=read(); !isdigit(c); c=read()) {
if (c == '-') iosig=true;
if (c == -1) return;
}
for (x=0; isdigit(c); c=read()) x=((x+(x<<2))<<1)+(c^'0');
if (iosig) x=-x;
}
const int OUT_LEN = 10000000;
char obuf[OUT_LEN], *ooh=obuf;
inline void print(char c) {
if (ooh==obuf+OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), ooh=obuf;
*ooh++=c;
}
template<class T>
inline void print(T x) {
static int buf[30], cnt;
if (x==0) print('0');
else {
if (x<0) print('-'), x=-x;
for (cnt=0; x; x/=10) buf[++cnt]=x%10+48;
while(cnt) print((char)buf[cnt--]);
}
}
inline void flush() { fwrite(obuf, 1, ooh - obuf, stdout); }

const int N = 50005, M = 100005;
int p, top, cnt, n, m, ans, num=1, h[N], dfn[N], low[N], stk[N], f[N<<1], q[N<<1], g[N<<1], e[M<<1], pre[M<<1];
vector<int> E[N<<1];
inline void add(int x, int y){ e[++num]=y, pre[num]=h[x], h[x]=num;}
void tarjan(int u, int fa=0){
dfn[u]=low[u]=++cnt;
stk[++top]=u;
for(int i=h[u]; i; i=pre[i]) if(i!=fa)
if(!dfn[e[i]]){
tarjan(e[i], i^1), low[u]=min(low[u], low[e[i]]);
if(low[e[i]]==dfn[e[i]]) E[u].push_back(e[i]);
}
else if(dfn[e[i]]<dfn[u]){
low[u]=min(low[u], dfn[e[i]]);
E[e[i]].push_back(++p);
for(int j=top; stk[j]!=e[i]; --j) E[p].push_back(stk[j]);
}
--top;
}
void dfs(int u){
for(int v:E[u]) dfs(v);
if(u<=n){
int x=0;
for(int v:E[u])
if(f[v]+1>f[u]) x=f[u], f[u]=f[v]+1;
else if(f[v]+1>x) x=f[v]+1;
ans=max(ans, f[u]+x);
}
else{
int siz=E[u].size()+1, x=0, h=1, t=0;
for(int v:E[u]){
++x, f[u]=max(f[u], f[v]+min(x, siz-x));
g[x]=f[v];
while(h<=t && x-q[h]>siz/2) ++h;
if(h<=t) ans=max(ans, x-q[h]+g[x]+g[q[h]]);
while(h<=t && g[x]-x>=g[q[t]]-q[t]) --t;
q[++t]=x;
}
++x;
for(int i=1; i<siz; ++i){
++x;
g[x]=g[i];
while(h<=t && x-q[h]>siz/2) ++h;
if(h<=t) ans=max(ans, x-q[h]+g[x]+g[q[h]]);
while(h<=t && g[x]-x>=g[q[t]]-q[t]) --t;
q[++t]=x;
}
--f[u];
}
}
int main() {
read(n), read(m), p=n;
while(m--){
static int k, x, y;
read(k), read(x);
for(int i=1; i<k; ++i) read(y), add(x, y), add(y, x), x=y;
}
tarjan(1), dfs(1);
return printf("%d\n", ans), 0;
}

「BZOJ 1023」「SHOI2008」cactus仙人掌图

https://cekavis.github.io/bzoj-1023/

Author

Cekavis

Posted on

2018-11-01

Updated on

2022-06-16

Licensed under

Comments