F - Auxiliary Set

2024-02-19 09:32
文章标签 set auxiliary

本文主要是介绍F - Auxiliary Set,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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];  //多组数据,代替sonvoid 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;
}

这篇关于F - Auxiliary Set的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/724255

相关文章

poj 3050 dfs + set的妙用

题意: 给一个5x5的矩阵,求由多少个由连续6个元素组成的不一样的字符的个数。 解析: dfs + set去重搞定。 代码: #include <iostream>#include <cstdio>#include <set>#include <cstdlib>#include <algorithm>#include <cstring>#include <cm

Collection List Set Map的区别和联系

Collection List Set Map的区别和联系 这些都代表了Java中的集合,这里主要从其元素是否有序,是否可重复来进行区别记忆,以便恰当地使用,当然还存在同步方面的差异,见上一篇相关文章。 有序否 允许元素重复否 Collection 否 是 List 是 是 Set AbstractSet 否

论文翻译:ICLR-2024 PROVING TEST SET CONTAMINATION IN BLACK BOX LANGUAGE MODELS

PROVING TEST SET CONTAMINATION IN BLACK BOX LANGUAGE MODELS https://openreview.net/forum?id=KS8mIvetg2 验证测试集污染在黑盒语言模型中 文章目录 验证测试集污染在黑盒语言模型中摘要1 引言 摘要 大型语言模型是在大量互联网数据上训练的,这引发了人们的担忧和猜测,即它们可能已

多路转接之select(fd_set介绍,参数详细介绍),实现非阻塞式网络通信

目录 多路转接之select 引入 介绍 fd_set 函数原型 nfds readfds / writefds / exceptfds readfds  总结  fd_set操作接口  timeout timevalue 结构体 传入值 返回值 代码 注意点 -- 调用函数 select的参数填充  获取新连接 注意点 -- 通信时的调用函数 添加新fd到

Android set Tag, findViewWithTag使用

设置了tag为“principal”的view ImageView principal = (ImageView) findViewById(R.id.imagen_home_0);principal.setTag("principal"); 在其它地方获取,获取已经设置了tag为“principal”的view LayoutInflater inflater = LayoutInflate

C++ STL关联容器Set与集合论入门

1. 简介 Set(集合)属于关联式容器,也是STL中最实用的容器,关联式容器依据特定的排序准则,自动为其元素排序。Set集合的底层使用一颗红黑树,其属于一种非线性的数据结构,每一次插入数据都会自动进行排序,注意,不是需要排序时再排序,而是每一次插入数据的时候其都会自动进行排序。因此,Set中的元素总是顺序的。 Set的性质有:数据自动进行排序且数据唯一,是一种集合元素,允许进行数学上的集合相

Eclipse或MyEclipse中Java Working Set管理项目

随着学习JAVA的时间的越来越久,项目也越来越多,Eclipse或MyEclipse界面中显示一堆! 每次工作使用到的项目肯定不会太多...... 每次从这么大数量的工程当中找到自己要使用的, 必须大规模的滚动滚动条...... 图片一   Project Explorer中:    图片二:Package Explorer中: 这样就好找很多了,分类放!

STL set整理

#include<set>#include<cstdio>#include<iterator>#include<iostream>#include<algorithm>using namespace std;//set 集合的操作//multisetset<int>Set1;set<int>Set2;set<int>Set3;/*begin() 返回指向第一个元素的迭代器

解决PHP Warning: strftime(): It is not safe to rely on the system's timezone set

当运行一些程序时,在httpd日志中会有如下警告日志: PHP Warning:  strftime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set(

Java中集合类Set、List和Map的区别

Java中的集合包括三大类,它们是Set、List和Map,它们都处于java.util包中,Set、List和Map都是接口,它们有各自的实现类。Set的实现类主要有HashSet和TreeSet,List的实现类主要有ArrayList,Map的实现类主要有HashMap和TreeMap。那么它们有什么区别呢? Set中的对象不按特定方式排序,并且没有重复对象。但它的有些实现类能对集合中的对