bzoj 2286 [Sdoi2011]消耗战 虚树

倍增,然后直接上虚树。

#include <bits/stdc++.h>
using namespace std;
#define N 260000
#define ll long long
#define inf 1e9
int n,Q,m,cnt,top,tot;
int head[N],nex[N<<1],to[N<<1],val[N<<1];
int pos[N],a[N],st[N],deep[N],bel[N],tp[N];
ll f[N];
int fa[N][21],mn[N][21];
vector<int>vec[N],v[N];
void add(int x,int y,int v)
{
    tot++;
    nex[tot]=head[x];head[x]=tot;
    to[tot]=y;val[tot]=v;
}
void dfs(int x,int y)
{
    pos[x]=++cnt;
    deep[x]=deep[y]+1;
    for(int i=1;i<=20;i++)
    {
        fa[x][i]=fa[fa[x][i-1]][i-1];
        mn[x][i]=min(mn[x][i-1],mn[fa[x][i-1]][i-1]);
    }
    for(int i=head[x];i;i=nex[i])
        if(to[i]!=y)
        {
            fa[to[i]][0]=x;
            mn[to[i]][0]=val[i];
            dfs(to[i],x);
        }
}
int cmp(int x,int y){return pos[x]<pos[y];}
int lca(int x,int y)
{
    if(deep[x]<deep[y])swap(x,y);
    for(int i=20;i>=0;i--)
        if(deep[fa[x][i]]>=deep[y])
            x=fa[x][i];
    if(x==y)return x;
    for(int i=20;i>=0;i--)
        if(fa[x][i]!=fa[y][i])
            x=fa[x][i],y=fa[y][i];
    return fa[x][0];
}
int cal(int x,int y)
{
    int ret=inf;
    for(int i=20;i>=0;i--)
        if(deep[fa[x][i]]>=deep[y])
            ret=min(ret,mn[x][i]),x=fa[x][i];
    return ret;
}
void dfs(int x)
{
    f[x]=0;
    for(int i=0,t;i<vec[x].size();i++)
    {
        dfs(t=vec[x][i]);
        f[x]+=min(f[t],(ll)v[x][i]);
    }
    if(tp[x])f[x]=inf;
}
void cl(int x)
{
    vec[x].clear();
    v[x].clear();tp[x]=0;
}
int main()
{
    //freopen("tt.in","r",stdin);
    scanf("%d",&n);
    for(int i=1,x,y,v;i<n;i++)
    {
        scanf("%d%d%d",&x,&y,&v);
        add(x,y,v);add(y,x,v);
    }
    dfs(1,0);
    scanf("%d",&Q);
    for(;Q--;)
    {
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
            scanf("%d",&a[i]);
        sort(a+1,a+1+m,cmp);
        st[top=1]=1;bel[1]=(cnt=1);cl(1);
        for(int i=1;i<=m;i++)
        {
            int t;
            while((t=lca(a[i],st[top]))!=st[top])
            {
                int t1=st[top-1],t2=st[top];
                top--;
                if(deep[t]>deep[st[top]])
                {
                    st[++top]=t,bel[t]=++cnt;
                    t1=t;cl(cnt);
                }
                vec[bel[t1]].push_back(bel[t2]);
                v[bel[t1]].push_back(cal(t2,t1));
            }
            if(a[i]!=st[top])
            {
                st[++top]=a[i];
                bel[a[i]]=++cnt;cl(cnt);
            }
        }
        for(int i=top;i>=2;i--)
        {
            int t1=bel[st[i-1]],t2=bel[st[i]];
            vec[t1].push_back(t2);
            v[t1].push_back(cal(st[i],st[i-1]));
        }
        for(int i=1;i<=m;i++)tp[bel[a[i]]]=1;
        dfs(1);
        printf("%lld\n",f[1]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值