<虚树+树型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 ...
随机推荐
- Python C++ OpenCV TensorFlow手势识别(1-10) 毕设 定制开发
Python C++ OpenCV TensorFlow手势识别(1-10) 毕设 支持定制开发 (MFC,QT, PyQt5界面,视频摄像头识别) QQ: 3252314061 效果如下:
- Laravel自定义排序
如果数据库的status字段有0,1,2,3几种状态,如果想让status为1,2的状态排在最前面 那么可以这样: $obj = $obj->orderByRaw(DB::raw('FIELD( ...
- 前端与算法 leetcode 387. 字符串中的第一个唯一字符
目录 # 前端与算法 leetcode 387. 字符串中的第一个唯一字符 题目描述 概要 提示 解析 解法一:双循环 解法二:Set法单循环 算法 传入测试用例的运行结果 执行结果 GitHub仓库 ...
- HTML+css基础 p段落标签 a 超链接标签 Src和href有什么区别和关联? target属性 Meta标签
p段落标签: <p></p> 1.他是唯一一个可以不写结束标签的双标签. a 超链接标签: 从一个页面链接到另一个页面.靠的是href属性. Src和href有什么区别和关联 ...
- 函数防抖节流的理解及在Vue中的应用
防抖和节流的目的都是为了减少不必要的计算,不浪费资源,只在适合的时候再进行触发计算. 一.函数防抖 定义 在事件被触发n秒后再执行回调,如果在这n秒内又被触发,则重新计时:典型的案例就是输入搜索:输入 ...
- 【ELK】elasticsearch中使用脚本报错:scripts of type [inline], operation [update] and lang [groovy] are disabled
查看ID为2的这条数据: 使用更新命令: 使用脚本对年龄+5 curl -XPOST http://192.168.6.16:9200/my_new_index/user/2/_update?pret ...
- ActionMq + mqttws3.1 实现持久化订阅
activemq版本:5.15.3 Eclipse Paho MQTT JavaScript library mqttws3.1:在amq安装目录下webapp-demo目录下可以找到 实现步骤请阅读 ...
- RabbitMQ系列(二)环境搭建
参考: https://www.cnblogs.com/ericli-ericli/p/5902270.html https://blog.csdn.net/weixin_30619101/artic ...
- 我是如何一步步编码完成万仓网ERP系统的(六)产品库设计 2.百度Ueditor编辑器
https://www.cnblogs.com/smh188/p/11533668.html(我是如何一步步编码完成万仓网ERP系统的(一)系统架构) https://www.cnblogs.com/ ...
- 微信小程序+php 授权登陆,完整代码
先上图 实现流程: 1.授权登陆按钮和正文信息放到了同一个页面,未授权的时候显示登陆按钮,已授权的时候隐藏登陆按钮,显示正文信息,当然也可以授权和正文分开成两个页面,在授权页面的onlo ...