hdu 5927 树形dp

本文介绍了一种针对树结构的算法,该算法能够高效地处理关于重要节点及其最低公共祖先(LCA)的查询。具体而言,在给定一棵树和一系列节点集合的情况下,能够快速计算出既是重要节点又是不同重要节点间LCA的节点数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题意:

给一棵树,然后每次询问时给出哪些点是不重要的点,让求这棵树上重要的点以及是两个不同的重要的点的lca一共有多少个;

思路:

每次给出不重要的点,剩下的就是重要的点了,然后求不重要的点中有多少个是两个重要的点的lca,可以把不重要的点按深度从大到小排序;
然后一开始初始化的时候我们保存了每个节点的儿子节点数目,把这个信息复制到这些点中,然后把这些点按排好的顺序更新它的父节点的信息;
如果一个点是不重要的点,且它的儿子节点中有>=2的个数有重要节点,那么这个点就是符合要求的了;

#include <bits/stdc++.h>
using namespace std ;
const int N = 1e5+100;

vector<int> v[N];

int dp[N];
int dep[N];
int ans;
int cc[N];
int fa[N];
int cnt[N];
int du[N];
int vis[N];
struct node
{
    int x,d;
}no[N];
int son[N];


void dfs(int x,int y,int dd){
    fa[x]=y;
    dep[x]=dd;
    vis[x]=1;
    for(int i=0;i<v[x].size();i++)
    {
        if(vis[v[x][i]]==1) continue;

        cnt[x]++;
        dfs(v[x][i],x,dd+1);

    }

}

int cmp(node a,node b)
{
    return a.d>b.d;
}


int main()
{
        int n,m;
        int tt;
        scanf("%d",&tt);
        int kk=1;
        while(tt--)
        {

        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++)
        {
            vis[i]=0;
            cnt[i]=0;
            v[i].clear();
        }
        for(int i=1;i<n;i++)
        {
            int u,vv;
            scanf("%d%d",&u,&vv);
            v[u].push_back(vv);
            v[vv].push_back(u);
        }

        dfs(1,0,1);
        printf("Case #%d:\n",kk++);
        while(m--)
        {
            int t;
            scanf("%d",&t);
            ans=n-t;
            for(int j=0;j<t;j++)
            {
                int x;
                scanf("%d",&x);
                no[j].x=x,no[j].d=dep[x];
                son[no[j].x]=cnt[no[j].x];
            }
            sort(no,no+t,cmp);
            for(int i=0;i<t;i++)
            {
                if(son[no[i].x]>=2)
                {
                    ans++;
                }
                else if(son[no[i].x]==0)
                {
                    son[fa[no[i].x]]--;
                }
            }
            printf("%d\n",ans );
        }

        }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值