BZOJ 2286: [Sdoi2011]消耗战

Description

在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达。现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军胜利在望。已知在其他k个岛屿上有丰富能源,为了防止敌军获取能源,我军的任务是炸毁一些桥梁,使得敌军不能到达任何能源丰富的岛屿。由于不同桥梁的材质和结构不同,所以炸毁不同的桥梁有不同的代价,我军希望在满足目标的同时使得总代价最小。
侦查部门还发现,敌军有一台神秘机器。即使我军切断所有能源之后,他们也可以用那台机器。机器产生的效果不仅仅会修复所有我军炸毁的桥梁,而且会重新随机资源分布(但可以保证的是,资源不会分布到1号岛屿上)。不过侦查部门还发现了这台机器只能够使用m次,所以我们只需要把每次任务完成即可。

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

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

Sample Output

12

32

22

HINT

对于100%的数据,2<=n<=250000,m>=1,sigma(ki)<=500000,1<=ki<=n-1
#分析
显然如果只有一个询问的话只要树形dp即可。
若是多个询问的话我们就可以通过构建虚树来优化复杂度。
对于被选定的点对(x,y),设f=lca(x,y),若x和f之间没有被选定的点,那么x到f的路径就可以变为f->x。那么就可以把这棵树变成一棵只有k*2个节点的树,然后树形dp即可。
现在关键是如何构建虚树。
先把所有选定的点按照dfs序排序,假设y是x和y的lca则x必然可以省去。我们维护一个栈,对于新加入的一个点x,设栈顶元素为y,f=lca(x,y),那么y到f的路径上的点就可以被压缩,然后把f和x入栈即可。

代码

#include <bits/stdc++.h>

const int N = 250005;

typedef long long ll;

int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}

struct Edge
{
    int to,next;
    ll w;
}e[N * 2];

int next1[N],next2[N];
int cnt;

void add1(int x,int y,int z)
{
    e[++cnt].to = y, e[cnt].next = next1[x], next1[x] = cnt; e[cnt].w = z;
    e[++cnt].to = x, e[cnt].next = next1[y], next1[y] = cnt; e[cnt].w = z;
}

void add2(int x,int y)
{
    if (x == y)
        return;
    e[++cnt].to = y, e[cnt].next = next2[x], next2[x] = cnt;
}

int tim;
int dfn[N];
int dep[N];
ll mn[N];

int logn;
int fa[N][20];

void dfs(int x)
{
    dfn[x] = ++tim;
    for (int i = 1; i <= logn; i++)
        fa[x][i] = fa[fa[x][i - 1]][i - 1];
    dep[x] = dep[fa[x][0]] + 1;
    for (int i = next1[x]; i; i = e[i].next)
    {
        if (e[i].to == fa[x][0])
            continue;
        fa[e[i].to][0] = x;
        mn[e[i].to] = std::min(mn[x], e[i].w);
        dfs(e[i].to);
    }
}

int getLca(int x,int y)
{
    if (dep[x] < dep[y])
        std::swap(x,y);
    for (int i = logn; i >= 0; i--)
    {
        if (dep[fa[x][i]] >= dep[y])
            x = fa[x][i];
    }
    if (x == y)
        return x;
    for (int i = logn; i >= 0; i--)
    {
        if (fa[x][i] != fa[y][i])
            x = fa[x][i], y = fa[y][i];
    }
    return fa[x][0];
}

ll f[N];
int t[N];

int a[N];

void dp(int x)
{
    f[x] = mn[x];
    ll s = 0;
    for (int i = next2[x]; i; i = e[i].next)
    {
        dp(e[i].to);
        s += f[e[i].to];
    }
    if (s < f[x] && s && !t[x])
        f[x] = s;
    next2[x] = 0;
}

bool cmp(int x,int y)
{
    return dfn[x] < dfn[y];
}

int stack[N];
int n;

void solve()
{
    int k = read();
    cnt = 0;
    for (int i = 1; i <= k; i++)
        a[i] = read(), t[a[i]] = 1;
    std::sort(a + 1, a + k + 1, cmp);
    int top = 0;
    stack[++top] = 1;
    for (int i = 1; i <= k; i++)
    {
        int lca = getLca(stack[top], a[i]);
        if (lca == stack[top])
        {
            if (stack[top] != a[i])
                stack[++top] = a[i];
            continue;
        }
        while (dep[stack[top - 1]] >= dep[lca])
            add2(stack[top - 1], stack[top]), top--;
        if (dep[stack[top]] > dep[lca])
            add2(lca, stack[top]), top--;
        if (lca != stack[top])
            stack[++top] = lca;
        stack[++top] = a[i];
    }
    while (top > 1)
    {
        add2(stack[top - 1], stack[top]);
        top--;
    }
    dp(1);
    for (int i = 1; i <= k; i++)
        t[a[i]] = 0;
    printf("%lld\n",f[1]);
}

int main()
{
    n = read();
    logn = log(n) / log(2);
    for (int i = 1; i < n; i++)
    {
        int x = read(), y = read(), z = read();
        add1(x,y,z);
    }
    mn[1] = 1e17;
    dfs(1);
    int m = read();
    for (int i = 1; i <= m; i++)
        solve();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值