HDU 5927 Auxiliary Set LCA 2016东北4省赛

Auxiliary Set
Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 974    Accepted Submission(s): 304


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
Hint




For the query {12, 3}:
•node 4, 5, 6 are important nodes For the query {5}:
•node 12, 3, 4, 6 are important nodes
•node 5 is the lea of node 4 and node 3 For the query {3, 14}:
• node 2, 5, 6 are important nodes 




Source
2016CCPC东北地区大学生程序设计竞赛 - 重现赛

题意大致就是 给一个XXX集合 集合中的点满足以下其中之一
①:这个点是重要的点
②:这个点是2个不同的重要点的LCA
给q次查询 每次查询有m个数 代表不重要的m个点 问XX集合有几个点

数据规模看起来比较魔性 至今不知如何算复杂度
其实只要dfs一遍 得到每个点的父亲
然后 对每个不重要的点进行检查(dfs)
检测在不在集合中(是否有>=2个子树存在重要的点)就行

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

inline void read(int&x){
    x=0;
    char t;
    while(isdigit(t=getchar()))
        x=x*10+t-'0';
}

const int N=100000+5;
const int M=100000+5;

struct Edge{
    int to,next;
};

int head[N];
Edge edge[2*N];
int father[N];

int unimportV[N];//不重要的点

inline void addEdge(int k,int u,int v){
    edge[k]={v,head[u]};
    head[u]=k;
}

bool unimport[N];//是否不重要
int ans;

void getFatherInfo(int u,int fa){//找爸爸
    father[u]=fa;
    for(int i=head[u];i!=-1;i=edge[i].next)
        if(edge[i].to!=fa)
            getFatherInfo(edge[i].to,u);
}

bool check(int u){//is import?
    if(!unimport[u])
        return true;
    int counter=0;//有几颗子树有重要点
    for(int i=head[u];i!=-1;i=edge[i].next){
        if(edge[i].to==father[u])
            continue;
        counter+=check(edge[i].to);
        if(counter>=2){
            unimport[u]=false;//有超过2棵子树有重要点 这个点等价于一个重要点
            ++ans;
            return true;
        }
    }
    return counter>0;
}

int main()
{
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    int T,n,q;
    read(T);
    for(int t=1;t<=T;++t){
        read(n);
        read(q);
        fill(head,head+n+1,-1);//init
        for(int i=0,u,v;i<n-1;++i){//build tree
            read(u);
            read(v);
            addEdge(2*i,u,v);
            addEdge(2*i+1,v,u);
        }
        //get the father info
        getFatherInfo(1,-1);
        printf("Case #%d:\n",t);
        for(int i=0,m;i<q;++i){
            read(m);
            for(int j=0;j<m;++j){
                read(unimportV[j]);
                unimport[unimportV[j]]=true;
            }
            ans=n-m;
            for(int j=0;j<m;++j){
                check(unimportV[j]);
            }
            for(int j=0;j<m;++j)
                unimport[unimportV[j]]=false;
            printf("%d\n",ans);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值