// never forget open "Head.cpp", boy, never !
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause") #else #define D_e_Line ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
#include <vector> const int N = 250007; int n; namespace EdgeStar{ struct Edge{
int nxt, pre, w;
}e[N << 1];
int head[N], cntEdge;
inline void add(int u, int v, int w){
e[++cntEdge] = (Edge){head[u], v, w}, head[u] = cntEdge;
} }
using namespace EdgeStar; long long minn[N]; namespace TreeChainParticipation{ int siz[N], son[N], fa[N], dep[N];
inline void DFS_First(int u, int father){
fa[u] = father, dep[u] = dep[father] + 1, siz[u] = 1;
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(v == father) continue;
minn[v] = Min(minn[u], e[i].w);
DFS_First(v, u); siz[u] += siz[v];
if(!son[u] || siz[v] > siz[son[u]]){
son[u] = v;
}
} }
int top[N], dfn[N], dfnIdx;
inline void DFS_Second(int u, int ancester){
top[u] = ancester, dfn[u] = ++dfnIdx;
if(!son[u]) return;
DFS_Second(son[u], ancester);
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(v != fa[u] && v != son[u]){
DFS_Second(v, v);
}
}
}
inline int LCA(int x, int y){
while(top[x] != top[y]){
if(dep[top[x]] < dep[top[y]]) Swap(x, y);
x = fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
} }
using namespace TreeChainParticipation; int sta[N], _top;
vector<int> G[N]; //Vitual Tree
inline void Insert(int x){
if(_top == 1){
sta[++_top] = x;
return;
}
int lca = LCA(x, sta[_top]);
if(lca == sta[_top]){
//G[sta[_top]].push_back(x);
//sta[++_top] = x;
return;
}
while(_top > 1 && dfn[sta[_top - 1]] >= dfn[lca]){
G[sta[_top - 1]].push_back(sta[_top]);
--_top;
}
// maybe it's not need in this problem // or maybe need ? // shit, of course ! Who said the node 1 should be root
if(lca != sta[_top]){
G[lca].push_back(sta[_top]);
sta[_top] = lca;
}
sta[++_top] = x;
} inline long long DFS(int u){
if(G[u].size() == 0) return minn[u];
long long sum = 0;
for(vector<int>::iterator v = G[u].begin(); v != G[u].end(); ++v){
sum += DFS(*v);
}
G[u].clear();
return Min(sum, minn[u]);
} int act[N]; inline int cmp(const int &a, const int &b){
return dfn[a] < dfn[b];
} int main(){ io >> n;
R(i,2,n){
int u, v, w;
io >> u >> v >> w;
add(u, v, w);
add(v, u, w);
} minn[1] = 9223372036854775807; DFS_First(1, 0);
DFS_Second(1, 1); int Tasks;
io >> Tasks;
while(Tasks--){
int m;
io >> m;
R(i,1,m){
io >> act[i];
}
sort(act + 1, act + m + 1, cmp); // important !
_top = 1;
sta[_top] = 1;
R(i,1,m){
Insert(act[i]);
}
while(_top){
G[sta[_top - 1]].push_back(sta[_top]);
--_top;
} printf("%lld\n", DFS(1));
} return 0;
} /*
10
1 5 13
1 9 6
2 1 19
2 4 8
2 3 91
5 6 8
7 5 4
7 8 31
10 7 9
3
2 10 6
4 5 7 8 3
3 9 4 6 */

BZOJ2286/Luogu2495 [Sdoi2011]消耗战 (虚树)的更多相关文章

  1. 【BZOJ2286】[Sdoi2011]消耗战 虚树

    [BZOJ2286][Sdoi2011]消耗战 Description 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的 ...

  2. [BZOJ2286][SDOI2011]消耗战(虚树DP)

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 4998  Solved: 1867[Submit][Statu ...

  3. bzoj2286: [Sdoi2011]消耗战 虚树

    在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军胜利在望.已知在其他k个 ...

  4. BZOJ2286: [Sdoi2011]消耗战(虚树/树形DP)

    Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 5246  Solved: 1978[Submit][Status][Discuss] Descript ...

  5. bzoj 2286: [Sdoi2011]消耗战 虚树+树dp

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一 ...

  6. bzoj 2286 [Sdoi2011]消耗战 虚树+dp

    题目大意:多次给出关键点,求切断边使所有关键点与1断开的最小费用 分析:每次造出虚树,dp[i]表示将i和i子树与父亲断开费用 对于父亲x,儿子y ①y为关键点:\(dp[x]\)+=\(dismn( ...

  7. 【BZOJ】2286: [Sdoi2011]消耗战 虚树+DP

    [题意]给定n个点的带边权树,每次询问给定ki个特殊点,求隔离点1和特殊点的最小代价.n<=250000,Σki<=500000. [算法]虚树+DP [题解]考虑普通树上的dp,设f[x ...

  8. [SDOI2011]消耗战(虚树+树形动规)

    虚树dp 虚树的主要思想: 不遍历没用的的节点以及没用的子树,从而使复杂度降低到\(\sum\limits k\)(k为询问的节点的总数). 所以怎么办: 只把询问节点和其LCA放入询问的数组中. 1 ...

  9. bzoj 2286(洛谷 2495) [Sdoi2011]消耗战——虚树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2286 https://www.luogu.org/problemnew/show/P2495 ...

随机推荐

  1. Kubernetes client-go DeltaFIFO 源码分析

    概述Queue 接口DeltaFIFO元素增删改 - queueActionLocked()Pop()Replace() 概述 源码版本信息 Project: kubernetes Branch: m ...

  2. Docker打包镜像并上传

    Docker打包镜像并上传 登录 账号 docker login --username=yourusername 密码 yourPassword 推送到仓库 docker镜像打标签 docker ta ...

  3. 深入C++04:模板编程

    模板编程 函数模板 模板意义:对类型也进行参数化: 函数模板:是不编译的,因为类型不知道 模板的实例化:函数调用点进行实例化,生成模板函数 模板函数:这才是要被编译器所编译的 函数模板.模板的特例化. ...

  4. Java添加条形码到PDF表格

    条码的应用已深入生活和工作的方方面面.在处理条码时,常需要和各种文档格式相结合.当需要在文档中插入.编辑或者删除条码时,可借助于一些专业的类库工具来实现.本文,以操作PDF文件为例,介绍如何在编辑表格 ...

  5. CabloyJS也有工作流引擎了,是你想要的吗?

    前言 众所周知,NodeJS作为后端开发语言和运行环境,样样都好,就差一个NodeJS工作流引擎.CabloyJS 4.0重点开发了NodeJS工作流引擎,并作为内置的基础核心模块,近一步拓展了Nod ...

  6. Java绘图基础

    <零基础学Java> Java绘图基础 绘图是高级程序设计中非常重要的技术,例如,应用程序需要绘制闪屏图像.背景图像.组件外观Web程序可以绘制统计图.数据库存储的图像资源等. Graph ...

  7. NC14301 K-th Number

    NC14301 K-th Number 题目 题目描述 Alice are given an array A[1..N] with N numbers. Now Alice want to build ...

  8. Kalman卡尔曼滤波,Least Square最小二乘估计,加权最小二乘,递归最小二乘

    以下是Kalman的收敛性证明思路: cite:Stochastic Processes and Filtering Theory

  9. rust实战系列-base64编码

    前言 某些只能使用ASCII字符的场景,往往需要传输非ASCII字符的数据,这时就需要一种编码可以将数据转换成ASCII字符,而base64编码就是其中一种. 编码原理很简单,将原始数据以3字节(24 ...

  10. 编译安装Python出现“pip is configured with locations that require TLS/SSL, however the ssl.....”

    ubuntu: sudo apt-get install libssl-dev Cenos: sudo yum install openssl-devel 重新编译: ./configure --en ...