Auxiliary Set HDU - 5927(树形dp+思维)

11 篇文章 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: 

$\bullet $It is an important vertex 
$\bullet $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 \leq 1000$), which indicates the number of test cases. 

For each test case, the first line contains two integers n ($1 \leq n \leq 100000$), q ($0 \leq q \leq 100000$). 

In the following n -1 lines, the i-th line contains two integers $u_i,v_i (1\leq u_i,v_i \leq n)$ indicating there is an edge between $u_i$i and $v_i$ in the tree. 

In the next q lines, the i-th line first comes with an integer $m_i (1 \leq m_i \leq 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 $\sum^q_{i=1} m_i \leq 100000$. 

It is also guaranteed that the number of test cases in which $n \geq 1000$  or $\sum_{i=1}^{q} m_i \geq 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 

题目思路:
如果只考虑第一个条件的话无疑是道傻逼题,直接n-cnt就行了,加了第二个条件就有点麻烦了,通过分析不难发现我们只用对于每一次查询去考虑每一个不重要的点是否为两个重要点的lca就行了,通过分析我们很容易知道一个不重要的点的子树中如果有两个以上的子树有重要的点那么它就是符合条件的,我们很容易想到,对于每一个不重要的点去跑一边dfs,实力亲测会超时,那么我们怎么去优化呢,有一个重要的突破口,只有不重要和重要的两种点,一个不重要节点,如果它的合法子树个数大于二就肯定是满足的,这样的话,我们会想到从最深的不重要节点开始去判断,如果他不是合法的那么它父亲子树个数减一。然后依次往上判断就行了。
ac代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<iostream>
#include<cmath>
#include<vector>
#define eps 1e-6
using namespace std;
const int maxn = 1e5+10;
int T;
int n,m;
int head[maxn];
int tot;
int vis[maxn];
int par[maxn];
int rak[maxn];
int son[maxn];
int vis2[maxn];
int tmp[maxn];
int tmpcnt[maxn];
struct node
{
    int u,v;
    int net;
}E[maxn*2+5];
void __init__()
{
    memset(head,-1,sizeof(head));
    tot = 0;
    memset(vis,0,sizeof(vis));
    memset(son,0,sizeof(son));
    memset(tmpcnt,0,sizeof(tmpcnt));
}
int cmp(int a,int b)
{
    return rak[a]>rak[b];
}
void build(int u,int v)
{
    E[tot].u = u;
    E[tot].v = v;
    E[tot].net = head[u];
    head[u] = tot++;
}
void dfs(int u,int fa,int deep)
{
    vis[u] = 1;
    par[u] = fa;
    rak[u] = deep;
  //  cout<<u<<endl;
    for(int i = head[u];i!=-1;i= E[i].net){
        int to = E[i].v;
        if(to==fa)
            continue;
        if(vis[to])
            continue;
        son[u]++;
        dfs(to,u,deep+1);
    }
}
int main()
{
    scanf("%d",&T);
    int kase = 1;
    while(T--)
    {
        scanf("%d%d",&n,&m);
        __init__();
        for(int i = 0;i<n-1;i++){
            int a,b;
            scanf("%d%d",&a,&b);
            build(a,b);
            build(b,a);
        }
        dfs(1,-1,0);
        int sum;
        printf("Case #%d:\n",kase++);
        for(int j = 1;j<=m;j++)
        {
            int cnt = 0;
            scanf("%d",&sum);
            for(int i = 0;i<sum;i++){
                scanf("%d",&tmp[i]);
                cnt++;
            }
            sort(tmp,tmp+sum,cmp);
            int cntcnt = 0;
            for(int i = 0;i<sum;i++){
                  if(son[tmp[i]]==0){
                        son[par[tmp[i]]]--;
                        tmpcnt[par[tmp[i]]]++;
                  }
                  if(son[tmp[i]]>=2)
                  {
                        cntcnt++;
                  }
                }
            for(int i = 0;i<sum;i++){
                son[par[tmp[i]]] += tmpcnt[par[tmp[i]]];
                tmpcnt[par[tmp[i]]] = 0;
            }
            //cout<<cntcnt<<endl;
            printf("%d\n",n-cnt+cntcnt);
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值