HDU5927 Auxiliary Set(dfs)

来源:互联网 发布:js面向对象编程案例 编辑:程序博客网 时间:2024/05/16 08:00

Auxiliary Set

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


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  orqi=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东北地区大学生程序设计竞赛 - 重现赛
题意:有一颗有根数树(根为1),n个节点给出n-1条边,q次询问,每次询问给出一个不重要点集合,有mi个,然后求一种集合的元素个数,
这个集合内包括:重要的点,两个不同的重要的点的最近公共祖先。
思路:首先,结果集合里有n-mi个重要的点,剩下的就在mi个不重要的点找是否有符合"两个不同的重要的点的最近公共祖先"这个要求的点。
先做一遍dfs,算出每个结点的深度,父结点,和孩子个数。然后每次询问把mi个不重要点按照深度从大到小排序。遍历不重要结点,如果当前结点的
儿子数目大于或等于2,那么它就符合要求,否则如果他的儿子数是0那就让它的父结点的儿子数减一(这样做可以使得如果某个结点有儿子的结点,
那么他的儿子结点里一定有重要的点或者有通过不重要结点与它的连通,注意每个结点只算跟它直接相连的儿子个数,所以如果儿子数目大于或等于2,
就肯定有2个以上的重要结点而且不在同一条边上,那当前遍历的点就是它们的最近公共祖先)。
#include <bits/stdc++.h>using namespace std;#define maxn 100010int head[maxn<<1], tot;int son[maxn], fa[maxn], cop[maxn];int dep[maxn], d[maxn];struct node{    int to, next;}G[maxn<<1];void add(int u, int v){    G[tot].to = v;    G[tot].next = head[u];    head[u] = tot++;}void dfs(int now, int pre){    fa[now] = pre, son[now] = 0, dep[now] = dep[pre]+1;    for(int i = head[now];i != -1;i = G[i].next){        int to = G[i].to;        if(to==pre) continue;        dfs(to, now);        son[now]++;    }}bool cmp(int a, int b){    return dep[a] > dep[b];}int main(){    int T, n, q, u, v, k = 1, m, i, j, ans;    scanf("%d", &T);    while(T--){        scanf("%d %d", &n, &q);        tot = 1;        memset(head, -1, sizeof head);        for(i = 0;i < n-1;i++){            scanf("%d %d", &u, &v);            add(u, v);            add(v, u);        }        printf("Case #%d:\n", k++);        dfs(1, 0);        for(i = 0;i < q;i++){            scanf("%d", &m);            for(j = 0;j < m;j++) scanf("%d", &d[j]);            sort(d, d+m, cmp);            for(j = 0;j < m;j++) cop[d[j]] = son[d[j]];            ans = n-m;            for(j = 0;j < m;j++){                if(cop[d[j]]>=2) ans++;                else if(cop[d[j]]==0) cop[fa[d[j]]]--;            }            printf("%d\n", ans);        }    }}


1 0
原创粉丝点击