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 ...
随机推荐
- UOJ #150 【NOIP2015】 运输计划
题目描述 公元 \(2044\) 年,人类进入了宇宙纪元. \(L\) 国有 \(n\) 个星球,还有 \(n-1\) 条双向航道,每条航道建立在两个星球之间,这 \(n-1\) 条航道连通了 \(L ...
- json解析性能比较(gson与jackson) (zz)
现在json的第三方解析工作很多,如json-lib,gson,jackson,fastjson等等.在我们完成一般的json-object转换工作时,几乎都没有任何问题.但是当数据的量上来时,他们的 ...
- Windbg调优Kafka.Client内存泄露
从来没写过Blog,想想也是,工作十多年了,搞过N多的架构.技术,不与大家分享实在是可惜了.另外,从传统地ERP行业转到互联网,也遇到了很所前所未有的问题,原来知道有一些坑,但是不知道坑太多太深.借着 ...
- Web性能优化-合并js与css,减少请求
Web性能优化已经是老生常谈的话题了, 不过笔者也一直没放在心上,主要的原因还是项目的用户量以及页面中的js,css文件就那几个,感觉没什么优化的.人总要进步的嘛,最近在被angularjs吸引着,也 ...
- WebApiTestClient自定义返回值说明
WebApiTestClient是基于微软HelpPage一个客户端调试扩展工具,用来做接口调试比较方便.但是对返回值的自定义说明还是有缺陷的.有园友写过一篇文章,说可以通过对类进行注释,然后通过在I ...
- 备忘:powerbroker运行一个命令
pbrun su<space>-<space><taget user name> example: pbrun su - pmsdev
- 个人觉得目前 最好用的Taobao API的NodeJS封装
话说,Top API SDK默认只给了四种语言的SDK,没有我大NodeJS,这可怎么行,于是封装了一个. 参考地址 GitHub: https://github.com/xiaoppp/TopAPI ...
- 网页设计:Meta标签详解
很多人忽视了HTML标签META的强大功效,一个好的META标签设计可以大大提高你的个人网站被搜索到的可能性,有兴趣吗,谁我来重新认识一下META标签吧! META标签是HTML语言HEAD区的一个辅 ...
- 关于图像文章垂直无缝连接滚动——JS实现
<!-- 作者:chenyehuacecil@163.com 时间:2015-02-04 描述:实现整篇文章从下到上的无缝连接滚动--><html xmlns="http: ...
- 1-mkdir 命令总结
mkdir make directories 创建目录 [语法]: ls [选项] [参数] [功能介绍] mkdir命令用来创建目录.该命令创建由dirname命名的目录.如果在目录名的前面没有加任 ...