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.保证 ...
随机推荐
- 慕学在线网0.2_users表设计(1)
1.Django App设计: users-用户管理(任何web中,users表都是第一个被设计的) course-课程管理 organization-机构和教师管理 operation-用户操作管理 ...
- linux 下的ssh
ssh服务 1.检查是否有被安装,命令 rpm -qa|grep "ssh" 2.检查ssh有没有在运行,命令 rpm -qa|grep "ssh" 3.如何启 ...
- MSSQL一种取代游标的方案
今天看到一篇文章写的自己整理记录下,据说比用游标快. DECLARE @字段1 数据类型; DECLARE @字段2 数据类型; DECLARE @TMP_WHILE_ID INT; ,),TMP_W ...
- 转:更改SQLServer实例默认字符集
需求 安装数据库时,将字符集安装成了“SQL_Latin1_General_CP1_CI_AS”,现在需要将其更改为“Chinese_PRC_CI_AS”. 方法 重新生成系统数据库 ,然后还原配 ...
- sleep和Sleep区别
windows Sleep 单位是毫秒 linux sleep 单位是秒
- linux E325: 注意 发现交换文件 "*.swp" 解决方法
今天在linux里修改脚本文件时,不小心非正常关闭了脚本文件,再次编辑时,每次都提示 如下错误: E325: 注意发现交换文件 ".bqh.sh.swp" 所有者: root 日期 ...
- Hive-1.2.1_05_案例操作
1. 建库建表 # 建库 create database exercise; # 建表 create table student(Sno int,Sname string,Sex string,Sag ...
- myeclipse中配置spring xml自己主动提示
版权声明: https://blog.csdn.net/zdp072/article/details/24582173 这是一篇分享技巧的文章:myeclipse中配置spring xml自己主动提示 ...
- js如何获得局部变量的值
方法一: <script> var a; //全局变量 function test(){ var b=20; //局部变量 return b; //返回局部变量的值 }; a=test ...
- js常见错误类型
(1)SyntaxError SyntaxError是解析代码时发生的语法错误 // 变量名错误 var 1a; // 缺少括号 console.log 'hello'); (2)ReferenceE ...