牛客多校Ancestor(lca,集合的lca)

题目描述

NIO is playing a game about trees.

The game has two trees A,BA, BA,B each with NNN vertices. The vertices in each tree are numbered from 111 to NNN and the iii-th vertex has the weight viv_ivi​. The root of each tree is vertex 1. Given KKK key numbers x1,…,xkx_1,\dots,x_kx1​,…,xk​, find the number of solutions that remove exactly one number so that the weight of the lowest common ancestor of the vertices in A with the remaining key numbers is greater than the weight of the lowest common ancestor of the vertices in B with the remaining key numbers.

输入描述:

The first line has two positive integers N,K(2≤K≤N≤105)N,K (2 \leq K \leq N \leq 10^5)N,K(2≤K≤N≤105).The second line has KKK unique positive integers x1,…,xK(xi≤N)x_1,\dots,x_K (x_i \leq N)x1​,…,xK​(xi​≤N).The third line has NNN positive integers ai(ai≤109)a_i (a_i \leq 10^9)ai​(ai​≤109) represents the weight of vertices in A.The fourth line has N−1N - 1N−1 positive integers {pai}\{pa_i\}{pai​}, indicating that the number of the father of vertices i+1i+1i+1 in tree A is paipa_ipai​.The fifth line has nnn positive integers bi(bi≤109)b_i (b_i \leq 10^9)bi​(bi​≤109) represents the weight of vertices in B.The sixth line has N−1N - 1N−1 positive integers {pbi}\{pb_i\}{pbi​}, indicating that the number of the father of vertices i+1i+1i+1 in tree B is pbipb_ipbi​.

输出描述:

One integer indicating the answer.

示例1

输入

5 3
5 4 3
6 6 3 4 6
1 2 2 4
7 4 5 7 7
1 1 3 2

输出

1

说明

In first case, the key numbers are 5,4,3. 
Remove key number 5, the lowest common ancestors of the vertices in A with the remaining key numbers is 2, in B is 3.
Remove key number 4, the lowest common ancestors of the vertices in A with the remaining key numbers is 2, in B is 1.
Remove key number 3, the lowest common ancestors of the vertices in A with the remaining key numbers is 4, in B is 1.
Only remove key number 5 satisfies the requirement.

示例2

输入

10 3
10 9 8
8 9 9 2 7 9 0 0 7 4
1 1 2 4 3 4 2 4 7
7 7 2 3 4 5 6 1 5 3
1 1 3 1 2 4 7 3 5

输出

2

备注:

The lowest common ancestor (LCA) (also called least common ancestor) of two nodes v and w in a tree or directed acyclic graph (DAG) T is the lowest (i.e. deepest) node that has both v and w as descendants, where we define each node to be a descendant of itself (so if v has a direct connection from w, w is the lowest common ancestor).(From Wiki.)

思路:

1,lca的板子题稍微进阶点,参看lca的文章

2,用两棵树存各自的前缀后缀lca,然后调用即可

代码:

int n,k;
int vea[maxj],veb[maxj];
int lg[maxj];
int x[maxj];
void dd(){for(int i=1;i<=n;++i)lg[i]=lg[i-1]+(1<<lg[i-1]==i);
}
struct node{int pp[maxj],las[maxj];
int dep[maxj],vis[maxj],fa[maxj][100];
vector<int>g[maxj];
void add(int u,int v){g[u].emplace_back(v);
}
void dfs(int now ,int pre){dep[now]=dep[pre]+1;fa[now][0]=pre;for(int i=1;(1<<i)<=dep[now];++i){fa[now][i]=fa[fa[now][i-1]][i-1];}for(int i=0;i<g[now].size();++i){if(pre==g[now][i])continue;dfs(g[now][i],now);}
}int lca(int x,int y){if(dep[x]<dep[y])swap(x,y);while(dep[x]>dep[y])x=fa[x][lg[dep[x]-dep[y]]-1];if(x==y)return x;for(int k=lg[dep[x]];k>=0;--k){if(fa[x][k]!=fa[y][k]){x=fa[x][k];y=fa[y][k];}}return fa[x][0];
}
void dolca(){dfs(1,-1);pp[1]=x[1];for(int i=2;i<=k;++i){pp[i]=lca(pp[i-1],x[i]);}las[k]=x[k];for(int i=k-1;i>=1;--i){las[i]=lca(las[i+1],x[i]);}
}
int asklca(int w){if(w==1)return las[2];if(w==k)return pp[k-1];return lca(pp[w-1],las[w+1]);
}
}treea,treeb;//分treea和treeb两个对象void solve(){cin>>n>>k;//用struct封装一下,函数重用for(int i=1;i<=k;++i)cin>>x[i];for(int i=1;i<=n;++i)cin>>vea[i];for(int i=2;i<=n;++i){int pa;cin>>pa;treea.add(pa,i);}for(int i=1;i<=n;++i)cin>>veb[i];for(int i=2;i<=n;++i){int pa;cin>>pa;treeb.add(pa,i);}dd();treea.dolca();treeb.dolca();int ans=0;
//     for(int i=1;i<=k;++i)cout<<treea.las[i]<<' ';for(int i=1;i<=k;++i){int aa=treea.asklca(i);int bb=treeb.asklca(i);
//         cout<<aa<<' '<<bb<<'\n';if(vea[aa]>veb[bb])ans++;}cout<<ans<<'\n';
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/25245.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

PHP实名认证接口开发示例、银行卡实名认证API

在互联网技术多元化、高速的发展下&#xff0c;催生出在挑战中不断奋勇前进的互联网企业。但不能忽视的是&#xff0c;互联网技术的快速迭代也会使部分企业在冲击中败下阵来&#xff0c;面临淘汰的危机。随着O2O、共享经济等新兴商业形式的兴起&#xff0c;企业对实名认证业务的…

如何使用Python中的列表解析(list comprehension)进行高效列表操作

Python中的列表解析&#xff08;list comprehension&#xff09;是一种创建列表的简洁方法&#xff0c;它可以在单行代码中执行复杂的循环和条件逻辑。列表解析提供了一种快速且易于阅读的方式来生成新的列表。 以下是一些使用列表解析进行高效列表操作的示例&#xff1a; 1.…

用Python编写自动发送每日电子邮件报告的脚本

为了用 Python 编写自动发送每日电子邮件报告的脚本&#xff0c;你可以使用 smtplib 库来发送电子邮件&#xff0c;使用 email 库来创建电子邮件内容。此外&#xff0c;你可以使用 schedule 库来安排每天发送邮件的任务。以下是一个示例脚本以及如何设置和运行它的指导。 步骤…

JSON如何处理包含特殊字符的字段

在JSON中处理包含特殊字符的字段时&#xff0c;你通常不需要直接处理这些特殊字符&#xff0c;因为JSON格式本身就会对特殊字符进行转义。当你使用编程语言或工具来生成或解析JSON时&#xff0c;这些转义通常是自动处理的。 然而&#xff0c;如果你需要手动处理或理解这些转义…

华为策略流控

以下脚本仅做参考&#xff0c;具体IP地址和接口请按照现场实际情况写入。 [Huawei]acl 3001 [Huawei-acl-adv-3001]rule permit ip source 192.168.1.10 0.0.0.0 destination 192.168.2.10 0.0.0.0 //匹配需要做测试的源和目标地址 [Huawei-acl-adv-3001]rule permit ip sour…

[AIGC] CompletableFuture的重要方法有哪些?

CompletableFuture具有多种方法&#xff0c;使其成为异步编程的强大工具。在这里&#xff0c;我们将介绍一些最重要和常用的方法&#xff1a; CompletableFuture<T> supplyAsync(Supplier<T> supplier): 使用ForkJoinPool.commonPool()作为线程池来异步执行Suppile…

力扣2781.最长合法子字符串的长度

力扣2781.最长合法子字符串的长度 将字符串数组存入哈希表 枚举所有右端点反向遍历子串在哈希表中找所有以i为右端点的字符串若找到相同子串 更新j k 1 class Solution {public:int longestValidSubstring(string word, vector<string>& forbidden) {unordered_…

【马琴绿绮】马维衡古琴之马氏汉风 明代杉木制;周身髹朱红色漆

【马琴绿绮式】马维衡古琴之马氏汉风 明代杉木制&#xff1b;琴体周身髹朱红色漆&#xff0c;鹿角霜灰胎&#xff1b;形体壮硕、风格高古&#xff1b;音色松透、浑厚&#xff0c;音质纯净&#xff0c;按弹舒适&#xff0c;手感丝滑。

C++ 课堂实验 读取a.txt中文本,统计文本中字母数量

题目描述:读取a.txt中文本&#xff0c;统计文本中字母数量。 相关知识&#xff08;略&#xff09; 编程要求 根据提示&#xff0c;在右侧编辑器Begin-End处补充代码&#xff0c;完成本关要求。 测试说明 输入 读取a.txt读入文本 如&#xff1a; abc abc 输出 输出文本中字母数…

Effective Java 2 遇到多个构造器参数时要考虑使用构建器

第2个经验法则&#xff1a;用遇到多个构造器参数时要考虑使用构建器&#xff08;consider a builder when faced with many constructor parameters&#xff09; 上一条讨论了静态工厂相对于构造器来说有五大优势。但静态工厂和构造器有个共同的局限性:它 们都不能很好地扩展到…

华为坤灵路由器初始化的几个坑,含NAT配置

1、aaa密码复杂度修改&#xff1a; #使能设备对密码进行四选三复杂度检查功能。 <HUAWEI>system-view [HUAWEI]aaa [HUAWEI-aaa]local-aaa-user password policy administrator [HUAWEI-aaa-lupp-admin]password complexity three-of-kinds 2、本地用户名长度必须大…

springcloudalibaba项目注册nacos,在nacos上修改配置项不生效问题

一、背景 之前的项目启动正常,后来发现springcloudalibaba的各版本匹配不正确,于是对项目中的springboot、springcloud、springcloudalibaba版本进行匹配升级,nacos1.4.2匹配的springboot、springcloud、springcloudalibaba版本与我的项目中的版本比较接近,于是我便重新安…

零基础入门篇①⑦ Python可变序列类型--集合

Python从入门到精通系列专栏面向零基础以及需要进阶的读者倾心打造,9.9元订阅即可享受付费专栏权益,一个专栏带你吃透Python,专栏分为零基础入门篇、模块篇、网络爬虫篇、Web开发篇、办公自动化篇、数据分析篇…学习不断,持续更新,火热订阅中🔥专栏限时一个月(5.8~6.8)重…

某国有投资集团用人机制改革项目成功案例纪实

——引入淘汰机制&#xff0c;优化用人机制&#xff0c;有效传导压力 【客户行业】国有企业 【问题类型】用人机制改革 【客户背景】 某集团位于北方三线城市&#xff0c;是面向工业领域的综合类国有资本投资公司&#xff0c;集团员工数千人&#xff0c;部门十余个&#xf…

[方法] 《鸣潮》/《原神》呼出与锁定光标的功能细节

本方法适用于Cinemachine - FreeLook。 1. 锁定与呼出光标的功能实现 // 锁定光标 private void LockMouse() {// 将光标锁定在屏幕中间Cursor.lockState CursorLockMode.Locked;// 隐藏光标Cursor.visible false; }// 呼出光标 private void UnLockMouse() {// 释放光标Cu…

算法家族之一——二分法

目录 算法算法的打印效果如果算法里的整型“i”为1如果算法里的整型“i”为11 算法的流程图算法的实际应用总结 大家好&#xff0c;我叫 这是我58&#xff0c;现在&#xff0c;请看下面的算法。 算法 #define _CRT_SECURE_NO_WARNINGS 1//<--预处理指令 #include <stdi…

MyBatis使用MySQL5和MySQL8的用法

MySQL5: 添加依赖&#xff1a; <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency> 配置数据库&#xff1a; <!-- 配置连接数据库的信息&…

中国宠业新锐品牌展,2024苏州国际宠物展6月28日开展!

中国宠业新锐品牌展&#xff0c;2024苏州国际宠物展6月28日开展&#xff01; ​ 第2届华东国际宠物用品展览会(苏州)暨中国宠业新锐品牌展&#xff0c;将于6月28日-30日在苏州国际博览中心盛大举办&#xff0c;锁定年中市场黄金档期&#xff0c;同期以“NB展&#xff0c;更新鲜…

ObjectARX打印当前图纸为PDF(亲测有效)

CAD二次开发定制ObjectARX安装配置AutoCAD插件ZWCAD插件C++ //----------------------------------------------------------------------------- //----- acrxEntryPoint.cpp //----------------------------------------------------------------------------- #include &quo…

kvm管理工具-virsh

virsh 查看全部虚拟机列表停止虚拟机列表启动虚拟机强制关闭虚拟机连接虚拟机控制台查看虚拟机的详细信息查看虚拟机接口信息查看虚拟机xml文件配置删除虚拟机 KVM&#xff08;Kernel-based Virtual Machine&#xff09;是一种基于 Linux 内核的虚拟化技术&#xff0c;允许在一…