Auxiliary Set
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1120 Accepted Submission(s): 349
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 uii 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 uii 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
题意:
有一棵n个节点的有根树(1为树根),先定义重要节点x如下:1.x是重要节点。2.x的两个重要节点的lca。每次询问给出m个不重要节点(意味着其余点都是重要节点),问当前树中有重要节点的个数。
思路:
本题其实是求不重要节点中有多少节点可以变成重要节点。首先对树进行一次dfs,求出每个节点的父亲和儿子的个数,以及其深度,然后对于m个不重要节点,按深度从大到小排序,然后依次判断:对于当前点,如果它的儿子数量大于等于2,那么当前点一定是一个重要节点,如果儿子数量等于1,那么关系到当前点的祖先节点是不是重要节点,不处理,如果等于0,那么当前点肯定不是重要节点,同时其父亲节点的儿子数量减1
参考:http://blog.youkuaiyun.com/discreeter/article/details/52749361#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int maxn = 110000; struct node { int to,next; }s[maxn*2]; int cnt,head[maxn],dep[maxn],pa[maxn],vis[maxn],son[maxn]; int n,m,t; int x[maxn],tmp[maxn]; void init() { cnt = 0; for(int i = 1; i <= n; i++) { head[i] = -1; son[i] = 0,vis[i] = 0; } } void add(int u, int v) { s[cnt].to = v; s[cnt].next = head[u]; head[u] = cnt++; } void dfs(int u, int fa, int d) { vis[u] = 1; pa[u] = fa; dep[u] = d; for(int i = head[u]; i != -1; i = s[i].next) { int v = s[i].to; if(!vis[v]) { dfs(v,u,d+1);son[u]++; } } } int cmp(int a,int b) { return dep[a] > dep[b]; } int main() { int u,v; int cas=1; scanf("%d", &t); while(t--) { scanf("%d%d",&n, &m); init(); for(int i = 1; i < n;i++) { scanf("%d%d", &u, &v); add(u,v),add(v,u); } dfs(1,-1,1); printf("Case #%d:\n",cas++); while(m--) { int k; scanf("%d",&k); for(int i = 0; i < k; i++) { scanf("%d",&x[i]);tmp[x[i]]=son[x[i]]; } sort(x,x+k,cmp); int sum = n - k; for(int i = 0; i < k; i++) { if(tmp[x[i]] >= 2) sum++; else if(tmp[x[i]] == 0) tmp[pa[x[i]]]--; } printf("%d\n",sum); } } }