传送门


$\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. [VUE ERROR] Invalid default value for prop "slides": Props with type Object/Array must use a factory function to return the default value

    错误原因: 当给子组件设置 props 属性时,如果参数类型是 Array 或 Object ,它的默认值必须是由工场函数返回,不能直接赋值 错误代码: proE: { type: Array, de ...

  2. openCV 色彩空间

    ---恢复内容开始--- 1.使用cv2.inrange()获取某个范围内的图像取值,指定某个通道的最小值和最大值 import numpy as np def color_space(image): ...

  3. Python3.5中安装Scrapy包时出现问题

    在Python3.5中安装Scrapy第三方库 pip install Scrapy 安装到后面出现的这类错误: error: Microsoft Visual C++ 14.0 is require ...

  4. pip安装django的时候提示没有这个命令

    问题描述: 在安装pyenv安装完python的时候,用pip安装django提示没有这个命令 [root@zabbix ~]# pip install django== 2.0 pyenv: pip ...

  5. Navicat连接Oracle 报 ORA-12737 set CHS16GBK错误

            4,680   今天看到0day5上面更新了一个用友ERP的漏洞,确实可以下载任意文件:但是用友ERP基本上都是使用了oracle数据库,必须要有一个好的数据库连接工具才可以,Navi ...

  6. 使用CefSharp的一些需要注意的点

    程序关闭的时候一定要加上: CefSharp.Cef.Shutdown(); 因为关闭的时候,不将浏览器关闭的话,会阻塞主线程导致报错. 修复CefSharp浏览器组件中文输入的bug // brow ...

  7. read_csv报错Initializing from file failed

    Python版本:Python 3.6 pandas.read_csv() 报错 OSError: Initializing from file failed,一般由两种情况引起:一种是函数参数为路径 ...

  8. UVA12171-Sculpture(离散化+floodfill)

    Problem UVA12171-Sculpture Accept: 196  Submit: 1152 Time Limit: 3000 mSec Problem Description Imagi ...

  9. eclipse 自动创建web.xml

    eclipse 自动创建web.xml 以前每次创建web项目的时候,总是从其他的项目中拷贝一个web.xml文件到新项目,然后改吧改吧就可以了,但是这终究是一件麻烦的事,要是能够通过软件自动创建,那 ...

  10. linux 的常用命令---------第十一阶段

    软件管理rpm.yum 在 windows 与 linux 之间 实现小文件传输(仅支持在 X shell 中完成文件传输,虚拟机中不可实现): # yum install  lrzsz  -y    ...