zoukankan      html  css  js  c++  java
  • bzoj2286 (sdoi2011)消耗战(虚树)

     [Sdoi2011]消耗战

    Time Limit: 20 Sec  Memory Limit: 512 MB
    Submit: 4052  Solved: 1463
    [Submit][Status][Discuss]

    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

    Source

     
    题解:这道题十分惭愧,毕竟不是我自己打的,写了一次代码还写挂了,但是十分欣赏这个数据结构,
    怎么办呢,于是直接贴了hzw代码,还是先理解为主吧,(⊙o⊙)…
    hzw的代码比较精简,我看了看,也理解了一番,直接写在代码上了,

    这是hzw的题解,难以理解,十分正常。

    我还觉得这篇题解写的不错

    那么就需要用到虚树的技巧了,虚树就是通过维护一个单调栈把树的关键点和它们之间的lca按照dfs序遍历一遍,遍历的过程中通过单调栈的调整来理清树的父亲和儿子之间的关系。

    首先,对树节点进行dfs。在期间对节点进行标号dfn。

    然后,维护一个单调栈。这个单调栈的节点都在一条链上。

    对于栈顶元素 p,栈次顶元素 q, 即将插入节点x 有如下关系:

    1.lca是p.此时dfn(x)>dfn(p)=dfn(lca)

    这说明 x在p的下面,直接把x入栈即可

    2.p和x分立在lca的两棵子树下.此时 dfn(x)>dfn(p)>dfn(ilca)

    这时候就有三种讨论了

    针对这道题的连边就是树型dp处理

    (1)如果dfn(q)>dfn(lca),可以直接连边q->p,然后退一次栈.


    (2)如果dfn(q)=dfn(lca),说明q=lca,直接连边lca->p,把p退栈,此时子树已经构建完毕.

    (3)如果dfn(q)<dfn(lca),说明lca被p与q夹在中间,此时连边lca->q,把p退栈,再把lca压入栈.此时子树构建完毕.


      1 #include<iostream>
      2 #include<set>
      3 #include<map>
      4 #include<cstdio>
      5 #include<cstring>
      6 #include<cstdlib>
      7 #include<ctime>
      8 #include<vector>
      9 #include<queue>
     10 #include<algorithm>
     11 #include<cmath>
     12 #include<bitset>
     13 #include<stack>
     14 #define inf 1e60
     15 #define pa pair<int,int>
     16 #define ll long long 
     17 using namespace std;
     18 int read()
     19 {
     20     int x=0,f=1;char ch=getchar();
     21     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
     22     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
     23     return x*f;
     24 }
     25 int bin[20];
     26 int n,m,cnt,ind,top;
     27 int last[250005],last2[250005],fa[250005][20];
     28 ll mn[250005],f[250005];
     29 int h[250005],mark[250005],deep[250005];
     30 int st[250005];
     31 struct edge{
     32     int to,next,v;
     33 }e[500005],ed[500005];
     34 void insert(int u,int v,int w)//普通增边吧 
     35 {
     36     e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;e[cnt].v=w;
     37     e[++cnt].to=u;e[cnt].next=last[v];last[v]=cnt;e[cnt].v=w;
     38 }
     39 void insert2(int u,int v)//这是虚树增边 
     40 {
     41     if(u==v)return;
     42     ed[++cnt].to=v;ed[cnt].next=last2[u];last2[u]=cnt;
     43 }
     44 bool cmp(int a,int b)
     45 {
     46     return mark[a]<mark[b];
     47 }
     48 void pre(int x)//预处理深度,以及父亲 
     49 {
     50     mark[x]=++ind;
     51     for(int i=1;bin[i]<=deep[x];i++)
     52         fa[x][i]=fa[fa[x][i-1]][i-1];
     53     for(int i=last[x];i;i=e[i].next)
     54         if(e[i].to!=fa[x][0])
     55         {
     56             mn[e[i].to]=min(mn[x],(ll)e[i].v);//到路径的最小值。 
     57             deep[e[i].to]=deep[x]+1;
     58             fa[e[i].to][0]=x;
     59             pre(e[i].to);
     60         }
     61 }
     62 int lca(int x,int y)//倍增求lca 
     63 {
     64     if(deep[x]<deep[y])swap(x,y);
     65     int t=deep[x]-deep[y];
     66     for(int i=0;bin[i]<=t;i++)
     67         if(t&bin[i])x=fa[x][i];
     68     for(int i=19;i>=0;i--)
     69         if(fa[x][i]!=fa[y][i])
     70             x=fa[x][i],y=fa[y][i];
     71     if(x==y)return x;
     72     return fa[x][0];
     73 }
     74 void dp(int x)
     75 {
     76     f[x]=mn[x];
     77     ll tmp=0;
     78     for(int i=last2[x];i;i=ed[i].next)
     79     {
     80         dp(ed[i].to);
     81         tmp+=f[ed[i].to];
     82     }
     83     last2[x]=0;
     84     if(tmp==0)f[x]=mn[x];
     85     else if(tmp<=f[x])f[x]=tmp;
     86 }
     87 void solve()
     88 {
     89     cnt=0;
     90     int K=read();
     91     for(int i=1;i<=K;i++)
     92         h[i]=read();
     93     sort(h+1,h+K+1,cmp);//输入是无序的 
     94     int tot=0;
     95     h[++tot]=h[1];
     96     for(int i=2;i<=K;i++)
     97         if(lca(h[tot],h[i])!=h[tot]) h[++tot]=h[i];
     98     st[++top]=1;//根节点是总根 
     99     for(int i=1;i<=tot;i++)
    100     {
    101         int now=h[i],f=lca(now,st[top]);
    102         while(1)
    103         {
    104             if(deep[f]>=deep[st[top-1]])
    105             {
    106                 insert2(f,st[top--]);
    107                 if(st[top]!=f)st[++top]=f;
    108                 break;
    109             }
    110             insert2(st[top-1],st[top]);top--;
    111         }
    112         if(st[top]!=now)st[++top]=now;
    113     }
    114     while(--top)insert2(st[top],st[top+1]);
    115     dp(1);
    116     printf("%lld
    ",f[1]);
    117 }
    118 int main()
    119 {
    120     bin[0]=1;for(int i=1;i<20;i++)bin[i]=bin[i-1]<<1;
    121     n=read();
    122     for(int i=1;i<n;i++)
    123     {
    124         int u=read(),v=read(),w=read();
    125         insert(u,v,w);
    126     }
    127     mn[1]=inf;pre(1);
    128     m=read();
    129     for(int i=1;i<=m;i++)
    130         solve();
    131     return 0;
    132 }

    重复判断上述过程

  • 相关阅读:
    预处理器&预处理变量&头文件保护&条件编译
    Xctf攻防世界—crypto—Normal_RSA
    RSA共模攻击
    centos7安装宝塔面板
    cobalt strike出现连接超时情况解决办法
    C语言变量
    Hello World!
    ctfshow—web—web7
    ctfshow—web—web6
    ctfshow—web—web5
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/7637850.html
Copyright © 2011-2022 走看看