即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

2016CCPC东北地区大学生程序设计竞赛 – Auxiliary Set(思维题)

编程语言 qq_33183401 52℃ 0评论

Auxiliary Set

Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)


Total Submission(s): 1536 Accepted Submission(s): 463

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 (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.

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


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


3


题意:给出一个n个节点n-1条边的树。每次询问给出一个集合,集合内的点为不重要点,集合外的点为重要点。如果不重要点是两个重要点的lca则转变成重要点。输出重要点的个数。


题解:先预处理出每个点的父亲,儿子个数和深度.对于集合中的不要重要点,按深度降序排序。则这个点的儿子数量如果超过两个,就可以变成重要点。如果儿子数量为0,则该节点父亲节点不为根的情况下,将父亲节点的儿子数-1.因为这是”没用的“儿子。


代码:

#include
#include
#include
#include
#include
#include
using namespace std;
const int N = 100000 + 100;
int t,n,m,cnt;
int head[N<<2];
int u,v,x;
int c[N];
int read(){
    int x=0;char ch = getchar();
    while('0'>ch||ch>'9')ch=getchar();
    while('0'<=ch&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x;
}
struct node
{
    int to,next;
}edge[N<<2];
struct nod
{
    int son,pre,dep;
}a[N],b[N];
void add(int u,int v)
{
    edge[cnt].to=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
void dfs(int u,int pre,int dep)
{
    int ans=0,v;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        v=edge[i].to;
        if(v==pre)continue;
        ans++;
        dfs(v,u,dep+1);
    }
    a[u].son=ans;
    a[u].pre=pre;
    a[u].dep=dep;
}
bool  cmp (int x,int y )
{
    return a[x].dep>a[y].dep;
}
int main() 
{     t=read();
    for(int k=1;k<=t;k++)
    {
        init();
        n=read(),m=read();
        for(int i=1;i1,-1,0);
        printf("Case #%d:\n",k);
        while(m--)
        {
            x=read();
            for(int i=1;i<=x;i++)
            {
                c[i]=read();
            }
                sort(c+1,c+1+x,cmp);
                for(int i=1;i<=x;i++)
                b[c[i]]=a[c[i]];
                int ans=n-x;
                for(int i=1;i<=x;i++)
                {
                    int id=c[i];
                    if(b[id].son>1) ans++;
                    else if(b[id].son==0&&b[id].pre!=-1)
                    b[b[id].pre].son--;
                }
                printf("%d\n",ans);
        }
    }
}

转载请注明:CodingBlog » 2016CCPC东北地区大学生程序设计竞赛 – Auxiliary Set(思维题)

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情