当前位置:网站首页>P5024 [noip2018 improvement group] solution to defending the Kingdom
P5024 [noip2018 improvement group] solution to defending the Kingdom
2022-07-20 19:39:00 【wkh2021】
Ideas :
We can use tree D P DP DP Find a point in linear time u u u If the color is c c c , Then the minimum cost of the whole tree is f u , c f_{u,c} fu,c
( The specific method is to form a tree from bottom to top D P DP DP Draw point u u u If elected c c c The minimum cost of this color in the whole subtree is d p 1 u , c dp1_{u,c} dp1u,c , Then from top to bottom D P DP DP Draw point u u u If your father chooses c c c If this color , With u u u Root u u u The minimum cost in the father's tree is d p 2 u , c dp2_{u,c} dp2u,c , The specific details are not detailed here )
It is found that if two points are fixed u , v u,v u,v The colors of are c , d c,d c,d , Then it should be for the tree u u u To v v v On the path ( It doesn't contain u u u and v v v ) Consider whether to dye black at each point of .
because d p dp dp Information is reducible , So if the dyeing scheme on that chain has been determined , We can easily calculate the total cost : Consider three consecutive points on the path from top to bottom u , v , w u,v,w u,v,w The color is c c c , that v v v My contribution is f v , c − d p 1 w , d − d p 2 v , d f_{v,c}-dp1_{w,d}-dp2_{v,d} fv,c−dp1w,d−dp2v,d , among d d d It means that you can and c c c Adjacent colors . We count this contribution in ( u , v ) (u,v) (u,v) On this side .
Consider how to determine the optimal on chain coloring scheme . For the two chains from top to bottom on the tree , The father at the top of one chain is the bottom of the other chain , We want to merge the information of these two chains . Find out D P DP DP The transfer is only related to the color of both ends of the chain , So for a chain, just record whether its two sides are dyed black . Enumerate the colors of two adjacent points when merging , If not all for 0 Then it's legal .
therefore , Let's consider doubling . Make g i , j , k g_{i,j,k} gi,j,k Express i i i The upward length of the point is 2 j 2^j 2j Chain , The order is from bottom to top or from top to bottom D P DP DP value . In this way, we can transfer through multiplication , Inquiry is like inquiry L C A LCA LCA Same query . Time complexity O ( ( n + q ) l o g n ) O((n+q)logn) O((n+q)logn) .
There are many details in the code implementation , Pay attention to the situation of a point in another sub tree when asking for special judgment .
AC code:
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long llong;
const int maxn = 1e5, maxm = 2e5, logn = 16; const llong infl = 1e18 + 1e9 + 1;
int n, m, a[maxn + 3], tot, ter[maxm + 3], nxt[maxm + 3], lnk[maxn + 3];
int dep[maxn + 3], cnt, l[maxn + 3], r[maxn + 3], fa[maxn + 3][logn + 3];
llong dp1[maxn + 3][2], dp2[maxn + 3][2], f[maxn + 3][2];
inline void upd_min(llong &a, llong b) {
a = min(a, b);
}
struct node {
llong dp[2][2];
node() {
dp[0][0] = dp[0][1] = dp[1][0] = dp[1][1] = infl; }
llong get_min() {
llong ans = infl;
for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) {
upd_min(ans, dp[i][j]);
}
return ans;
}
friend inline node merge(const node &a, const node &b) {
node c;
for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) for (int l = 0; l < 2; l++) {
if (k || l) upd_min(c.dp[i][j], a.dp[i][k] + b.dp[l][j]);
}
}
return c;
}
} g[maxn + 3][logn + 3][2];
void add_edge(int u, int v) {
ter[++tot] = v;
nxt[tot] = lnk[u];
lnk[u] = tot;
}
void dfs1(int u, int p) {
dp1[u][1] = a[u];
for (int e = lnk[u], v; e; e = nxt[e]) {
if ((v = ter[e]) == p) continue;
dfs1(v, u);
dp1[u][0] += dp1[v][1];
dp1[u][1] += min(dp1[v][0], dp1[v][1]);
}
}
void dfs2(int u, int p) {
for (int e = lnk[u], v; e; e = nxt[e]) {
if ((v = ter[e]) == p) continue;
dp2[v][0] = dp2[u][1] + dp1[u][0] - dp1[v][1];
dp2[v][1] = min(dp2[u][0], dp2[u][1]) + dp1[u][1] - min(dp1[v][0], dp1[v][1]);
dfs2(v, u);
}
f[u][0] = dp2[u][1];
f[u][1] = a[u] + min(dp2[u][0], dp2[u][1]);
for (int e = lnk[u], v; e; e = nxt[e]) {
if ((v = ter[e]) == p) continue;
f[u][0] += dp1[v][1];
f[u][1] += min(dp1[v][0], dp1[v][1]);
}
}
void dfs3(int u, int p) {
dep[u] = dep[p] + 1, fa[u][0] = p;
l[u] = r[u] = ++cnt;
llong A = f[p][0] - dp1[u][1] - dp2[p][1];
llong B = f[p][1] - min(dp1[u][0], dp1[u][1]) - min(dp2[p][0], dp2[p][1]);
g[u][0][0].dp[0][0] = A, g[u][0][0].dp[1][1] = B;
g[u][0][1].dp[0][0] = A, g[u][0][1].dp[1][1] = B;
for (int i = 0, t; (t = fa[fa[u][i]][i]); i++) {
fa[u][i + 1] = t;
g[u][i + 1][0] = merge(g[u][i][0], g[fa[u][i]][i][0]);
g[u][i + 1][1] = merge(g[fa[u][i]][i][1], g[u][i][1]);
}
for (int e = lnk[u], v; e; e = nxt[e]) {
if ((v = ter[e]) == p) continue;
dfs3(v, u), r[u] = r[v];
}
}
llong solve(int u, int a, int v, int b) {
if (dep[u] > dep[v]) swap(u, v), swap(a, b);
node A, B;
B.dp[b][b] = f[v][b] - (!b ? dp2[v][1] : min(dp2[v][0], dp2[v][1]));
if (l[u] <= l[v] && l[v] <= r[u]) {
int diff = dep[v] - dep[u] - 1;
for (int i = 0; i <= logn; i++) {
if (diff >> i & 1) {
B = merge(g[v][i][1], B);
v = fa[v][i];
}
}
A.dp[a][a] = f[u][a] - (!a ? dp1[v][1] : min(dp1[v][0], dp1[v][1]));
return merge(A, B).get_min();
}
A.dp[a][a] = f[u][a] - (!a ? dp2[u][1] : min(dp2[u][0], dp2[u][1]));
int diff = dep[v] - dep[u];
for (int i = 0; i <= logn; i++) {
if (diff >> i & 1) {
B = merge(g[v][i][1], B);
v = fa[v][i];
}
}
if (fa[u][0] == fa[v][0]) goto next_part;
for (int i = logn; ~i; i--) {
if (fa[u][i] != fa[v][i]) {
A = merge(A, g[u][i][0]);
B = merge(g[v][i][1], B);
u = fa[u][i], v = fa[v][i];
}
}
next_part:;
node C; int x = fa[u][0];
C.dp[0][0] = f[x][0] - dp1[u][1] - dp1[v][1];
C.dp[1][1] = f[x][1] - min(dp1[u][0], dp1[u][1]) - min(dp1[v][0], dp1[v][1]);
return merge(A, merge(C, B)).get_min();
}
int main() {
scanf("%d %d %*s", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1, u, v; i < n; i++) {
scanf("%d %d", &u, &v);
add_edge(u, v), add_edge(v, u);
}
dfs1(1, 0);
dfs2(1, 0);
/* for (int i = 1; i <= n; i++) { for (int j = 0; j < 2; j++) { printf("%d %d %lld\n", i, j, f[i][j]); } } */
dfs3(1, 0);
for (int a, x, b, y; m--; ) {
scanf("%d %d %d %d", &a, &x, &b, &y);
llong ret = solve(a, x, b, y);
printf("%lld\n", ret == infl ? -1 : ret);
}
return 0;
}
边栏推荐
- S3c2440 u-boot migration - norflash driver support - s29al016u-boot version: 2008.10 development board: mini2440
- 关于GCC编译常用命令
- YACS 两数之积 题解
- CentOS7中安装mysql
- 域名解析中“TTL”是什么意思?
- /bin/bash^M: 坏的解释器: 没有那个文件或目录
- Preparation of GL hsanps glycyrrhizic acid coupled human serum albumin loaded resveratrol / Rhein phospholipid complex serum protein nanoparticles
- The difference between nor flash startup and NAND flash startup on S3C2440
- Arm assembly - BIC, Orr
- [arrays and common operations of arrays]
猜你喜欢
Installing MySQL in centos7
redhat安装过程及问题
DTX-GA-BSA NPs 载多西他赛和藤黄酸白蛋白纳米粒/硫鸟嘌呤白蛋白纳米粒
Wechat applet development uses onreachbottom to realize page bottom loading and paging
【开源】MagicData-RAMC :180小时中文对话式语音数据集正式发布
水溶性阿魏酸钠白蛋白纳米粒/P-CS-NP包载替尼泊苷多层包衣血清蛋白纳米粒的制备方法
面试官必问的 3 道 MQ 面试题,还有谁不会??
RES-BSANP白藜芦醇白蛋白纳米粒/包裹紫杉烷类的白蛋白纳米颗粒载体
STM32 -- RTC real time clock
Preparation of inh-rfp-bsa-nps loaded INH and RFP albumin nanoparticles / capataxel loaded albumin nanoparticles
随机推荐
The first batch of | magic data and other 10 enterprises promote the product evaluation of the data annotation platform of the Chinese Academy of communications
Array sort usage (sorting) functions can be used
nmos和pmos区别、工作原理及基本结构详解
Preparation of GL hsanps glycyrrhizic acid coupled human serum albumin loaded resveratrol / Rhein phospholipid complex serum protein nanoparticles
Community Shangxin | magichub IO open source this wave of data is quite "Ollie to"
The difference between nor flash startup and NAND flash startup on S3C2440
DML在图形界面化工具的使用
载他克莫司的HSA蛋白纳米粒/DCT-BSA多西紫杉醇白蛋白纳米粒/血清白蛋白-透明质酸纳米颗粒的制备
社区上新 | MagicHub.io开源这波数据相当“奥利给”
I met me | virtual digital human cultivation, facegood virtual digital human open source technology seminar
/bin/bash^M: 坏的解释器: 没有那个文件或目录
DOM 事件类型
程序员怎么写bug
【转载】pycharm打包.py程序为可执行文件exe
Win32 API下的多线程编程
>/dev/null 2>&1 &
广发证券网上开户?安全吗?
Preparation of PDA RBCs NPs polydopamine modified erythrocyte nanoparticles / hyaluronic acid coated brucine bovine serum protein nanoparticles
MySQL common statement knowledge points
[cloud co creation] design Huawei cloud storage architecture with the youngest cloud service hcie (Part 2)