题目链接

问题分析

其实是比较明显的动态DP。

懒于再推一遍式子,直接用 最小权点覆盖=全集-最大权独立集,然后就和这道题一样了。题解可以看这里

然后必须选或者不选的话,就直接把相应的点权变成\(-\infty\)或\(\infty\)就好了。如果是必须选,最后答案里不要忘了加回原来的值。

参考程序

#include <bits/stdc++.h>
using namespace std; const long long Maxn = 100010;
const long long INF = 10000000010;
struct matrix {
long long A[ 2 ][ 2 ];
matrix() {
A[ 0 ][ 0 ] = A[ 0 ][ 1 ] = A[ 1 ][ 0 ] = A[ 1 ][ 1 ] = -INF;
return;
}
matrix( long long *T ) {
A[ 0 ][ 0 ] = A[ 0 ][ 1 ] = T[ 0 ];
A[ 1 ][ 0 ] = T[ 1 ];
A[ 1 ][ 1 ] = -INF;
return;
}
matrix( long long a, long long b, long long c, long long d ) {
A[ 0 ][ 0 ] = a; A[ 0 ][ 1 ] = b; A[ 1 ][ 0 ] = c; A[ 1 ][ 1 ] = d;
return;
}
inline matrix operator * ( const matrix Other ) const {
return matrix( max( A[ 0 ][ 0 ] + Other.A[ 0 ][ 0 ], A[ 0 ][ 1 ] + Other.A[ 1 ][ 0 ] ),
max( A[ 0 ][ 0 ] + Other.A[ 0 ][ 1 ], A[ 0 ][ 1 ] + Other.A[ 1 ][ 1 ] ),
max( A[ 1 ][ 0 ] + Other.A[ 0 ][ 0 ], A[ 1 ][ 1 ] + Other.A[ 1 ][ 0 ] ),
max( A[ 1 ][ 0 ] + Other.A[ 0 ][ 1 ], A[ 1 ][ 1 ] + Other.A[ 1 ][ 1 ] ) );
}
};
struct edge {
long long To, Next;
edge() : To( 0 ), Next( 0 ) {}
edge( long long _To, long long _Next ) : To( _To ), Next( _Next ) {}
};
edge Edge[ Maxn << 1 ];
long long Start[ Maxn ], UsedEdge;
inline void AddEdge( long long x, long long y ) {
Edge[ ++UsedEdge ] = ( edge ) { y, Start[ x ] };
Start[ x ] = UsedEdge;
return;
}
long long n, m, Cost[ Maxn ], NowCost[ Maxn ], Sum;
char Ch[ 10 ];
long long Deep[ Maxn ], Father[ Maxn ], Size[ Maxn ], Son[ Maxn ], Top[ Maxn ], Index[ Maxn ], Ref[ Maxn ], Dfn[ Maxn ], Used;
long long Dp[ Maxn ][ 2 ], LDp[ Maxn ][ 2 ];
matrix Tree[ Maxn << 2 ]; void Build_Cut();
void Build();
void Change( long long u, long long Key );
matrix Query( long long Ind, long long Left, long long Right, long long L, long long R ); int main() {
scanf( "%lld%lld", &n, &m ); scanf( "%s", Ch );
for( long long i = 1; i <= n; ++i ) scanf( "%lld", &Cost[ i ] );
for( long long i = 1; i < n; ++i ) {
long long x, y; scanf( "%lld%lld", &x, &y );
AddEdge( x, y ); AddEdge( y, x );
}
Build_Cut();
Build();
for( long long i = 1; i <= m; ++i ) {
long long a, x, b, y;
scanf( "%lld%lld%lld%lld", &a, &x, &b, &y);
if( x == 0 ) { Sum -= Cost[ a ]; Sum += INF; Change( a, INF ); }
else { Sum -= Cost[ a ]; Sum += -INF; Change( a, -INF ); }
if( y == 0 ) { Sum -= Cost[ b ]; Sum += INF; Change( b, INF ); }
else { Sum -= Cost[ b ]; Sum += -INF; Change( b, -INF ); } matrix Temp = Query( 1, 1, n, Index[ 1 ], Dfn[ 1 ] );
long long Ans = max( Temp.A[ 0 ][ 0 ], Temp.A[ 1 ][ 0 ] );
Ans = Sum - Ans;
if( x == 1 ) Ans = Ans + INF + Cost[ a ];
if( y == 1 ) Ans = Ans + INF + Cost[ b ];
if( Ans >= INF ) printf( "-1\n" ); else printf( "%lld\n", Ans ); if( x == 0 ) { Sum -= INF; Sum += Cost[ a ]; Change( a, Cost[ a ] ); }
else { Sum -= -INF; Sum += Cost[ a ]; Change( a, Cost[ a ] ); }
if( y == 0 ) { Sum -= INF; Sum += Cost[ b ]; Change( b, Cost[ b ] ); }
else { Sum -= -INF; Sum += Cost[ b ]; Change( b, Cost[ b ] ); }
}
return 0;
} void Dfs1( long long u, long long Fa ) {
Deep[ u ] = Deep[ Fa ] + 1;
Father[ u ] = Fa;
Size[ u ] = 1;
for( long long t = Start[ u ]; t; t = Edge[ t ].Next ) {
long long v = Edge[ t ].To;
if( v == Fa ) continue;
Dfs1( v, u );
if( Size[ v ] > Size[ Son[ u ] ] ) Son[ u ] = v;
Size[ u ] += Size[ v ];
}
return;
} void Dfs2( long long u, long long Fa ) {
if( Son[ u ] ) {
Top[ Son[ u ] ] = Top[ u ];
Index[ Son[ u ] ] = ++Used;
Ref[ Used ] = Son[ u ];
Dfs2( Son[ u ], u );
}
for( long long t = Start[ u ]; t; t = Edge[ t ].Next ) {
long long v = Edge[ t ].To;
if( v == Fa || v == Son[ u ] ) continue;
Top[ v ] = v; Index[ v ] = ++Used; Ref[ Used ] = v;
Dfs2( v, u );
}
return;
} void Build_Cut() {
Dfs1( 1, 0 );
Top[ 1 ] = 1; Index[ 1 ] = ++Used; Ref[ Used ] = 1;
Dfs2( 1, 0 );
for( int i = 1; i <= n; ++i ) Dfn[ Top[ i ] ] = max( Dfn[ Top[ i ] ], Index[ i ] );
return;
} void Dfs3( long long u, long long Fa ) {
LDp[ u ][ 1 ] = NowCost[ u ];
for( long long t = Start[ u ]; t; t = Edge[ t ].Next ) {
long long v = Edge[ t ].To;
if( v == Fa || v == Son[ u ] ) continue;
Dfs3( v, u );
LDp[ u ][ 0 ] += max( Dp[ v ][ 0 ], Dp[ v ][ 1 ] );
LDp[ u ][ 1 ] += Dp[ v ][ 0 ];
}
if( Son[ u ] ) Dfs3( Son[ u ], u );
Dp[ u ][ 0 ] = LDp[ u ][ 0 ] + max( Dp[ Son[ u ] ][ 0 ], Dp[ Son[ u ] ][ 1 ] );
Dp[ u ][ 1 ] = LDp[ u ][ 1 ] + Dp[ Son[ u ] ][ 0 ];
return;
} void RealBuild( long long Ind, long long Left, long long Right ) {
if( Left == Right ) {
Tree[ Ind ] = matrix( LDp[ Ref[ Left ] ] );
return;
}
long long Mid = ( Left + Right ) >> 1;
RealBuild( Ind << 1, Left, Mid );
RealBuild( Ind << 1 | 1, Mid + 1, Right );
Tree[ Ind ] = Tree[ Ind << 1 ] * Tree[ Ind << 1 | 1 ];
return;
} matrix Query( long long Ind, long long Left, long long Right, long long L, long long R ) {
if( L <= Left && Right <= R ) return Tree[ Ind ];
long long Mid = ( Left + Right ) >> 1;
if( R <= Mid ) return Query( Ind << 1, Left, Mid, L, R );
if( L > Mid ) return Query( Ind << 1 | 1, Mid + 1, Right, L, R );
return Query( Ind << 1, Left, Mid, L, R ) * Query( Ind << 1 | 1, Mid + 1, Right, L, R );
} void Build() {
for( long long i = 1; i <= n; ++i ) NowCost[ i ] = Cost[ i ];
for( long long i = 1; i <= n; ++i ) Sum += NowCost[ i ];
Dfs3( 1, 0 );
RealBuild( 1, 1, n );
return;
} void Update( long long Ind, long long Left, long long Right, long long x ) {
if( Left == Right ) {
Tree[ Ind ] = matrix( LDp[ Ref[ Left ] ] );
return;
}
long long Mid = ( Left + Right ) >> 1;
if( x <= Mid ) Update( Ind << 1, Left, Mid, x );
if( x > Mid ) Update( Ind << 1 | 1, Mid + 1, Right, x );
Tree[ Ind ] = Tree[ Ind << 1 ] * Tree[ Ind << 1 | 1 ];
return;
} void Change( long long u, long long Key ) {
matrix Last = Query( 1, 1, n, Index[ Top[ u ] ], Dfn[ Top[ u ] ] );
LDp[ u ][ 1 ] += -NowCost[ u ] + Key; NowCost[ u ] = Key;
Update( 1, 1, n, Index[ u ] );
matrix Temp = Query( 1, 1, n, Index[ Top[ u ] ], Dfn[ Top[ u ] ] );
u = Father[ Top[ u ] ];
while( u ) {
matrix TTT = Query( 1, 1, n, Index[ Top[ u ] ], Dfn[ Top[ u ] ] );
LDp[ u ][ 0 ] += -max( Last.A[ 0 ][ 0 ], Last.A[ 1 ][ 0 ] ) + max( Temp.A[ 0 ][ 0 ], Temp.A[ 1 ][ 0 ] );
LDp[ u ][ 1 ] += -Last.A[ 0 ][ 0 ] + Temp.A[ 0 ][ 0 ];
Last = TTT;
Update( 1, 1, n, Index[ u ] );
Temp = Query( 1, 1, n, Index[ Top[ u ] ], Dfn[ Top[ u ] ] );
u = Father[ Top[ u ] ];
}
return;
}

luoguP5024 保卫王国的更多相关文章

  1. luoguP5024 保卫王国 动态dp

    题目大意: emmmmm 题解: QAQ #include <cstdio> #include <cstring> #include <iostream> usin ...

  2. LuoguP5024 保卫王国(动态DP,LCT)

    最小权覆盖集 = 全集 - 最大权独立集 强制取点.不取点可以使用把权值改成正无穷或负无穷实现 接下来就是经典的"动态最大权独立集"了 O(nlogn). 这不是我说的,是immo ...

  3. noip2018 d2t3 保卫王国 解题报告

    保卫王国 电脑卡懒得把题面挪过来了. 朴素 \[ dp_{i,0}=\sum dp_{s,1}\\ dp_{i,1}=\sum \min(dp_{s,0},dp_{s,1})+p_i \] 然后直接动 ...

  4. LG5024 保卫王国

    题意 题目描述 Z 国有\(n\)座城市,\(n - 1\)条双向道路,每条双向道路连接两座城市,且任意两座城市 都能通过若干条道路相互到达. Z 国的国防部长小 Z 要在城市中驻扎军队.驻扎军队需要 ...

  5. 「NOIP2018」保卫王国

    「NOIP2018保卫王国」 题目描述 有一棵 \(n\) 个点, 点有点权 \(a_i\),\(m\) 组询问, 每次求钦点两个节点必须选或者必须不选后的树上最小点覆盖. \(1 \leq n, m ...

  6. Uoj 441 保卫王国

    Uoj 441 保卫王国 动态 \(dp\) .今天才来写这个题. 设 \(f[u][0/1]\) 表示子树 \(u\) 中不选/选 \(u\) 时的最小权值和,显然有:\(f[u][0]=\sum ...

  7. 竞赛题解 - NOIP2018 保卫王国

    \(\mathcal{NOIP2018}\) 保卫王国 - 竞赛题解 按某一个炒鸡dalao名曰 taotao 的话说: \(\ \ \ \ \ \ \ \ \ "一道sb倍增题" ...

  8. [NOIP2018TG]保卫王国

    [NOIP2018TG]保卫王国 BZOJ luogu 当动态dp模板题写的,(全集-最大点权独立集)不能放军队的+inf,必须放军队-inf即可 注意矩阵乘法的顺序问题 #define ll lon ...

  9. 『保卫王国 树上倍增dp』

    保卫王国 Description Z 国有n座城市,n - 1条双向道路,每条双向道路连接两座城市,且任意两座城市 都能通过若干条道路相互到达. Z 国的国防部长小 Z 要在城市中驻扎军队.驻扎军队需 ...

随机推荐

  1. Java EE javax.servlet中的ServletResponse接口

    ServletResponse接口 public interface ServletResponse 子接口:HttpServletResponse 实现类:HttpServletResponseWr ...

  2. 【原创】大数据基础之Chronos

    官方:https://mesos.github.io/chronos/ mesos集群中替换crontab Chronos A fault tolerant job scheduler for Mes ...

  3. UVA571Jugs题解--简单数论(其实是瞎搞)

    题目链接 https://cn.vjudge.net/problem/UVA-571 分析 刚做了道倒水问题的题想看看能不能水二倍经验,结果发现了这道题 题意翻译:https://www.cnblog ...

  4. 在Markdown中写公式块

    Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式. Markdown中的公式语法是遵循LaTex语法的 $ sum = \sum_{i ...

  5. window, linux, mac 比较文件和文件夹的区别

    windows 端 winmerge beyondcompare Mac 和 linux  端 Meld kdiff3 diff command 更多可参考:https://alternativeto ...

  6. 学习--Spring IOC源码精读

    Spring核心IOC的源码分析(转载) 原文地址:https://javadoop.com/post/spring-ioc#toc11 转载地址:https://blog.csdn.net/nuom ...

  7. 解决 /etc/init.d/php-fpm no such file or directory等相关问题

    vi /etc/init.d/php-fpm #! /bin/sh # Comments to support chkconfig on CentOS # chkconfig: 2345 65 37 ...

  8. 论文笔记:Learning Region Features for Object Detection

    中心思想 继Relation Network实现可学习的nms之后,MSRA的大佬们觉得目标检测器依然不够fully learnable,这篇文章类似之前的Deformable ROI Pooling ...

  9. zabbix 监控TCP状态连接数

    1.zabbix客户端,监控TCP状态脚本,并保存到的定路径.(/usr/local/zabbix-agent/shells) # cat zabbix_linux_plugin.sh #!/bin/ ...

  10. java-消息队列相关-activeMQ

    ,1,如何防止activeMQ崩溃导致消息丢失呢? 第一点,首先消息需要使用持久化消息,服务挂掉,重启服务后消息依然可以消费,不会丢失: 第二点,ActiveMQ采用主从模式搭建集群,比如搭建3台主从 ...