传送门


$\sum k \leq 100000$虚树套路题

设$f_{i,0/1}$表示处理完$i$以及其所在子树的问题,且处理完后$i$所在子树内是否存在$1$个关键点满足它到$i$的路径上不存在任何点被封的最小代价。

转移考虑$i$是否是关键点以及是否封$i$号点。

注意判断相邻两个点同时是关键点的情况。同时需要注意:如果虚树上两个点不相邻,可以封这两个点之间的点。

 #include<bits/stdc++.h>
 //This code is written by Itst
 using namespace std;

 inline int read(){
     ;
     ;
     char c = getchar();
     while(c != EOF && !isdigit(c)){
         if(c == '-')
             f = ;
         c = getchar();
     }
     while(c != EOF && isdigit(c)){
         a = (a << ) + (a << ) + (c ^ ');
         c = getchar();
     }
     return f ? -a : a;
 }

 ;
 struct Edge{
     int end , upEd;
 }Ed[MAXN << ] , newEd[MAXN];
 ] , newHead[MAXN] , s[MAXN] , dfn[MAXN] , num[MAXN] , dp[MAXN][] , dep[MAXN];
 int N , headS , cntEd , cntNewEd , ts , cnt;
 bool imp[MAXN];

 inline void addEd(Edge* Ed , int* head , int& cntEd , int a , int b){
     Ed[++cntEd].end = b;
     Ed[cntEd].upEd = head[a];
     head[a] = cntEd;
 }

 void init(int now , int fa){
     dfn[now] = ++ts;
     dep[now] = dep[fa] + ;
     jump[now][] = fa;
      ; jump[now][i - ] ; ++i)
         jump[now][i] = jump[jump[now][i - ]][i - ];
     for(int i = head[now] ; i ; i = Ed[i].upEd)
         if(Ed[i].end != fa)
             init(Ed[i].end , now);
 }

 inline int jumpToLCA(int x , int y){
     if(dep[x] < dep[y])
         swap(x , y);
      ; i >=  ; --i)
          << i) >= dep[y])
             x = jump[x][i];
     if(x == y)
         return x;
      ; i >=  ; --i)
         if(jump[x][i] != jump[y][i]){
             x = jump[x][i];
             y = jump[y][i];
         }
     ];
 }

 inline void create(){
     imp[] = ;
     dp[][] = dp[][] = ;
      ; i <= cnt ; ++i){
         imp[num[i]] = ;
         dp[num[i]][] = ;
         dp[num[i]][] = N + ;
     }
      ; i <= cnt ; ++i)
         if(!headS)
             s[++headS] = num[i];
         else{
             int t = jumpToLCA(s[headS] , num[i]);
             if(t != s[headS]){
                 ]] > dep[t]){
                     addEd(newEd , newHead , cntNewEd , s[headS - ] , s[headS]);
                     --headS;
                 }
                 addEd(newEd , newHead , cntNewEd , t , s[headS]);
                 if(s[--headS] != t)
                     s[++headS] = t;
             }
             s[++headS] = num[i];
         }
     ){
         addEd(newEd , newHead , cntNewEd , s[headS - ] , s[headS]);
         --headS;
     }
     )
         addEd(newEd , newHead , cntNewEd ,  , s[headS]);
     --headS;
 }

 void dfs(int now){
     ;
     for(int i = newHead[now] ; i ; i = newEd[i].upEd){
         int t = newEd[i].end;
         dfs(t);
         if(imp[now])
             ] == now)
                 dp[now][] += dp[t][];
             else
                 dp[now][] += min(dp[t][] , dp[t][] + );
         else{
             ] == now)
                 dp[now][] = min(sum + dp[t][] , dp[now][] + dp[t][]);
             else
                 dp[now][] = min(sum + dp[t][] , dp[now][] + min(dp[t][] , dp[t][] + ));
             ] == now)
                 sum += dp[t][];
             else
                 sum += min(dp[t][] , dp[t][] + );
         }
         ] > N)
             dp[now][] = N + ;
         )
             sum = N + ;
     }
     sum = ;
     for(int i = newHead[now] ; i ; i = newEd[i].upEd){
         int t = newEd[i].end;
         if(!imp[now]){
             sum += min(dp[t][] , dp[t][]);
             dp[now][] += dp[t][];
         }
         dp[t][] = dp[t][] = imp[t] = ;
     }
     if(!imp[now])
         dp[now][] = min(dp[now][] , sum + );
     newHead[now] = ;
 }

 bool cmp(int a , int b){
     return dfn[a] < dfn[b];
 }

 int main(){
 #ifndef ONLINE_JUDGE
     freopen("613D.in" , "r" , stdin);
     //freopen("613D.out" , "w" , stdout);
 #endif
     N = read();
      ; i < N ; ++i){
         int a = read() , b = read();
         addEd(Ed , head , cntEd , a , b);
         addEd(Ed , head , cntEd , b , a);
     }
     init( , );
     for(int M = read() ; M ; --M){
         cnt = read();
          ; i <= cnt ; ++i)
             num[i] = read();
         sort(num +  , num + cnt +  , cmp);
         create();
         dfs();
         ][] , dp[][]);
         printf( ? - : t);
     }
     ;
 }

CF613D Kingdom and its Cities 虚树的更多相关文章

  1. CF613D Kingdom and its Cities 虚树 树形dp 贪心

    LINK:Kingdom and its Cities 发现是一个树上关键点问题 所以考虑虚树刚好也有标志\(\sum k\leq 100000\)即关键点总数的限制. 首先当k==1时 答案显然为0 ...

  2. CF613D Kingdom and its Cities 虚树 + 树形DP

    Code: #include<bits/stdc++.h> #define ll long long #define maxn 300003 #define RG register usi ...

  3. 【CF613D】Kingdom and its Cities 虚树+树形DP

    [CF613D]Kingdom and its Cities 题意:给你一棵树,每次询问给出k个关键点,问做多干掉多少个非关键点才能使得所有关键点两两不连通. $n,\sum k\le 10^5$ 题 ...

  4. CF613D:Kingdom and its Cities(树形DP,虚树)

    Description 一个王国有n座城市,城市之间由n-1条道路相连,形成一个树结构,国王决定将一些城市设为重要城市. 这个国家有的时候会遭受外敌入侵,重要城市由于加强了防护,一定不会被占领.而非重 ...

  5. CF613D Kingdom and its Cities(虚树+贪心)

    很休闲的一个题啊 其实一看到关于\(\sum k\)的限制,就知道是个虚树的题了 首先我们把虚树建出来,然后考虑怎么计算个数呢? 我们令\(f[x]\)表示以\(x\)的子树中,剩余了多少个还没有切断 ...

  6. [CF613D]Kingdom and its Cities

    description 题面 data range \[n, q,\sum k\le 10^5\] solution 还是虚树的练手题 \(f[0/1][u]\)表示\(u\)的子树内,\(u\)是否 ...

  7. 【CF613D】Kingdom and its Cities(虚树,动态规划)

    [CF613D]Kingdom and its Cities(虚树,动态规划) 题面 洛谷 CF 翻译洛谷上有啦 题解 每次构建虚树,首先特判无解,也就是关键点中存在父子关系. 考虑\(dp\),设\ ...

  8. CodeForces - 613D:Kingdom and its Cities(虚树+DP)

    Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in or ...

  9. Codeforces Round #613 Div.1 D.Kingdom and its Cities 贪心+虚树

    题目链接:http://codeforces.com/contest/613/problem/D 题意概述: 给出一棵树,每次询问一些点,计算最少删除几个点可以让询问的点两两不连通,无解输出-1.保证 ...

随机推荐

  1. JSONArray.toJSONString json乱码

    前提:配置文件已经配置了: <mvc:annotation-driven> <!-- 处理请求返回json字符串的中文乱码问题 --> <mvc:message-conv ...

  2. python 类函数,实例函数,静态函数

    一.实现方法 class Function(object): # 在类定义中定义变量 cls_variable = "class varibale" def __init__(se ...

  3. KVM虚拟化研究-1

    使用qemu-img创建镜像 例子: [root@HOST31 rybtest]# qemu-img create -f raw /rybtest/test1.raw 1G 使用qemu-img查看镜 ...

  4. (网页)javascript小技巧(非常全)

    事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture();  event.srcE ...

  5. 对word2vec的理解及资料整理

    对word2vec的理解及资料整理 无他,在网上看到好多对word2vec的介绍,当然也有写的比较认真的,但是自己学习过程中还是看了好多才明白,这里按照自己整理梳理一下资料,形成提纲以便学习. 介绍较 ...

  6. 消除TortoiseSVN 检出到(checkout)桌面上显示一堆问号

    之前不小心直接将版本库的内容检出到桌面,后才发现桌面上的文件图标都变成了问号,新建文件夹也同样如此. 为了解决这个问题,采用如下方法(任何一个检出文件夹均可这样操作): 1.删除桌面隐藏的.SVN文件 ...

  7. SOAP REST

    SOAP是基于RPC原理,是传统程序的函数调用和返回在RPC中被请求和应答代替了而已. SOAP Simple Object Access Protocol,是一种严格定义的信息交换协议,用于在web ...

  8. npm install node-sass 本地安装失败

    $ npm install --save node-sass --registry=https://registry.npm.taobao.org --disturl=https://npm.taob ...

  9. CRM 各种类型字段的 赋值 取值

    //lookupRecordEntity["new_lead"] = new EntityReference(entity.LogicalName, entity.Id); 获取时 ...

  10. tkinter内嵌Matplotlib系列(一)之解读官网教材

    目录 目录 前言 (一)小目标 1.首页卷面: 2.绘制一条函数曲线: 3.绘制多条曲线: (二)官方教材 1.对GUI框架的支持: 2.内嵌于tkinter的说明文档: (三)对官方教程的解读 目录 ...