Auxiliary Set

Auxiliary Set

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


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 ui i 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
  
  
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
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
 


题意 :查询符合条件的点的个数,条件为:它本身是 重要的点;若它不是重要的点,那他至少有两个孩子是重要的点
每次查询时,会说明n个点中那些不是重要的点,那么我们知道重要点的个数,因此我们只要这些不重要点的孩子是重要的点的个数是否大于等于2的个数

真是一道思维题

首先建树,dfs找出每个节点的父亲,孩子的个数,以及其深度,我们对不重要的点排序(根据点的深度由大到小 )
然后对不重要的点进行操作,若该结点的孩子大于等于2,则该结点符合题意,若等于0,说明它对它父亲没有贡献(即不是重要的点),它父亲的孩子的个数-1;
若该操作执行完,要进入下一次操作,有的节点父亲孩子的个数可能减少了,因此要对它进行恢复


代码如下:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>

using namespace std;

const int N=1e5+5;
struct Edge{
    int to,next;
}edge[N<<1];

int ant;
int head[N],pre[N],child[N],h[N],a[N],b[N];
void add(int st,int et)
{
    edge[ant].to=et;
    edge[ant].next=head[st];
    head[st]=ant++;
}
int cmp(int i,int j)
{
    return h[i]>h[j];
}
void dfs(int rt,int parent,int height)
{
    pre[rt]=parent;
    h[rt]=height;
    child[rt]=0;
    for(int i=head[rt];i!=-1;i=edge[i].next)
    {
        int to=edge[i].to;
        if(to!=parent)
        {
            child[rt]++;
            dfs(to,rt,height+1);
        }
    }
}
int main()
{
    int t;
    int n,q,m,cont=0;
    scanf("%d",&t);
    while(t--)
    {
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&q);
        ant=0;
        for(int i=1;i<n;i++)
        {
            int st,et;
            scanf("%d%d",&st,&et);
            add(st,et);
            add(et,st);
        }
        printf("Case #%d:\n",++cont);
        dfs(1,0,1);//rt parent height
        memset(b,0,sizeof(b));
        while(q--)
        {
            scanf("%d",&m);
            for(int i=1;i<=m;i++)
                scanf("%d",&a[i]);
            sort(a+1,a+m+1,cmp);
            int ans=0;
            for(int i=1;i<=m;i++)
            {
                if(child[a[i]]>=2)
                    ans++;
                else if(child[a[i]]==0)
                {
                    child[pre[a[i]]]--;
                    b[pre[a[i]]]++;
                }

            }
            printf("%d\n",n-m+ans);
            for(int i=1;i<=m;i++)
            {
                int p=pre[a[i]];
                if(b[p])
                {
                    child[p]+=b[p];
                    b[p]=0;
                }
            }

        }
    }
    return 0;
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值