\(\mathcal{Description}\)

  Link.

  给出含 \(n\) 个结点 \(m\) 条边的仙人掌图。\(q\) 次询问,每次询问给出一个点集 \(S\),求 \(S\) 内两两结点最短距离的最大值。

  \(n,\sum|S|\le3\times10^5\)。

\(\mathcal{Solution}\)

  圆方树 + 虚树 = 虚圆方树!

  首先,考虑对于整个仙人掌怎么求答案:建出圆方树,DP 记录子树最深结点深度,在方点处单调队列合并圆儿子的两条链贡献答案即可。

  接下来,只需要把“虚圆方树”给弄出来就好。关键即在于满足方点周围一定裹着它自己管辖的圆点的性质,那么在建虚树边时特殊考虑一下就完成啦。

\(\mathcal{Code}\)

/* Clearink */

#include <cstdio>
#include <vector> #define rep( i, l, r ) for ( int i = l, repEnd##i = r; i <= repEnd##i; ++i )
#define per( i, r, l ) for ( int i = r, repEnd##i = l; i >= repEnd##i; --i ) inline char fgc() {
static char buf[1 << 17], *p = buf, *q = buf;
return p == q && ( q = buf + fread( p = buf, 1, 1 << 17, stdin ), p == q )
? EOF : *p++;
} inline int rint() {
int x = 0; char s = fgc();
for ( ; s < '0' || '9' < s; s = fgc() );
for ( ; '0' <= s && s <= '9'; s = fgc() ) x = x * 10 + ( s ^ '0' );
return x;
} template<typename Tp>
inline void wint( Tp x ) {
if ( x < 0 ) putchar( '-' ), x = -x;
if ( 9 < x ) wint( x / 10 );
putchar( x % 10 ^ '0' );
} typedef long long LL; template<typename Tp>
inline void chkmin( Tp& a, const Tp b ) { b < a && ( a = b ); }
template<typename Tp>
inline void chkmax( Tp& a, const Tp b ) { a < b && ( a = b ); }
inline LL lmin( const LL a, const LL b ) { return a < b ? a : b; } const int MAXN = 3e5, MAXM = MAXN << 1, MAXLG = 20;
const LL LINF = 1ll << 60;
int n, m; template<const int NODE, const int EDGE>
struct Graph {
int ecnt, head[NODE], to[EDGE], nxt[EDGE];
LL len[EDGE];
Graph(): ecnt( 1 ) {} inline void operator () ( const int s, const int t, const LL w ) {
#ifdef RYBY
printf( "%d %d %lld\n", s, t, w );
#endif
to[++ecnt] = t, len[ecnt] = w, nxt[ecnt] = head[s], head[s] = ecnt;
}
};
#define adj( t, u, v ) \
for ( int e = t.head[u], v; v = t.to[e], e; e = t.nxt[e] ) Graph<MAXN + 5, MAXM * 2 + 5> src; int vnode, dfc, dfn[MAXN * 2 + 5], low[MAXN + 5];
LL pre[MAXN * 2 + 5];
Graph<MAXN * 2 + 5, MAXN * 2 + 5> cac; inline void buildCactus( const int u, const int f ) {
static int top = 0, stk[MAXN + 5];
dfn[u] = low[u] = ++dfc, stk[++top] = u;
adj( src, u, v ) if ( v != f ) {
if ( !dfn[v] ) {
buildCactus( v, u );
chkmin( low[u], low[v] ); if ( low[v] >= dfn[u] ) {
cac( u, ++vnode, 0 ), pre[vnode] = src.len[e];
int las = u, ttop = top, cnt = 0;
do {
int w = stk[top];
for ( int i = src.head[w]; i; i = src.nxt[i] ) {
if ( i ^ e ^ 1 && src.to[i] == las ) {
++cnt;
pre[w] = pre[las] + src.len[i];
pre[vnode] += src.len[i];
break;
}
}
las = w;
} while ( stk[top--] != v ); do {
int w = stk[ttop];
cac( vnode, w, cnt ?
lmin( pre[w], pre[vnode] - pre[w] ) : pre[vnode] );
} while ( stk[ttop--] != v );
}
} else chkmin( low[u], dfn[v] );
}
} // `dfc` and `dfn` was used by `buildCactus`, pay attention.
int dep[MAXN * 2 + 5], fa[MAXN * 2 + 5][MAXLG + 5];
LL dis[MAXN * 2 + 5]; inline void initCactus( const int u ) {
dfn[u] = ++dfc;
for ( int i = 1; fa[u][i - 1]; fa[u][i] = fa[fa[u][i - 1]][i - 1], ++i );
adj( cac, u, v ) {
dep[v] = dep[u] + 1, dis[v] = dis[u] + cac.len[e], fa[v][0] = u;
initCactus( v );
}
} inline int lca( int u, int v ) {
if ( dep[u] < dep[v] ) u ^= v ^= u ^= v;
per ( i, MAXLG, 0 ) if ( dep[fa[u][i]] >= dep[v] ) u = fa[u][i];
if ( u == v ) return u;
per ( i, MAXLG, 0 ) if ( fa[u][i] != fa[v][i] ) u = fa[u][i], v = fa[v][i];
return fa[u][0];
} inline int climb( int u, const int par ) {
per ( i, MAXLG, 0 ) if ( dep[fa[u][i]] > dep[par] ) u = fa[u][i];
return u;
} Graph<MAXN * 2 + 5, MAXN * 2 + 5> virc; inline void vlink( int s, int t ) {
// s is t's ancestor in cactus tree.
if ( s > n ) {
int is = climb( t, s );
virc( s, is, dis[is] - dis[s] ), s = is;
}
if ( t > n ) {
virc( fa[t][0], t, dis[t] - dis[fa[t][0]] ), t = fa[t][0];
}
if ( s != t ) virc( s, t, dis[t] - dis[s] );
} inline void buildVirCac( std::vector<int>& vec ) {
static int top, stk[MAXN * 2 + 5];
virc.ecnt = 0, stk[top = 1] = 1; std::sort( vec.begin(), vec.end(), []( const int a, const int b ) {
return dfn[a] < dfn[b];
} ); for ( int u: vec ) if ( u != 1 ) {
int anc = lca( stk[top], u );
while ( dep[stk[top]] > dep[anc] ) {
int a = stk[top--], b = dep[stk[top]] < dep[anc] ? anc : stk[top];
vlink( b, a );
}
if ( stk[top] != anc ) stk[++top] = anc;
stk[++top] = u;
} while ( top > 1 ) {
int a = stk[top--], b = stk[top];
vlink( b, a );
}
} LL ans, f[MAXN * 2 + 5];
bool book[MAXN + 5]; inline void contri( const int u, const std::vector<int>& cir ) {
static int que[MAXN + 5], hd, tl;
int sz = int( cir.size() ); LL half = pre[u] >> 1; #define val( i ) ( pre[cir[i]] + ( i >= sz >> 1 ? pre[u] : 0 ) ) que[hd = tl = 1] = 0;
rep ( i, 1, sz - 1 ) {
while ( hd <= tl && val( i ) - val( que[hd] ) > half ) ++hd; if ( hd <= tl ) {
chkmax( ans, f[cir[que[hd]]] - val( que[hd] )
+ f[cir[i]] + val( i ) );
} while ( hd <= tl && f[cir[que[tl]]] - val( que[tl] )
<= f[cir[i]] - val( i ) ) --tl;
que[++tl] = i;
} #undef val
} inline void solve( const int u, const int par ) {
f[u] = -LINF;
adj( virc, u, v ) solve( v, u );
if ( u <= n ) {
LL mx = book[u] ? 0 : -LINF, sx = -LINF;
adj( virc, u, v ) {
if ( LL d = f[v] + virc.len[e]; d > mx ) sx = mx, mx = d;
else if ( d > sx ) sx = d;
}
chkmax( ans, mx + sx ), f[u] = mx;
} else {
static std::vector<int> cir; cir.clear(); LL tmpp = pre[par]; pre[par] = 0;
cir.push_back( par );
adj( virc, u, v ) cir.push_back( v );
int sz = int( cir.size() );
cir.resize( sz << 1 );
rep ( i, 0, sz - 1 ) cir[sz + i] = cir[i]; contri( u, cir );
pre[par] = tmpp; adj( virc, u, v ) chkmax( f[u], f[v] + virc.len[e] );
}
virc.head[u] = 0;
} int main() {
n = rint(), m = rint();
rep ( i, 1, m ) {
int u = rint(), v = rint(), w = rint();
src( u, v, w ), src( v, u, w );
} #ifdef RYBY
puts( "+++ +++ +++" );
#endif vnode = n, buildCactus( 1, 0 );
dfc = 0, dep[1] = 1, initCactus( 1 ); #ifdef RYBY
puts( "--- --- ---" );
#endif std::vector<int> vec;
for ( int q = rint(), k; q--; vec.clear() ) {
k = rint(), vec.resize( k );
rep ( i, 0, k - 1 ) book[vec[i] = rint()] = true;
buildVirCac( vec ); ans = 0, solve( 1, 0 );
wint( ans ), putchar( '\n' ); for ( int u: vec ) book[u] = false;
} return 0;
}

Solution -「UOJ #87」mx 的仙人掌的更多相关文章

  1. Solution -「UOJ #46」玄学

    \(\mathcal{Description}\)   Link.   给定序列 \(\{a_n\}\) 和 \(q\) 次操作,操作内容如下: 给出 \(l,r,k,b\),声明一个修改方案,表示 ...

  2. Solution -「UOJ #450」复读机

    \(\mathcal{Description}\)   Link.   求从 \(m\) 种颜色,每种颜色无限多的小球里选 \(n\) 个构成排列,使得每种颜色出现次数为 \(d\) 的倍数的排列方案 ...

  3. Solution -「ARC 104E」Random LIS

    \(\mathcal{Description}\)   Link.   给定整数序列 \(\{a_n\}\),对于整数序列 \(\{b_n\}\),\(b_i\) 在 \([1,a_i]\) 中等概率 ...

  4. Solution -「UNR #5」「UOJ #671」诡异操作

    \(\mathcal{Desciprtion}\)   Link.   给定序列 \(\{a_n\}\),支持 \(q\) 次操作: 给定 \(l,r,v\),\(\forall i\in[l,r], ...

  5. Solution -「JOISC 2020」「UOJ #509」迷路的猫

    \(\mathcal{Decription}\)   Link.   这是一道通信题.   给定一个 \(n\) 个点 \(m\) 条边的连通无向图与两个限制 \(A,B\).   程序 Anthon ...

  6. Solution -「UR #21」「UOJ #632」挑战最大团

    \(\mathcal{Description}\)   Link.   对于简单无向图 \(G=(V,E)\),定义它是"优美"的,当且仅当 \[\forall\{a,b,c,d\ ...

  7. Solution -「UR #2」「UOJ #32」跳蚤公路

    \(\mathcal{Description}\)   Link.   给定一个 \(n\) 个点 \(m\) 条边的带权有向图,每条边还有属性 \(s\in\{-1,0,1\}\).对于每个 \(u ...

  8. Solution -「CTS 2019」「洛谷 P5404」氪金手游

    \(\mathcal{Description}\)   Link.   有 \(n\) 张卡牌,第 \(i\) 张的权值 \(w_i\in\{1,2,3\}\),且取值为 \(k\) 的概率正比于 \ ...

  9. Solution -「BZOJ 3812」主旋律

    \(\mathcal{Description}\)   Link.   给定含 \(n\) 个点 \(m\) 条边的简单有向图 \(G=(V,E)\),求 \(H=(V,E'\subseteq E)\ ...

随机推荐

  1. vs2017 快捷键 - 总结

    1.格式化代码 先选中需要格式的代码,一般是全选[Ctrl+A]后,Ctrl+K+F[按定Ctrl不动,依序点击 K和F,然后再放开 Ctrl ] 2.多行注释 注释: 先CTRL+K,然后CTRL+ ...

  2. Three.js 实现虎年春节3D创意页面

    背景 虎年 春节将至,本文使用 React + Three.js 技术栈,实现趣味 3D 创意页面.本文包含的知识点主要包括:ShadowMaterial. MeshPhongMaterial 两种基 ...

  3. JAVA多线程之并发编程三大核心问题

    概述 并发编程是Java语言的重要特性之一,它能使复杂的代码变得更简单,从而极大的简化复杂系统的开发.并发编程可以充分发挥多处理器系统的强大计算能力,随着处理器数量的持续增长,如何高效的并发变得越来越 ...

  4. Go 分布式令牌桶限流 + 兜底策略

    上篇文章提到固定时间窗口限流无法处理突然请求洪峰情况,本文讲述的令牌桶线路算法则可以比较好的处理此场景. 工作原理 单位时间按照一定速率匀速的生产 token 放入桶内,直到达到桶容量上限. 处理请求 ...

  5. 【reverse】逆向3 寻找地址

    [reverse]逆向3 寻找地址 寻址公式一:[立即数] 读取内存的值: mov eax,dword prt ds:[0x13FFC4] 将内存编号为0x13FFC4.0x13FFC5.0x13FF ...

  6. [Jetson Nano]Jetson Nano快速入门

    NVIDIAJetsonNano开发套件是适用于制造商,学习者和开发人员的小型AI计算机.相比Jetson其他系列的开发板,官方报价只要99美金,可谓是相当有性价比.本文如何是一个快速入门的教程,主要 ...

  7. X架构键盘选购指南

    X架构键盘 联想 JME8002(未知) JME7597(推荐) 据说是ThinkPAD键盘的血统. Thinkapad 小红点键盘,据说腿撑容易坏 明基 明基精钢侠 海湾海贝(键程长,不累手,但不易 ...

  8. 使用Cesium的地下渲染技术可视化瑞士的地质资源

    Cesium中文网:http://cesiumcn.org/ | 国内快速访问:http://cesium.coinidea.com/ Camptocamp为瑞士拓扑的孪生数字增加了地下可视化功能. ...

  9. Docker 与 K8S学习笔记(十九)—— Pod的配置管理

    我们在部署应用时常常会考虑将应用程序与配置文件相分离,这样可以使应用程序更好的复用,并且通过不同配置也能实现更灵活的功能.将应用制作成镜像后,我们可以在启动容器时通过环境变量或挂载文件的方式注入,但是 ...

  10. Servlet Listener(监听器)

    监听器 Listener 是一个实现特定接口的 Java 程序,这个程序专门用于监听另一个 Java 对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法将立即自动执行.监听器的相关概 ...