Auxiliary Set
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 689 Accepted Submission(s): 209
Problem Description
Given a rooted tree with n vertices, some of the vertices are important.
An auxiliary set is a set containing vertices satisfying at least one of the two conditions:
∙
It is an important vertex
∙
It is the least common ancestor of two different important vertices.
You are given a tree with n vertices (1 is the root) and q queries.
Each query is a set of nodes which indicates the unimportant vertices in the tree. Answer the size (i.e. number of vertices) of the auxiliary set for each query.
An auxiliary set is a set containing vertices satisfying at least one of the two conditions:
∙
∙
You are given a tree with n vertices (1 is the root) and q queries.
Each query is a set of nodes which indicates the unimportant vertices in the tree. Answer the size (i.e. number of vertices) of the auxiliary set for each query.
Input
The first line contains only one integer T (
T≤1000
), which indicates the number of test cases.
For each test case, the first line contains two integers n ( 1≤n≤100000
), q (
0≤q≤100000
).
In the following n -1 lines, the i-th line contains two integers u
i
,v
i
(1≤u
i
,v
i
≤n)
indicating there is an edge between
u
i![]()
i and
v
i![]()
in the tree.
In the next q lines, the i-th line first comes with an integer m
i
(1≤m
i
≤100000)
indicating the number of vertices in the query set.Then comes with mi different integers, indicating the nodes in the query set.
It is guaranteed that ∑
q
i=1
m
i
≤100000
.
It is also guaranteed that the number of test cases in which n≥1000
or
∑
q
i=1
m
i
≥1000
is no more than 10.
For each test case, the first line contains two integers n ( 1≤n≤100000
In the following n -1 lines, the i-th line contains two integers u
In the next q lines, the i-th line first comes with an integer m
It is guaranteed that ∑
It is also guaranteed that the number of test cases in which n≥1000
Output
For each test case, first output one line "Case #x:", where x is the case number (starting from 1).
Then q lines follow, i-th line contains an integer indicating the size of the auxiliary set for each query.
Then q lines follow, i-th line contains an integer indicating the size of the auxiliary set for each query.
Sample Input
1 6 3 6 4 2 5 5 4 1 5 5 3 3 1 2 3 1 5 3 3 1 4
Sample Output
Case #1: 3 6 3HintFor the query {1,2, 3}: •node 4, 5, 6 are important nodes For the query {5}: •node 1,2, 3, 4, 6 are important nodes •node 5 is the lea of node 4 and node 3 For the query {3, 1,4}: • node 2, 5, 6 are important nodes
Source
一棵根为1的树 给出不重要的点 求这个集合的大小 这个集合包括重要的点和有两个重要的子孙的不重要的点 所以我们要做的就是判断哪些不重要的点拥有两个以上的重要子孙然后进入集合 当时做的时候 就只是按照输入顺序判断当前点的儿子个数然后改变当前点的父亲的儿子数 计算结果后再恢复原值 结果当然错了 遇到这种数据会错
1-2 2-4 2-5 1-3 3-6 3-7 不重要的点: 1 2 3 5 6 结果会错运行结果会是5但其实是3啊 如果不重要的点的顺序变为 5 6 2 3 1 结果会不一样 所以对于不重要的点按当前的深度排序在判断修改值即可 同时标记哪个点改变了 改变了多少 之后还要恢复原值的。
//当时做没想到将不重要的点进行深度排序
#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define maxn 100010
using namespace std;
int cnt,head[maxn];
int son[maxn],fa[maxn];
int rec[maxn],deep[maxn];
int mark[maxn];
struct Edge {
int u,v,next;
}edge[maxn*2];
void init(){
cnt=0;
memset(head,-1,sizeof(head));
memset(son,0,sizeof(son));
memset(fa,0,sizeof(fa));
memset(deep,0,sizeof(deep));
}
void add(int u,int v){
Edge E={u,v,head[u]};
edge[cnt]=E;
head[u]=cnt++;
}
bool cmp(int a,int b){
return deep[a] > deep[b];
}
void dfs(int u,int f){
fa[u]=f;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(v!=f){
deep[v]=deep[u]+1;
son[u]++;
dfs(v,u);
}
}
}
int main(){
int t,n,q,m,u,v,ans,cnt=0;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d",&n,&q);
for(int i=1;i<n;++i){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(1,0);
printf("Case #%d:\n",++cnt);
while(q--){
scanf("%d",&m);
ans=n-m;
int tem;
for(int i=0;i<m;++i)
scanf("%d",&rec[i]);
sort(rec,rec+m,cmp);
for(int i=0;i<m;++i){
if(son[rec[i]]>=2)
ans++;
else if(son[rec[i]]==0){
son[fa[rec[i]]]--;
mark[fa[rec[i]]]++;
}
}
for(int i=0;i<m;++i){
if(mark[fa[rec[i]]]){
son[fa[rec[i]]]+=mark[fa[rec[i]]];
mark[fa[rec[i]]]=0;
}
}
printf("%d\n",ans);
}
}
return 0;
}