当前位置:网站首页>2022牛客多校第三场 A Ancestor
2022牛客多校第三场 A Ancestor
2022-08-05 00:18:00 【Rain Sure】
题目链接
题目大意
给了我们两棵树 A A A和 B B B,然后给了我们 k k k个点,问我们分别在 A A A和 B B B删去其中一个点,使得剩余点在 A A A树中的最近公共祖先的权值大于剩余点在 B B B树中的最近公共祖先的权值。
题解
首先,预处理出来,这 k k k个点的前缀最近公共祖先和后缀最近公共祖先,然后,分别枚举 k k k个点,将该点去掉的最近公共祖先就是 l c a ( p r e [ i − 1 ] , b a c k [ i + 1 ] ) lca(pre[i - 1], back[i + 1]) lca(pre[i−1],back[i+1]),如此便可进行判断。
代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);
#define x first
#define y second
#define int long long
#define endl '\n'
const int inf = 1e9 + 10;
const int maxn = 100010, M = 200010;
const int mod = 1e9 + 7;
typedef pair<int,int> PII;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
int key[maxn];
int n, k;
struct Tree
{
int h[maxn], e[M], ne[M], idx;
int v[maxn];
int pre[maxn], back[maxn];
int fa[maxn][20];
int depth[maxn];
Tree()
{
memset(h, -1, sizeof h);
}
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
void bfs(int root)
{
memset(depth, 0x3f, sizeof depth);
depth[0] = 0, depth[root] = 1;
queue<int> q;
q.push(root);
while(q.size())
{
auto t = q.front();
q.pop();
for(int i = h[t]; ~i; i = ne[i]) {
int j = e[i];
if(depth[j] > depth[t] + 1) {
depth[j] = depth[t] + 1;
q.push(j);
fa[j][0] = t;
for(int k = 1; k <= 18; k ++) {
fa[j][k] = fa[fa[j][k - 1]][k - 1];
}
}
}
}
}
int lca(int a, int b)
{
if(a == 0) return b;
if(b == 0) return a;
if(depth[a] < depth[b]) swap(a, b);
for(int k = 18; k >= 0; k --){
if(depth[fa[a][k]] >= depth[b]) {
a = fa[a][k];
}
}
if(a == b) return a;
for(int k = 18; k >= 0; k --){
if(fa[a][k] != fa[b][k]) {
a = fa[a][k];
b = fa[b][k];
}
}
return fa[a][0];
}
void init()
{
bfs(1);
pre[1] = key[1], back[k] = key[k];
for(int i = 2; i <= k; i ++) pre[i] = lca(pre[i - 1], key[i]);
for(int i = k - 1; i >= 1; i --) back[i] = lca(back[i + 1], key[i]);
}
int calc(int x)
{
return v[lca(pre[x - 1], back[x + 1])];
}
}ta, tb;
signed main()
{
IOS;
cin >> n >> k;
for(int i = 1; i <= k; i ++) cin >> key[i];
for(int i = 1; i <= n; i ++) cin >> ta.v[i];
for(int i = 2; i <= n; i ++){
int x; cin >> x;
ta.add(x, i);
}
for(int i = 1; i <= n; i ++) cin >> tb.v[i];
for(int i = 2; i <= n; i ++){
int x; cin >> x;
tb.add(x, i);
}
ta.init();
tb.init();
int res = 0;
for(int i = 1; i <= k; i ++){
int t1 = ta.calc(i), t2 = tb.calc(i);
if(t1 > t2) res ++;
}
cout << res << endl;
return 0;
}
边栏推荐
- Modelers experience sharing: model study method
- 统计单词(DAY 101)华中科技大学考研机试题
- tensor.nozero(), mask, [mask]
- leetcode: 266. All Palindromic Permutations
- 软件测试面试题:软件验收测试的合格通过准则?
- After another 3 days, I have sorted out 90 NumPy examples, and I can't help but bookmark it!
- gorm的Raw与scan
- Cython
- 电子行业MES管理系统的主要功能与用途
- 2022牛客暑期多校训练营5(BCDFGHK)
猜你喜欢

刘润直播预告 | 顶级高手,如何创造财富

三、实战---爬取百度指定词条所对应的结果页面(一个简单的页面采集器)
![[230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots](/img/fa/5bdc81b1ebfc22d31f42da34427f3e.png)
[230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots

软件质量评估的通用模型

what?测试/开发程序员要被淘汰了?年龄40被砍到了32?一瞬间,有点缓不过神来......

What is next-generation modeling (with learning materials)

【idea】idea配置sql格式化

导入JankStats检测卡帧库遇到问题记录
![[CVA Valuation Training Camp] Financial Modeling Guide - Lecture 1](/img/8b/360df9a9094037dc358cb21c60cdc8.png)
[CVA Valuation Training Camp] Financial Modeling Guide - Lecture 1

First, the basic concept of reptiles
随机推荐
怎样进行在不改变主线程执行的时候,进行日志的记录
软件测试面试题:请你分别画出 OSI 的七层网络结构图和 TCP/IP 的四层结构图?
【论文笔记】—低照度图像增强—Unsupervised—EnlightenGAN—2019-TIP
LeetCode Hot 100
【CVA估值训练营】财务建模指南——第一讲
矩阵数学原理
ansible学习笔记分享-含剧本示例
DNS常见资源记录类型详解
Three tips for you to successfully get started with 3D modeling
【Valentine's Day special effects】--Canvas realizes full screen love
[230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots
"Relish Podcast" #397 The factory manager is here: How to use technology to empower the law?
MAUI Blazor 权限经验分享 (定位,使用相机)
E - Distance Sequence (前缀和优化dp
数据类型-整型(C语言)
【Unity编译器扩展之进度条】
The master teaches you the 3D real-time character production process, the game modeling process sharing
matlab中rcosdesign函数升余弦滚降成型滤波器
隐私计算综述
日志(logging模块)