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.

Input

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.

Output

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.

Examples
Input
4
1 3
2 3
4 3
4
2 1 2
3 2 3 4
3 1 2 4
4 1 2 3 4
Output
1
-1
1
-1
Input
7
1 2
2 3
3 4
1 5
5 6
5 7
1
4 2 4 6 7
Output
2
Note

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的更多相关文章

  1. 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 ...

  2. CF613D Kingdom and its Cities 虚树 树形dp 贪心

    LINK:Kingdom and its Cities 发现是一个树上关键点问题 所以考虑虚树刚好也有标志\(\sum k\leq 100000\)即关键点总数的限制. 首先当k==1时 答案显然为0 ...

  3. 【CF613D】Kingdom and its Cities 虚树+树形DP

    [CF613D]Kingdom and its Cities 题意:给你一棵树,每次询问给出k个关键点,问做多干掉多少个非关键点才能使得所有关键点两两不连通. $n,\sum k\le 10^5$ 题 ...

  4. 【CF613D】Kingdom and its Cities

    [CF613D]Kingdom and its Cities 题面 洛谷 题解 看到关键点当然是建虚树啦. 设\(f[x]\)表示以\(x\)为根的子树的答案,\(g[x]\)表示以\(x\)为根的子 ...

  5. 【CF613D】Kingdom and its Cities(虚树,动态规划)

    [CF613D]Kingdom and its Cities(虚树,动态规划) 题面 洛谷 CF 翻译洛谷上有啦 题解 每次构建虚树,首先特判无解,也就是关键点中存在父子关系. 考虑\(dp\),设\ ...

  6. Codeforces Round #613 Div.1 D.Kingdom and its Cities 贪心+虚树

    题目链接:http://codeforces.com/contest/613/problem/D 题意概述: 给出一棵树,每次询问一些点,计算最少删除几个点可以让询问的点两两不连通,无解输出-1.保证 ...

  7. Codeforces 450D:Jzzhu and Cities(最短路,dijkstra)

    D. Jzzhu and Cities time limit per test: 2 seconds memory limit per test: 256 megabytes input: stand ...

  8. CodeForces - 115E:Linear Kingdom Races (DP+线段树+lazy)

    pro: 从左到有有N个车道,都有一定程度损坏,所以有不同的修理费a[]: 有M场比赛,每场比赛的场地是[Li,Ri],即如果这个区间的车道都被修理好,则可以举办这个比赛,并且收益是Pi.问最多得到多 ...

  9. Educational Codeforces Round 12 A. Buses Between Cities 水题

    A. Buses Between Cities 题目连接: http://www.codeforces.com/contest/665/problem/A Description Buses run ...

随机推荐

  1. jquery选择器空格与大于号、加号与波浪号的区别

    空格:$('parent childchild')表示获取parent下的所有的childchild节点,所有的子孙. 大于号:$('parent > child')表示获取parent下的所有 ...

  2. ubuntu-12.10-server中打开终端的方式

    ubuntu-12.10-server系统在图形界面的任务栏上找不到终端的踪影,可以使用以下两种方式调出 1.在图形界面中点击Dash Home 点击后搜索terminal即可 2.可以通过快捷键CT ...

  3. SpringMVC源码分析系列

    说到java的mvc框架,struts2和springmvc想必大家都知道,struts2的设计基本上完全脱离了Servlet容器,而springmvc是依托着Servlet容器元素来设计的,同时sp ...

  4. mSites and Smarty

    目前的页面实现方式是需要向后台请求接口,返回 JSON 数据,动态拼接字符串塞进 DOM 中(innerHTML).考虑用 Smarty 生成静态页面,可以在后台用 PHP 得到数据,字符串拼接,然后 ...

  5. 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!

    今年微软Build 2016大会最让开发人员兴奋的消息之一,就是在Windows上可以原生运行Linux bash,对开发人员来说,这是一个喜闻乐见的消息. 1 安装 你必须安装开发者预览版本,才能使 ...

  6. ASP.NET MVC 多语言实现技巧 最简、最易维护和最快速开发

    说说传统做法的缺点 1.做过多语言的都知道这玩意儿太花时间 2.多语言架构一般使用资源文件.XML或者存储数据库来实现.这样就在一定程序上降低了性能 3.页面的可读性变差,需要和资源文件进行来回切换 ...

  7. 深入理解OOP(第一天):多态和继承(初期绑定和编译时多态)

    在本系列中,我们以CodeProject上比较火的OOP系列博客为主,进行OOP深入浅出展现. 无论作为软件设计的高手.或者菜鸟,对于架构设计而言,均需要多次重构.取舍,以有利于整个软件项目的健康构建 ...

  8. (404) 未找到 获取StatusCode状态码

    异常代码: (HttpWebResponse)req.GetResponse(); 当执行这段代码出现异常 解决问题 那如果我们想获得错误发生时候服务器段错误页面的源代码该如何做呢? 其实非常非常简单 ...

  9. bloom filter

    Bloom filter 是由 Howard Bloom 在 1970 年提出的二进制向量数据结构,它具有很好的空间和时间效率,被用来检测一个元素是不是集合中的一个成员. 结    构 二进制 召回率 ...

  10. android开发------初识Activity

    之前我们简单说过,Activity实际上是一个窗体,用来存放我们的程序外观. 我们先来创建一个空的Activity,不加载任何layout.要做的是,定义自己的类,继承android的Activity ...