bzoj 2286: [Sdoi2011]消耗战 虚树+树dp
2286: [Sdoi2011]消耗战
Time Limit: 20 Sec Memory Limit: 512 MB
[Submit][Status][Discuss]
Description
Input
第一行一个整数n,代表岛屿数量。
接下来n-1行,每行三个整数u,v,w,代表u号岛屿和v号岛屿由一条代价为c的桥梁直接相连,保证1<=u,v<=n且1<=c<=100000。
第n+1行,一个整数m,代表敌方机器能使用的次数。
接下来m行,每行一个整数ki,代表第i次后,有ki个岛屿资源丰富,接下来k个整数h1,h2,…hk,表示资源丰富岛屿的编号。
Output
输出有m行,分别代表每次任务的最小代价。
Sample Input
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
Sample Output
32
22
HINT
对于100%的数据,2<=n<=250000,m>=1,sigma(ki)<=500000,1<=ki<=n-1
Source
思路:虚树建树+树形dp;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<bitset>
#include<time.h>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x) cout<<"bug"<<x<<endl;
const int N=3e5+,M=1e6+,inf=1e9+,MOD=1e9+;
const LL INF=1e18+,mod=1e9+; struct edge
{
int v,w,next;
}edge[N<<];
int head[N],fa[N][],edg,deep[N];
int in[N],out[N],tot,mi[N][];
void init()
{
memset(head,-,sizeof(head));
edg=;tot=;
}
void add(int u,int v,int w)
{
edg++;
edge[edg].v=v;
edge[edg].w=w;
edge[edg].next=head[u];
head[u]=edg;
}
void dfs(int u,int fat)
{
tot++;
in[u]=tot;
for (int i=; i<= ;i++)
{
fa[u][i] = fa[fa[u][i-]][i-];
mi[u][i] = min(mi[u][i-],mi[fa[u][i-]][i-]);
}
for (int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
int w=edge[i].w;
if(v==fat) continue;
deep[v]=deep[u]+;
fa[v][]=u;
mi[v][]=w;
dfs(v,u);
}
out[u]=tot;
}
int RMQ_LCA(int x,int y) {
if(deep[x]<deep[y]) swap(x,y);
int d=deep[x]-deep[y];
for (int i=; i<= ;i++)
if((<<i)&d) x=fa[x][i];
for (int i=; i>= ;i--) {
if(fa[x][i]!=fa[y][i]) {
x=fa[x][i];y=fa[y][i];
}
}
if(x==y) return x;
else return fa[x][];
}
int getmi(int x,int y)//虚树保证在一条链上
{
int z=deep[y]-deep[x];
int ans=inf;
for(int i=;i<=;i++)
if(z&(<<i))ans=min(ans,mi[y][i]),y=fa[y][i];
return ans;
} int h[M];
struct f
{
int v,w,nex;
}edge2[N<<];
int head2[N],edg2;
void add2(int u,int v,int w)
{
edge2[++edg2]=(f){v,w,head2[u]};
head2[u]=edg2;
}
bool cmp(int u, int v) {
return in[u] < in[v];
}
bool check(int u, int v) {
return in[u] <= in[v] && in[v] <= out[u];
}
int build(int A[], int tot)
{
std::sort(A + , A + + tot, cmp);
for(int i = , old_tot = tot; i <= old_tot; i++)
A[++tot] = RMQ_LCA(A[i - ], A[i]);
std::sort(A + , A + + tot, cmp);
tot = std::unique(A + , A + + tot) - A - ;
std::stack<int> S; S.push(A[]);
for(int i = ; i <= tot; i++) {
while(!S.empty() && !check(S.top(), A[i])) S.pop();
int u = S.top(), v = A[i]; // u是v的祖先
int w=getmi(u,v);
add2(u,v,w);
add2(v,u,w);//u, v, deep[v] - deep[u]);
S.push(v);
}
return tot;
}
LL dp[N];
int cnt[N];
void dfs2(int u,int fa,LL pre)
{
LL si=;
for(int i=head2[u];i!=-;i=edge2[i].nex)
{
int v=edge2[i].v;
int w=edge2[i].w;
if(v==fa)continue;
dfs2(v,u,w);
si+=dp[v];
}
if(cnt[u])dp[u]=pre;
else dp[u]=min(pre,si);
}
int main()
{
init();
memset(head2,-,sizeof(head2));
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);add(v,u,w);
}
dfs(,);
int q;
scanf("%d",&q);
while(q--)
{
edg2=;
int t;
scanf("%d",&t);
for(int i=;i<=t;i++)
scanf("%d",&h[i]),cnt[h[i]]=;
h[++t]=;
t = build(h, t);
dfs2(,,INF);
printf("%lld\n",dp[]);
for(int i=;i<=t;i++)
head2[h[i]]=-,cnt[h[i]]=;
}
return ;
}
2286: [Sdoi2011]消耗战
Time Limit: 20 Sec Memory Limit: 512 MB
Submit: 4080 Solved: 1476
[Submit][Status][Discuss]
Description
Input
第一行一个整数n,代表岛屿数量。
接下来n-1行,每行三个整数u,v,w,代表u号岛屿和v号岛屿由一条代价为c的桥梁直接相连,保证1<=u,v<=n且1<=c<=100000。
第n+1行,一个整数m,代表敌方机器能使用的次数。
接下来m行,每行一个整数ki,代表第i次后,有ki个岛屿资源丰富,接下来k个整数h1,h2,…hk,表示资源丰富岛屿的编号。
Output
输出有m行,分别代表每次任务的最小代价。
Sample Input
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
Sample Output
32
22
HINT
对于100%的数据,2<=n<=250000,m>=1,sigma(ki)<=500000,1<=ki<=n-1
Source
bzoj 2286: [Sdoi2011]消耗战 虚树+树dp的更多相关文章
- BZOJ 2286: [Sdoi2011]消耗战 虚树 树形dp 动态规划 dfs序
https://www.lydsy.com/JudgeOnline/problem.php?id=2286 wa了两次因为lca犯了zz错误 这道题如果不多次询问的话就是裸dp. 一棵树上多次询问,且 ...
- BZOJ.2286.[SDOI2011]消耗战(虚树 树形DP)
题目链接 BZOJ 洛谷P2495 树形DP,对于每棵子树要么逐个删除其中要删除的边,要么直接断连向父节点的边. 如果当前点需要删除,那么直接断不需要再管子树. 复杂度O(m*n). 对于两个要删除的 ...
- bzoj 2286 [Sdoi2011]消耗战 虚树+dp
题目大意:多次给出关键点,求切断边使所有关键点与1断开的最小费用 分析:每次造出虚树,dp[i]表示将i和i子树与父亲断开费用 对于父亲x,儿子y ①y为关键点:\(dp[x]\)+=\(dismn( ...
- BZOJ 2286 [Sdoi2011]消耗战 ——虚树
虚树第一题. 大概就是建一颗只与询问有关的更小的新树,然后在虚树上DP #include <map> #include <ctime> #include <cmath&g ...
- BZOJ 2286: [Sdoi2011]消耗战 虚树
Description 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军 ...
- BZOJ 2286: [Sdoi2011]消耗战
2286: [Sdoi2011消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2082 Solved: 736[Submit][Status] ...
- bzoj 2286 [Sdoi2011]消耗战(虚树+树上DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2286 [题意] 给定一棵树,切断一条树边代价为ci,有m个询问,每次问使得1号点与查询 ...
- BZOJ 2286 [Sdoi2011]消耗战(虚树+树形DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2286 [题目大意] 出一棵边权树,每次给出一些关键点,求最小边割集, 使得1点与各个关 ...
- BZOJ 2286: [Sdoi2011消耗战 [DP 虚树]
传送门 题意: 删除价值和最小的边使得$1$号点与$k$个关键点不连通 一个树形DP...但是询问多次,保证总的关键点数为$O(n)$ 先说一下这个$DP$ $f[i]$表示子树$i$中的关键点与$1 ...
随机推荐
- 接口自动化框架(java)--1.项目概述
项目github地址: https://github.com/tianchiTester/API_AutoFramework 这套框架的报告是自己封装的 1.测试基类TestBase: 接口请求的te ...
- jmeter压测mysql报can not be represented as java.sql.Timestame错误解决方法
JDBC Request 测试mysql时报以下问题? jmeter报错信息: 解决方法: 在数据库url后拼接上字符串?characterEncoding=utf8&zeroDateTim ...
- C# string 常用功能的方法扩展
#region Usings using System; using System.Text; using System.Data; using System.Data.SqlClient; usin ...
- js点击什么显示什么的内容,隐藏其它和进度条
点击什么显示什么的内容 <div style="width:200px; height:40px"> <div class="yiji" st ...
- PL/SQL Block中对单引号进行转义
可以使用如下的方式: STR := q'[ CREATE TABLE TNAME AS SELECT ... FROM INPUT_TABLE IP WHERE ((IP.DATE_FIELD = T ...
- day16 python之匿名函数,递归函数
匿名函数 匿名函数格式 函数名 = lambda 参数 :返回值 #参数可以有多个,用逗号隔开 #匿名函数不管逻辑多复杂,只能写一行,且逻辑执行结束后的内容就是返回值 #返回值和正常的函数一样可以是任 ...
- OpenStack-Neutron-Fwaas-代码【二】
上一节从代码层面来讲解了fwaas的流程,这里通过具体查看iptables规则来说下应用规则的流程: 1.首先通过命令获取当前路由中的规则 #ip netns exec qrouter-[router ...
- 记录心得-FastJson分层解析demo示例
记录一下,平时用到,可速查!关键: // startArray(); 开始解析数组 // endArray(); 结束解析数组 // startObject(); 开始解析键值对 // endObje ...
- Ubuntu MariaDB PhpMyAdmin
root@www:~# apt-get -y install phpmyadmin php-mbstring php-gettext # select which one you using (thi ...
- Windbg程序调试系列5-高CPU问题分析
上篇博客中给大家分享了使用Windbg进行Live Debugging: Windbg程序调试系列4-Live Debugging 本篇中我们继续,跟大家分享常见的应用程序高CPU使用率问题分析. 先 ...