HDU - 5927Auxiliary Set (树dfs+LCA)

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≤1000T≤1000), which indicates the number of test cases. 

For each test case, the first line contains two integers n (1≤n≤1000001≤n≤100000), q (0≤q≤1000000≤q≤100000). 

In the following n -1 lines, the i-th line contains two integers ui,vi(1≤ui,vi≤n)ui,vi(1≤ui,vi≤n)indicating there is an edge between uiuii and vivi in the tree. 

In the next q lines, the i-th line first comes with an integer mi(1≤mi≤100000)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∑i=1qmi≤100000. 

It is also guaranteed that the number of test cases in which n≥1000n≥1000  or ∑qi=1mi≥1000∑i=1qmi≥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

        
  

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 

题意:给出一棵树和不重要的点,求出   1:是重要的点 2:是两个重要点的最近公共祖先,符合1或2 任意一条的点的个数。

思路:先DFS把树求出来,再把深度,子节点的个数求出来,在每一次查询的时候,首先把重要的点计算上,然后对于每一个不重要的点,只要它的子树中含有重要的点的个数大于等于两个就可以算上,那么我们可以把不重要的点按深度排序,从最深的点开始判断,当一个结点的子树>=2 ,ans++;当节点的子树的个数==1,对ans没有影响;但是当节点的子树的个数==0时,当前节点的父节点要减掉这个子节点。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<string>
#include<map>
#include<queue>
#include<vector>
#define ll long long
using namespace std;
//const double pi = acos(-1);
const ll inf=1e18;
const ll mod=1e9+7;
const int maxn=1e5+10;
vector<int>v1[2*maxn];
int vis[maxn],f[maxn],a[maxn],son[maxn],son1[maxn],deep[maxn];
void dfs(int x,int z){
    deep[x]=z;
    for(int i=0; i<v1[x].size(); i++){
        int y=v1[x][i];
        if(vis[y]==0){
            f[y]=x;
            son[x]++;
            vis[y]=1;
            dfs(y,z+1);
        }
    }
}
bool cmp(int x,int y){
    return deep[x]>deep[y];
}
int main(){
    int t;
    scanf("%d",&t);
    int k=0;
    while(t--){
        k++;
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=0; i<=maxn; i++){
            f[i]=-1;
            vis[i]=0;
            v1[i].clear();
            son[i]=0;
            son1[i]=0;
            deep[i]=0;
        }
        for(int i=0; i<n-1; i++){
            int x,y;
            scanf("%d%d",&x,&y);
            v1[x].push_back(y);
            v1[y].push_back(x);
        }
        vis[1]=1;
        dfs(1,0);
        printf("Case #%d:\n",k);
        for(int i=0; i<m; i++){
            int ans=0,num;
            scanf("%d",&num);
            ans+=(n-num);
            for(int j=0; j<num; j++){
                scanf("%d",&a[j]);
                son1[a[j]]=son[a[j]];
            }
            sort(a,a+num,cmp);
            for(int j=0;j<num;j++){
                if(son1[a[j]]>=2)ans++;
                else if(son1[a[j]]==1)continue;
                else
                    son1[f[a[j]]]--;
            }
            printf("%d\n",ans);
        }
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值