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.保证 ...
随机推荐
- css选择器:基本选择器
基本选择器 1.通用元素选择器 *表示应用到所有的标签. *{ padding:0px; margin:0px; } 2.元素/标签选择器 匹配所有p标签的元素 p{ color:red; backg ...
- Android 开发学习笔记
1.Genymotion 解决虚拟镜像下载速度特别慢的问题 http://blog.csdn.net/qing666888/article/details/51622762 2.
- [20171107]dbms_shared_pool.pin.txt
[20171107]dbms_shared_pool.pin.txt --//昨天与别人聊天提到,如果dbms_shared_pool.pin对象,可以改变对应的chunk的类型.我自己也不确定,做一 ...
- Pyhon环境变量的一些坑
在正常的情况下,使用编译器执行Python文件,无需考虑环境变量的改变 例:sum --one --one1.py --two --two1.py 在执行one.py文件需要调用 two.py中某个方 ...
- MailKit帮助类
public class EmailHelp { /// <summary> /// Smtp服务器地址 /// </summary> private static reado ...
- 视频截图Util
VideoToPicUtil.java package com.zhwy.util; import java.io.File; import java.util.ArrayList; impor ...
- 10 种机器学习算法的要点(附 Python 和 R 代码)
本文由 伯乐在线 - Agatha 翻译,唐尤华 校稿.未经许可,禁止转载!英文出处:SUNIL RAY.欢迎加入翻译组. 前言 谷歌董事长施密特曾说过:虽然谷歌的无人驾驶汽车和机器人受到了许多媒体关 ...
- Hibernate 中的 idclass mapping 问题
关于出现 idclass mapping 运行错误 @IdClass 注释通常用于定义包含复合键id的Class.即多个属性的关键复合. @IdClass(CountrylanguageEntityP ...
- 关于无限试用JetBrains产品的方案
JetBrains免费试用期限为30天,通过对其试用机制的设想,找到了其破解试用机制的方案,具体如下: 在选择试用JetBrains产品的时候,它会在 C:\Users\用户名\对应产品\config ...
- awk实战演示
awk:报告生成器,格式化文本输出. 我们一般使用的awk命令其实就是gawk,在centos7系统下,awk是gawk的链接文件. 基本用法:gawk [options] 'program' FI ...