F - Auxiliary Set

本文介绍了一种针对树形结构的数据查询算法,通过构建辅助集来高效处理多个查询请求。该算法首先定义了辅助集的概念,即包含树中重要节点及作为两个不同重要节点最近公共祖先的节点集合。接着详细阐述了如何处理非重要节点,通过排序和计数的方式快速确定辅助集中元素的数量。

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

F - Auxiliary Set

 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

  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 
G
     题意:  

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

思路

把不是重要的点按照深度排序,然后按照孩子的个数来判断这个点是不是能够加到集合里。

对于m个不重要节点,按深度从大到小排序,然后依次判断:对于当前点, 
如果它的儿子数量大于等于2,说明他有两个重要节点孩子,那么当前点一定是一个重要节点; 
如果儿子数量等于1,那么关系到当前点的祖先节点是不是重要节点,不处理; 
如果孩子数等于0,那么当前点肯定不是重要节点,同时其父亲节点的儿子数量减1 
只要满足孩子数大于等于2的都是Auxiliary Set元素。

代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
const int maxx=1e5+10;
using namespace std;

vector<int>s[maxx];
int fa[maxx];  //父节点
int son[maxx];  //子节点个数
int deep[maxx];  //深度
int vis[maxx];
int uu[maxx];
int temp[maxx];  //多组数据,代替son

void dfs(int u,int f,int d)
{
    vis[u]=1;
    fa[u]=f;
    deep[u]=d;  //深度
    for(int i=0,l=s[u].size();i<l;i++)  //遍历与u连接的点
    {
        int v=s[u][i];
        if(!vis[v])
        {
            son[u]++;  //点u的子节点个数
            dfs(v,u,d+1);  //遍历u的子节点v,此时v的父节点是u,深度+1
        }
    }
}


int cmp(int a,int b)
{
    return deep[a]>deep[b];  //从底看
}

int main()
{
    int T;
    scanf("%d",&T);
    int n,q;
    for(int num=1;num<=T;num++)
    {
        scanf("%d %d",&n,&q);
        for(int i=1;i<=n;i++)
        {
            s[i].clear();
            vis[i]=0;
            son[i]=0;
        }
        int u,v;
        for(int i=0;i<n-1;i++)
        {
            scanf("%d %d",&u,&v);
            s[u].push_back(v);
            s[v].push_back(u);
        }
        dfs(1,0,0);
        int m;
        printf("Case #%d:\n",num);
        while(q--)
        {
            scanf("%d",&m);
            int sum=n-m;
            for(int i=0;i<m;i++)
            {
                scanf("%d",&uu[i]);
                temp[uu[i]]=son[uu[i]];
            }
            sort(uu,uu+m,cmp);
            for(int i=0;i<m;i++)  //找两个重要节点的最近公共子节点
            {
                if(temp[uu[i]]>=2)
                    sum++;
                else if(temp[uu[i]]==0)  //只有一个子节点,一定不是重要节点
                    temp[fa[uu[i]]]--;  //该节点的父亲节点的儿子数量-1,排除父亲节点的非重要节点。

            }
            printf("%d\n",sum);
        }
    }
    return 0;
}

### 使用批处理脚本检查并设置Visual Studio 2019 Community版的安装路径及VC++ v142 x86/x64编译器组件 为了实现这一目标,可以通过编写批处理脚本来自动检测Visual Studio 2019的具体位置,并调用相应的`vcvarsall.bat`来初始化特定版本的编译工具链。 #### 批处理脚本示例 下面是一个简单的批处理脚本例子,该脚本会尝试定位Visual Studio 2019的位置并通过命令行加载所需的开发环境: ```batch @echo off setlocal enabledelayedexpansion REM 设置默认安装目录作为初始猜测 set VSPATH=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community if exist "%VSPATH%\Common7\Tools\VsDevCmd.bat" ( echo Found Visual Studio 2019 at %VSPATH% REM 初始化指定架构的编译环境 call "%VSPATH%\VC\Auxiliary\Build\vcvarsall.bat" amd64 cl /? ) else ( echo Could not find Visual Studio 2019 installation. ) endlocal pause ``` 此脚本首先定义了一个可能存在的Visual Studio 2019社区版的标准安装路径[^1]。如果找到了对应的`VsDevCmd.bat`文件,则认为已经找到正确的Visual Studio实例;接着通过调用`vcvarsall.bat`来准备针对AMD64平台(即64位Windows)的编译环境。最后展示编译器的帮助信息以验证是否成功设置了环境变量。 对于不同的体系结构(比如32位),只需更改传递给`vcvarsall.bat`参数即可,例如改为`x86`代表32位构建环境。 需要注意的是,上述脚本假设Visual Studio被安装在其标准位置。如果有多个版本共存或自定义了安装路径,那么需要更复杂的逻辑去遍历注册表或其他方式查找确切位置。 此外,当涉及到具体项目如AV1编码库(aom),则还需要确保CMake配置正确指向所使用的Visual Studio版本和目标平台[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值