Auxiliary Set HDU - 5927(思维题)

本文介绍了一种针对树形结构的数据查询算法,该算法能够高效地处理一系列关于树中重要节点及其辅助集大小的查询请求。具体而言,对于一棵包含特定重要节点的树,算法需回答每个查询中非重要节点集合所构成的辅助集的规模。

摘要生成于 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≤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
这题是一个思维题,被唬住了,也没怎么仔细想,现在想来也的确是这么一回事。当时过的人多,的确应该好好思考,也不是不可能想出来。

代码;

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#define maxx 100005
using namespace std;
int fa[maxx],d[maxx];
vector<int> tree[maxx];
int n,q;
int a[maxx];
int son[maxx];
void dfs(int u,int last,int deep)
{
    d[u]=deep;
    fa[u]=last;
    for(int i=0;i<tree[u].size();i++)
    {
        int v=tree[u][i];
        if(v==last)
            continue;
        son[u]++;
        dfs(v,u,deep+1);
    }
}
void init(int x)
{
    for(int i=1;i<=x;i++)
        tree[i].clear();
    memset(son,0,sizeof(son));
}
bool cmp(int x,int y)
{
    return d[x]>d[y];
}
int _size[maxx];
int main()
{
    int t;
    cin>>t;
    int cal=1;
    while(t--)
    {
        scanf("%d%d",&n,&q);
        int x,y;
        init(n);
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            tree[x].push_back(y);
            tree[y].push_back(x);
        }
        dfs(1,0,1);
        printf("Case #%d:\n",cal++);
        while(q--)
        {
            int num;
            scanf("%d",&num);
            for(int i=0;i<num;i++)
                scanf("%d",a+i);
            for(int i=0;i<num;i++)
                _size[a[i]]=son[a[i]];
            sort(a,a+num,cmp);
            int ans=n-num;
            for(int i=0;i<num;i++)
            {
                if(_size[a[i]]>=2)
                    ans++;
                else
                    if(!_size[a[i]])
                        _size[fa[a[i]]]--;
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值