codeforces 613D:Kingdom and its Cities
Description
Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in order not to lose face in front of the relatives, the King should first finish reforms in his kingdom. As the King can not wait for his daughter's marriage, reforms must be finished as soon as possible.
The kingdom currently consists of n cities. Cities are connected by n - 1 bidirectional road, such that one can get from any city to any other city. As the King had to save a lot, there is only one path between any two cities.
What is the point of the reform? The key ministries of the state should be relocated to distinct cities (we call such cities important). However, due to the fact that there is a high risk of an attack by barbarians it must be done carefully. The King has made several plans, each of which is described by a set of important cities, and now wonders what is the best plan.
Barbarians can capture some of the cities that are not important (the important ones will have enough protection for sure), after that the captured city becomes impassable. In particular, an interesting feature of the plan is the minimum number of cities that the barbarians need to capture in order to make all the important cities isolated, that is, from all important cities it would be impossible to reach any other important city.
Help the King to calculate this characteristic for each of his plan.
The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cities in the kingdom.
Each of the next n - 1 lines contains two distinct integers ui, vi (1 ≤ ui, vi ≤ n) — the indices of the cities connected by the i-th road. It is guaranteed that you can get from any city to any other one moving only along the existing roads.
The next line contains a single integer q (1 ≤ q ≤ 100 000) — the number of King's plans.
Each of the next q lines looks as follows: first goes number ki — the number of important cities in the King's plan, (1 ≤ ki ≤ n), then follow exactly ki space-separated pairwise distinct numbers from 1 to n — the numbers of important cities in this plan.
The sum of all ki's does't exceed 100 000.
For each plan print a single integer — the minimum number of cities that the barbarians need to capture, or print - 1 if all the barbarians' attempts to isolate important cities will not be effective.
4
1 3
2 3
4 3
4
2 1 2
3 2 3 4
3 1 2 4
4 1 2 3 4
1
-1
1
-1
7
1 2
2 3
3 4
1 5
5 6
5 7
1
4 2 4 6 7
2
In the first sample, in the first and the third King's plan barbarians can capture the city 3, and that will be enough. In the second and the fourth plans all their attempts will not be effective.
In the second sample the cities to capture are 3 and 5.
正解:虚树+DP
解题报告:
上次考试原题...
题目要求使得k个点互相不连通的最小代价。
考虑直接构虚树,然后贪心。具体看我的代码,不再赘述。关键是虚树注意有一些细节不要写错了。
//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
#define RG register
const int MAXN = ;
int n,m,ecnt,id[MAXN],k;
int first[MAXN],next[MAXN*],to[MAXN*];
int jump[MAXN][],deep[MAXN];
int que[MAXN],top,Stack[MAXN];
int head[MAXN];
int ans;
bool in[MAXN];
struct edge{
int to,next;
}e[MAXN];
inline bool cmp(RG int a,RG int b){return id[a]<id[b];}
inline void link(RG int x,RG int y){ if(x==y) return ; e[++ecnt].next=head[x]; head[x]=ecnt; e[ecnt].to=y;}
inline int getint()
{
RG int w=,q=; RG char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} inline void dfs(RG int x,RG int fa){
jump[x][]=fa; id[x]=++ecnt;
for(int i=;i<=;i++) jump[x][i]=jump[jump[x][i-]][i-];
for(int i=first[x];i;i=next[i]) {
RG int v=to[i]; if(v==fa) continue;
deep[v]=deep[x]+; dfs(v,x);
}
} inline int lca(RG int x,RG int y){
if(deep[x]<deep[y]) swap(x,y);
RG int t=; while((<<t) <= deep[x]) t++;
t--; for(int i=t;i>=;i--) if(deep[x]-(<<i)>=deep[y]) x=jump[x][i];
if(x==y) return y;
for(RG int i=t;i>=;i--) if(jump[x][i]!=jump[y][i]) { x=jump[x][i]; y=jump[y][i]; }
return jump[x][];
} inline int dp(RG int x,RG int fa){
RG int cap=,lin; RG bool flag=false,ff=true;
if(in[x]){//自己是的话,必须把所有有指挥官的儿子结点的路都堵上
cap++;
for(RG int i=head[x];i;i=e[i].next) {
RG int v=e[i].to; if(v==fa) continue;
lin=dp(v,x); cap+=lin;
if(lin>) ans++;
}
}
else{//自己不是的话,如果只有一个儿子结点则不用管,如果超过一个则需要把当前结点摧毁,并且等价于与上面部分断开了
for(RG int i=head[x];i;i=e[i].next) {
RG int v=e[i].to; if(v==fa) continue;
lin=dp(v,x); cap+=lin;
if(lin!= && flag && ff) ans++,ff=false;
if(lin>)flag=true;//!!!!!!只有在儿子结点有未处理的情况下才需要打标记!!!!!!
}
if(!ff) cap=;
}
head[x]=;
return cap;
} inline bool solve(){
m=getint(); for(RG int i=;i<=m;i++) que[i]=getint(),in[que[i]]=;
for(RG int i=;i<=m;i++) for(RG int j=first[que[i]];j;j=next[j]) if(in[to[j]]) return false;
sort(que+,que+m+,cmp);//按dfs序排序
top=;Stack[++top]=; RG int grand;
ecnt=;
for(RG int i=;i<=m;i++) {
grand=lca(Stack[top],que[i]);
while() {
if(deep[Stack[top-]]<=deep[grand]) {
link(grand,Stack[top]); top--;
if(Stack[top]!=grand) Stack[++top]=grand;
break;
}
link(Stack[top-],Stack[top]); top--;
}
if(Stack[top]!=que[i]) Stack[++top]=que[i];
}
top--;
while(top) link(Stack[top],Stack[top+]),top--;
ans=; dp(,);
printf("%d\n",ans);
return true;
} inline void work(){
n=getint(); RG int x,y;
for(RG int i=;i<n;i++) {
x=getint(); y=getint();
next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y;
next[++ecnt]=first[y]; first[y]=ecnt; to[ecnt]=x;
}
ecnt=; deep[]=; dfs(,);
k=getint(); for(RG int i=;i<=k;i++) { if(!solve()) printf("-1\n"); for(int j=;j<=m;j++) in[que[j]]=; }
} int main()
{
work();
return ;
}
codeforces 613D:Kingdom and its Cities的更多相关文章
- 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 ...
- CF613D Kingdom and its Cities 虚树 树形dp 贪心
LINK:Kingdom and its Cities 发现是一个树上关键点问题 所以考虑虚树刚好也有标志\(\sum k\leq 100000\)即关键点总数的限制. 首先当k==1时 答案显然为0 ...
- 【CF613D】Kingdom and its Cities 虚树+树形DP
[CF613D]Kingdom and its Cities 题意:给你一棵树,每次询问给出k个关键点,问做多干掉多少个非关键点才能使得所有关键点两两不连通. $n,\sum k\le 10^5$ 题 ...
- 【CF613D】Kingdom and its Cities
[CF613D]Kingdom and its Cities 题面 洛谷 题解 看到关键点当然是建虚树啦. 设\(f[x]\)表示以\(x\)为根的子树的答案,\(g[x]\)表示以\(x\)为根的子 ...
- 【CF613D】Kingdom and its Cities(虚树,动态规划)
[CF613D]Kingdom and its Cities(虚树,动态规划) 题面 洛谷 CF 翻译洛谷上有啦 题解 每次构建虚树,首先特判无解,也就是关键点中存在父子关系. 考虑\(dp\),设\ ...
- Codeforces Round #613 Div.1 D.Kingdom and its Cities 贪心+虚树
题目链接:http://codeforces.com/contest/613/problem/D 题意概述: 给出一棵树,每次询问一些点,计算最少删除几个点可以让询问的点两两不连通,无解输出-1.保证 ...
- Codeforces 450D:Jzzhu and Cities(最短路,dijkstra)
D. Jzzhu and Cities time limit per test: 2 seconds memory limit per test: 256 megabytes input: stand ...
- CodeForces - 115E:Linear Kingdom Races (DP+线段树+lazy)
pro: 从左到有有N个车道,都有一定程度损坏,所以有不同的修理费a[]: 有M场比赛,每场比赛的场地是[Li,Ri],即如果这个区间的车道都被修理好,则可以举办这个比赛,并且收益是Pi.问最多得到多 ...
- Educational Codeforces Round 12 A. Buses Between Cities 水题
A. Buses Between Cities 题目连接: http://www.codeforces.com/contest/665/problem/A Description Buses run ...
随机推荐
- Java设计模式之-----策略模式
首先,我们来看下策略模式的概念.一般的解释如下: 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变化.(原文:The St ...
- TinyFrame再续篇:整合Spring AOP实现日志拦截
上一篇中主要讲解了如何使用Spring IOC实现依赖注入的.但是操作的时候,有个很明显的问题没有解决,就是日志记录问题.如果手动添加,上百个上千个操作,每个操作都要写一遍WriteLog方法,工作量 ...
- centos7下使用yum安装mysql数据库以及设置远程访问
CentOS7的yum源中默认好像是没有mysql的.为了解决这个问题,我们要先下载mysql的repo源. 1. 下载mysql的repo源 $ wget http://repo.mysql.com ...
- MVC5 + EF6 + Bootstrap3 (15) 应用ModelState和Data Annotation做服务器端数据验证
Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-server-side-validation.html 系列 ...
- Webwork 学习之路【05】请求跳转前 xwork.xml 的读取
个人理解 WebWork 与 Struts2 都是将xml配置文件作为 Controler 跳转的基本依据,WebWork 跳转 Action 前 xml 文件的读取依赖 xwork-1.0.jar, ...
- web 前端常用组件【02】Select 下拉框
<select id="hello"></select> 关于 select 支持的属性和响应事件,可以参照:http://www.runoob.com ...
- 安装.NET Framework后程序无法启动的错误处理
最近发现一直在使用的Database.NET软件无法正常使用了,表现为当尝试进行Sql Server的连接创建时,直接报错 在事件查看器具体错误信息为: 日志名称: Applicat ...
- 史密斯(smith)圆图讲解
不管多么经典的射频教程,为什么都做成黑白的呢?让想理解史密斯原图的同学一脸懵逼. 这是什么东东? 今天解答三个问题: 1.是什么? 2.为什么? 3.干什么? 1.是什么? 该图表是由菲利普·史密斯( ...
- word2vec使用说明补充(google工具包)
[本文转自http://ir.dlut.edu.cn/NewsShow.aspx?ID=253,感谢原作者] word2vec是一个将单词转换成向量形式的工具.可以把对文本内容的处理简化为向量空间中的 ...
- CMD命令简单使用
1.定位磁盘 2.打开文件路径 3.查看文件目录里面所有的文件或目录信息