CF613D Kingdom and its Cities 虚树
$\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 虚树的更多相关文章
- CF613D Kingdom and its Cities 虚树 树形dp 贪心
LINK:Kingdom and its Cities 发现是一个树上关键点问题 所以考虑虚树刚好也有标志\(\sum k\leq 100000\)即关键点总数的限制. 首先当k==1时 答案显然为0 ...
- CF613D Kingdom and its Cities 虚树 + 树形DP
Code: #include<bits/stdc++.h> #define ll long long #define maxn 300003 #define RG register usi ...
- 【CF613D】Kingdom and its Cities 虚树+树形DP
[CF613D]Kingdom and its Cities 题意:给你一棵树,每次询问给出k个关键点,问做多干掉多少个非关键点才能使得所有关键点两两不连通. $n,\sum k\le 10^5$ 题 ...
- CF613D:Kingdom and its Cities(树形DP,虚树)
Description 一个王国有n座城市,城市之间由n-1条道路相连,形成一个树结构,国王决定将一些城市设为重要城市. 这个国家有的时候会遭受外敌入侵,重要城市由于加强了防护,一定不会被占领.而非重 ...
- CF613D Kingdom and its Cities(虚树+贪心)
很休闲的一个题啊 其实一看到关于\(\sum k\)的限制,就知道是个虚树的题了 首先我们把虚树建出来,然后考虑怎么计算个数呢? 我们令\(f[x]\)表示以\(x\)的子树中,剩余了多少个还没有切断 ...
- [CF613D]Kingdom and its Cities
description 题面 data range \[n, q,\sum k\le 10^5\] solution 还是虚树的练手题 \(f[0/1][u]\)表示\(u\)的子树内,\(u\)是否 ...
- 【CF613D】Kingdom and its Cities(虚树,动态规划)
[CF613D]Kingdom and its Cities(虚树,动态规划) 题面 洛谷 CF 翻译洛谷上有啦 题解 每次构建虚树,首先特判无解,也就是关键点中存在父子关系. 考虑\(dp\),设\ ...
- 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 ...
- Codeforces Round #613 Div.1 D.Kingdom and its Cities 贪心+虚树
题目链接:http://codeforces.com/contest/613/problem/D 题意概述: 给出一棵树,每次询问一些点,计算最少删除几个点可以让询问的点两两不连通,无解输出-1.保证 ...
随机推荐
- 转载--tomcat调优
转载 https://www.cnblogs.com/wangsen/p/9337969.html
- Android解析XML文件
XML文件和获取XML值 XML文件样例 <?xml version="1.0" encoding="utf-8"?> <citys> ...
- [20170615]执行dbms_sqldiag.dump_trace看执行计划.txt
[20170615]执行dbms_sqldiag.dump_trace看执行计划.txt --//上午在想查看10053执行计划时使用包时出现如下提示: SCOTT@book> @ &r ...
- AspNetCore2 Hangfire定时任务
Hangfire 是一个简单的用于.net及.net core 应用程序,通过数据库持久化,定时执行后台任务的组件 1.通过NuGet安装Hangfire 2.在Startup.cs文件的Config ...
- Spring Boot 使用 ServletFileUpload上传文件失败,upload.parseRequest(request)为空
使用Apache Commons FileUpload组件上传文件时总是返回null,调试发现ServletFileUpload对象为空,在Spring Boot中有默认的文件上传组件,在使用Serv ...
- vi 复制或剪切多行超级强大方法
同一个文件:光标移到起始行,输入ma 光标移到结束行,输入mb 光标移到粘贴行,输入mc 然后 :'a, 'b co 'c 把 co 改成 m 就成剪切了多个文件:在文件一: 光标移到起始行,输入ma ...
- 第五章 绘图基础(ALTWIND)
线上箭头表示画线的方向.WINDING模式和ALTERNATE模式都会填充三个封闭的L型区域,号码从1到3.两个更小的内部区域,号码为4和5,在ALTERNATE模式下不被填充.但是在WINDING模 ...
- js,ajax,layer笔记(弹出层,在弹出一个弹框)
整体认识: 因为作用域的问题,js 在页面初次加载时已近加载好了,所以要有第二次弹窗的效果,必须得在第一次成功之后再次让他加载js 代码: /*shaun*/showdetailsPag: funct ...
- Ceph基础知识和基础架构认识
1 Ceph基础介绍 Ceph是一个可靠地.自动重均衡.自动恢复的分布式存储系统,根据场景划分可以将Ceph分为三大块,分别是对象存储.块设备存储和文件系统服务.在虚拟化领域里,比较常用到的是Cep ...
- PostgreSQL 空间处理函数
PostGIS中的常用函数 以下内容包括比较多的尖括号,发布到blogger的时候会显示不正常,内容太多我也无暇一个个手动改代码,因此如有问题就去参考PostGIS官方文档. 首先需要说明一下,这里许 ...