HDU5927-Auxiliary Set

来源:互联网 发布:免费棋牌app源码 编辑:程序博客网 时间:2024/05/29 17:39

Auxiliary Set

                                                                        Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                                   Total Submission(s): 1539    Accepted Submission(s): 464


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.
 

Input
The first line contains only one integer T (T1000), which indicates the number of test cases.

For each test case, the first line contains two integers n (1n100000), q (0q100000).

In the following n -1 lines, the i-th line contains two integers ui,vi(1ui,vin) 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(1mi100000) 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=1mi100000.

It is also guaranteed that the number of test cases in which n1000  or qi=1mi1000 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. 
 

Sample Input
16 36 42 55 41 55 33 1 2 31 53 3 1 4
 

Sample Output
Case #1:363
Hint
For 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
2016CCPC东北地区大学生程序设计竞赛 - 重现赛
 

Recommend
wange2014
 

题意:给一棵有根树,有q次询问,每次给出m个“不重要”的点,但如果一个点是两个重要的点的最近公共祖先,它会变成重要的点,问有多少个重要点

解题思路:先求出所有点在树中的层数,每次对给出的m个不重要点根据它们在树中的层数从大到小排序,然后对每个不重要点看和它相连并且层数大于它的点,若这些点的下面存在重要点,那么这个点标记为1,若存在两个以上这样的点,则答案加一


#include <stdio.h>#include <stdlib.h>#include <string.h>#include <string>#include <math.h>#include <time.h>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <map>#include <set>using namespace std;const int INF = 0x3f3f3f3f;#define LL long longint s[100009], nt[200009], e[200009],n,m,ans,level[100009];bool visit[100009];struct node{int id, level;}a[100009];bool cmp(node a, node b){return a.level > b.level;}void dfs(int x,int y){for (int i = s[x];~i;i = nt[i]){int ee = e[i];if (level[ee]) continue;level[ee] = y + 1;dfs(ee, y + 1);}}void f(int x){int cnt = 0;for (int i = s[x];~i;i = nt[i]){int ee = e[i];if (level[ee] < level[x]) continue;if (visit[ee]) cnt++;if (cnt >= 2) { ans++; break; }}if (cnt >= 1) visit[x] = 1;}int main(){int t,cas=0,q;scanf("%d", &t);while (t--){printf("Case #%d:\n", ++cas);scanf("%d%d", &n, &q);int cnt = 1;memset(s, -1, sizeof s);memset(level, 0, sizeof level);for (int i = 1;i < n;i++){int u, v;scanf("%d%d", &u, &v);nt[cnt] = s[u], e[cnt] = v, s[u] = cnt++;nt[cnt] = s[v], e[cnt] = u, s[v] = cnt++;}level[1] = 1;dfs(1,1);while (q--){scanf("%d", &m);memset(visit, true, sizeof visit);for (int i = 1;i <= m;i++)scanf("%d", &a[i].id), visit[a[i].id] = 0,a[i].level=level[a[i].id];sort(a + 1, a + 1 + m, cmp);ans = 0;for (int i = 1;i <=m;i++) f(a[i].id);printf("%d\n",ans+(n-m));}}return 0;}