HDU 5927 Auxiliary Set 最近公共祖先 (dfs)

37 篇文章 0 订阅


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个节点,q次查询,每次查询告诉你m个非重要节点(其余都是重要节点),问你每次查询时Auxiliary Set元素有多少个?
满足一下两点中的一点即可为Auxiliary Set元素:
1:是重要节点
2:是两个重要节点的最近公共祖先

分析:
其实这一题就是问有多少个非重要节点可以是Auxiliary Set元素,即是两个重要节点的最近公共祖先。
首先对树进行一次dfs,求出每个节点的父亲和儿子的个数,以及其深度。
然后对于m个不重要节点,按深度从大到小排序,然后依次判断:对于当前点,
如果它的儿子数量大于等于2,说明他有两个重要节点孩子,那么当前点一定是一个重要节点;
如果儿子数量等于1,那么关系到当前点的祖先节点是不是重要节点,不处理;
如果孩子数等于0,那么当前点肯定不是重要节点,同时其父亲节点的儿子数量减1
只要满足孩子数大于等于2的都是Auxiliary Set元素。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int N = 100000 + 100;
vector<int>vec[N];
struct node
{
    int num_son;
    int pre;
    int deep;
}a[N],b[N];
int c[N];
bool cmp(int t1,int t2)
{
    return a[t1].deep>a[t2].deep;
}
void dfs(int rt,int pre,int dep)
{
    int i,j,sn;
    for(i=0;i<vec[rt].size();i++) {
        sn = vec[rt][i];
        if(sn==pre) continue;
        dfs(sn,rt,dep+1);
    }
    a[rt]={vec[rt].size()-1,pre,dep};
    if(rt==1) a[rt].num_son=vec[rt].size();
}
int main()
{
    int T,n,m,q,i,j,id;
    int u,v;
    scanf("%d",&T);
    for(int _=1;_<=T;_++) {
        scanf("%d%d",&n,&q);
        for(i=1;i<=n;i++) vec[i].clear();
        for(i=1;i<n;i++){
            scanf("%d%d",&u,&v);
            vec[u].push_back(v);
            vec[v].push_back(u);
        }
        dfs(1,-1,0);
        printf("Case #%d:\n",_);
        while(q--) {
            scanf("%d",&m);
            for(i=1;i<=m;i++) scanf("%d",&c[i]);
            sort(c+1,c+1+m,cmp);
            for(i=1;i<=m;i++) b[c[i]]=a[c[i]];
            int ans = n-m;
            for(i=1;i<=m;i++) {
                int id = c[i];
                if(b[id].num_son>1) ans++;
                else if(b[id].num_son==0 && b[id].pre!=-1) b[b[id].pre].num_son--;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值