// 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. 如何使用picGo+typora配置云笔记

    PicGo的使用 安装 picGo 2.3版本 (window可用)---官网有点慢,已经下载到个人仓库 https://gitee.com/lht1132950411/study/blob/mast ...

  2. 10分钟学会 API 测试 !

    本文面向对象主要是后端开发人员   API 开发好之后,我们需要对 API 进行简单的调试,确保 API 可以跑通再提交给前端人员进行对接或者是测试人员对 API 进行测试:   在测试过程中我们关注 ...

  3. Lifted ElGamal 门限加密算法

    本文详细学习Lifted ElGamal 门限加密算法 门限加密体制 (1)门限加密是可以抗合谋的 (2)表现在私钥分为\(n\)份,至少需要\(t\)份才能解密成功,叫做(t-n)门限.类似于&qu ...

  4. C#/VB.NET 在Word转PDF时生成目录书签

    当我们在转换Word文档到PDF格式时,想保留Word文档的标题作为PDF书签,那么应该如何操作呢?那么本文将以C#及VB.NET代码为例,介绍如何在Word转PDF时生成目录书签.下面是具体方法和步 ...

  5. ESXI系列问题整理以及记录——使用SSH为设备打VIB驱动包,同时提供一种对于ESXI不兼容螃蟹网卡(Realtek 瑞昱)的问题解决思路

    对于ESXI不兼容螃蟹网卡的问题,这里建议购买一张博通的低端单口千兆网卡,先使用博通网卡完成系统部署,再按照下文方法添加螃蟹网卡的VIB驱动,最后拆除博通网卡. 螃蟹网卡VIB驱动包下载地址:http ...

  6. jvm造轮子

    博客内容来源于 刘欣老师的课程,刘欣老师的公众号 码农翻身 博客内容来源于 Java虚拟机规范(JavaSE7) 博客内容的源码 https://gitee.com/zumengjie/litejvm ...

  7. Linux 源码编译安装软件

    程序包编译安装的步骤: 源代码-->预处理-->编译-->汇编-->链接-->执行 多文件:文件中的代码之间,很可能存在跨文件依赖关系 1.编译源码的项目工具 使用相关的 ...

  8. Java获取当天或者明天等零点时间(00:00:00)0时0分0秒的方法

    SimpleDateFormat sdfYMD = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar calendar = ...

  9. Git下载(快速快速快速下载!!)

    在安装Git环境的时候,需要下载Git的安装包,但是官网的下载网速实在是太慢的(几十M的安装包,下载速度只有几十K) (所以可以在镜像中下载,速度超快) Git镜像下载链接-------------- ...

  10. 数学公式 Latex 练习

    \[1+x+x^2+x^3+\cdots=\frac{1}{1-x}\quad x\in(-1, 1) \] 证明:设左边式子项数为 \(n\) 那么可以得到: \[\begin{split} S & ...