<虚树+树型DP> SDOI2011消耗战
<虚树+树型DP> SDOI2011消耗战
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 25e4 + 10;
inline LL in()
{
LL x = 0, flag = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') flag = 1; ch = getchar(); }
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
return x * flag;
}
int n, m;
struct Gra
{
int head[MAXN], nume;
struct Adj { int nex, to; LL w; } adj[MAXN << 1] ;
void clear()
{
memset(head, 0, sizeof head);
nume = 0;
}
void addedge(int from, int to, LL w)
{
adj[++ nume] = (Adj) { head[from], to, w } ;
head[from] = nume ;
}
void link(int from, int to, LL w)
{
addedge(from, to, w);
addedge(to, from, w);
}
} g[2];
int dep[MAXN], up[21][MAXN], lg[MAXN], dfn[MAXN], ind;
LL mn[MAXN];
void DFS(int u, int fa)
{
dfn[u] = ++ ind;
dep[u] = dep[fa] + 1;
up[0][u] = fa;
for (int i = 1; (1 << i) <= dep[u]; ++ i)
up[i][u] = up[i - 1][up[i - 1][u]];
for (int i = g[0].head[u]; i; i = g[0].adj[i].nex)
{
int v = g[0].adj[i].to;
if (v == fa) continue;
mn[v] = min(mn[u], g[0].adj[i].w);
DFS(v, u);
}
}
int lca(int x, int y)
{
if (dep[x] > dep[y]) swap(x, y);
while (dep[x] != dep[y]) y = up[lg[dep[y] - dep[x]]][y];
if (x == y) return x;
for (int i = lg[dep[x]]; i >= 0; -- i)
if (up[i][x] != up[i][y]) x = up[i][x], y = up[i][y];
return up[0][x];
}
int key[MAXN], stk[MAXN], top;
bool iskey[MAXN];
void insert(int u)
{
if (top == 1) return (void) (stk[++ top] = u);
int LCA = lca(u, stk[top]);
if (LCA == stk[top]) return (void) (stk[++ top] = u);
while (top > 1 && dep[LCA] <= dep[stk[top - 1]])
{
g[1].addedge(stk[top - 1], stk[top], 0);
-- top;
}
if (LCA != stk[top]) g[1].addedge(LCA, stk[top], 0), stk[top] = LCA;
stk[++ top] = u;
}
LL search(int u)
{
LL ret = 0;
for (int i = g[1].head[u]; i; i = g[1].adj[i].nex)
{
int v = g[1].adj[i].to;
ret += search(v);
}
if (iskey[u] || ret > mn[u] * 1LL) ret = mn[u];
iskey[u] = false; g[1].head[u] = 0;
return ret;
}
bool cmp(int x, int y) { return dfn[x] < dfn[y]; }
int main()
{
n = in();
for (int i = 1; i <= n; ++ i) lg[i] = lg[i - 1] + ((2 << lg[i - 1]) == i);
for (int i = 1; i < n; ++ i)
{
int u = in(), v = in(); LL w = in();
g[0].link(u, v, w);
}
mn[1] = 1e18;
DFS(1, 0);
m = in();
while (m --)
{
int q = in();
for (int i = 1; i <= q; ++ i) key[i] = in(), iskey[key[i]] = true ;
sort(key + 1, key + q + 1, cmp);
top = 0;
stk[++ top] = 1;
for (int i = 1; i <= q; ++ i) insert(key[i]);
while (top > 1) g[1].addedge(stk[top - 1], stk[top], 0), -- top;
g[1].nume = 0;
printf("%lld\n", search(1));
}
return 0;
}
<虚树+树型DP> SDOI2011消耗战的更多相关文章
- 【虚树学习笔记([SDOI2011]消耗战)】
题意 [SDOI2011]消耗战 想法 首先我们可以很自然的想到怎么在整棵树上进行求解\(DP\) 很简单 每个点有两个选择 要么对其子树的关键点递归求解 要么自己断开 当然断开的\(cost\)为其 ...
- bzoj 2286: [Sdoi2011]消耗战 虚树+树dp
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一 ...
- [BZOJ2286][SDOI2011]消耗战(虚树DP)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4998 Solved: 1867[Submit][Statu ...
- BZOJ2286 [Sdoi2011]消耗战 【虚树 + 树形Dp】
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 4261 Solved: 1552 [Submit][Sta ...
- BZOJ_2286_[Sdoi2011]消耗战_虚树+树形DP+树剖lca
BZOJ_2286_[Sdoi2011]消耗战_虚树+树形DP Description 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的 ...
- 洛谷P2495 [SDOI2011]消耗战(虚树dp)
P2495 [SDOI2011]消耗战 题目链接 题解: 虚树\(dp\)入门题吧.虚树的核心思想其实就是每次只保留关键点,因为关键点的dfs序的相对大小顺序和原来的树中结点dfs序的相对大小顺序都是 ...
- 虚树+【BZOJ2286】【SDOI2011】消耗战(虚树)(DP)
先看一道题: [BZOJ2286][SDOI2011]消耗战 Description 在一场战争中,战场由n个岛屿和n−1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总 ...
- BZOJ 2286 消耗战 - 虚树 + 树型dp
传送门 题目大意: 每次给出k个特殊点,回答将这些特殊点与根节点断开至少需要多少代价. 题目分析: 虚树入门 + 树型dp: 刚刚学习完虚树(好文),就来这道入门题签个到. 虚树就是将树中的一些关键点 ...
- 【BZOJ】2286: [Sdoi2011]消耗战 虚树+DP
[题意]给定n个点的带边权树,每次询问给定ki个特殊点,求隔离点1和特殊点的最小代价.n<=250000,Σki<=500000. [算法]虚树+DP [题解]考虑普通树上的dp,设f[x ...
随机推荐
- Jenkins打包编码GBK的不可映射字符
1.错误信息如下: 2.在Maven的POM中加入如下代码,然后重新打包即可. <properties> <!-- 文件拷贝时的编码 --> <project.bui ...
- UML类图记忆口诀
UML类图在设计模式书籍中用的比较多,经常忘记,口诀挺重要的,比如我们从小到大,除了乘法口诀.元素周期表等口诀形式的知识,其它的知识都基本忘记了, 所以编写口诀如下 1.三级石 2.见关一 3.零足迹 ...
- 深入理解react-native
欢迎转载,请支持原创,保留原文链接:http://blog.ilibrary.me http://blog.ilibrary.me/2016/12/25/react-native-internal ( ...
- .net Core 学习笔记(实体字段映射,IOC注入)
https://github.com/wj1034184751/ADO.NetCore.git 先简单的做一个学习架子, EF层用的是 EFCore(2.1.0)+ Pomelo.EntityFram ...
- 基于Jquery的进度条插件(实用)
Spin.js 最喜欢这款插件了,动画图片的长度.粗细.速度和角度都可以灵活控制,想要做成什么样都可以. 源码下载 在线演示 Percentage Loader 一款轻量的 jQuery 进 ...
- 详解:Java字符串类型"switch"的底层原理
前言: 最近更新得会比较频繁,希望大家见谅哦! 也感谢关注我的人,我会更加更加努力去做的! 基础 我们现在使用的Java的版本,基本上是都支持String类型的.当然除了String类型,还有int. ...
- Mybatis一对一,一对多,多对多代码
一对一 <!-- 关系映射 --> <!-- 1-1:自动映射 --> <select id="oneToOne" resultType=" ...
- CentOS 7.0 使用yum 安装 Mariadb
第一步: 使用命令查看是否已经安装: mysql -u root -p 返回 Enter password: 时表示已经安装成功的,需要卸载安装. 第二步: 使用yum直接安装mariadb,注意带 ...
- Bugku 代码审计
0x01.extract变量覆盖 代码: <?php$flag='xxx';extract($_GET);if(isset($shiyan)){$content=trim(file_get_co ...
- vue中的---MVVM(面试必问)
M---Model (数据) V---View (视图) VM---VIewModel (转换器) VIewModel主要做两件事: 1.把 Model 中的数据绑定到View(视图层). 2.监 ...