Auxiliary Set
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 318 Accepted Submission(s): 93
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:
∙ 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.
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 ui,vi(1≤ui,vi≤n) indicating there is an edge between ui i and vi in the tree.
In the next q lines, the i-th line first comes with an integer mi(1≤mi≤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 ∑qi=1mi≤100000 .
It is also guaranteed that the number of test cases in which n≥1000 or ∑qi=1mi≥1000 is no more than 10.
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 ui,vi(1≤ui,vi≤n) indicating there is an edge between ui i and vi in the tree.
In the next q lines, the i-th line first comes with an integer mi(1≤mi≤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 ∑qi=1mi≤100000 .
It is also guaranteed that the number of test cases in which n≥1000 or ∑qi=1mi≥1000 is no more than 10.
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
树上的dfs,找到每个重要结点或者两个重要节点的lca,求个数,但是初始化真是服气。。。 It is also guaranteed that the number of test cases in which n≥1000 or ∑qi=1mi≥1000 is no more than 10.这句话说明不能每次更新询问时都重新初始化,大部分时候m<1000,所以我们更新是每次更新m个非重要点之后查询结束后再更新回来,会将复杂度从o(n^2)降到o(n*m);吃一鉴长一智#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int maxn=100010; int n,m,q; int im[maxn]; int cnt; int qn[maxn]; int fa[maxn]; struct Node { int to; int next; }; struct Node edges[2*maxn]; int head[maxn]; int k; void AddNode(int from, int to) { edges[k].to=to; edges[k].next=head[from]; head[from]=k++; } int dfs(int u,int p) { if(im[u]==1) return 1; else if(im[u]==-1) return 0; int res=0; for(int i=head[u]; i!=-1; i=edges[i].next) { int v=edges[i].to; if(v==p) continue; res+=dfs(v,u); if(res==2) break; } if(res==1) { im[u]=1; return 1; } if(res==2) { im[u]=1; cnt++; return 1; } im[u]=-1; return 0; } void solve(int u) { int res=0; for(int i=head[u]; i!=-1; i=edges[i].next) { int v=edges[i].to; if(v==fa[u]) continue; res+=dfs(v,u); if(res==2) { im[u]=1; cnt++; return; } } if(res==0) im[u]=-1; else if(res) im[u]=1; } void find_fa(int u,int p) { fa[u]=p; for(int i=head[u]; i!=-1; i=edges[i].next) { int v=edges[i].to; if(v==p) continue; find_fa(v,u); } } int main() { int t; scanf("%d",&t); int cns=0; while(t--) { scanf("%d%d",&n,&q); k=0; memset(head,-1,sizeof(head)); for(int i=1; i<=n; i++) im[i]=1; for(int i=0; i<n-1; i++) { int x,y; scanf("%d%d",&x,&y); AddNode(x,y); AddNode(y,x); } find_fa(1,-1); printf("Case #%d:\n",++cns); for(int i=0; i<q; i++) { scanf("%d",&m); for(int j=0; j<m; j++) { scanf("%d",&qn[j]); im[qn[j]]=0; } cnt=n-m; for(int j=0; j<m; j++) { if(im[qn[j]]==0) solve(qn[j]); } for(int j=0; j<m; j++) { im[qn[j]]=1; } cout<<cnt<<endl; } } }