当前位置:网站首页>"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 record
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 record
2022-07-26 18:27:00 【Shanhj】
A-Ancestor
The main idea of the topic : Give two trees ( The number of knots is the same ), Every node in the tree has a value , And a set of key nodes , It is required to remove one of the key nodes , Make the remaining key nodes in A Treelike LCA Value ratio of B Treelike LCA It's worth more , Find the number of solutions .
practice : If you want to quickly find the remaining key nodes LCA, Key nodes can be preprocessed , Calculate the prefix LCA, Go to a point and follow the suffix LCA Calculate again LCA You can get the remaining points LCA. Therefore, this question can enumerate after removing each key point LCA value .
principle :
With prea[i] Indicates the front of the key node i There's a point in A Treelike LCA,lsta[i] Indicates the number of key nodes i And the following points are A Treelike LCA
that prea[i + 1] = LCA(prea[i], key[i + 1]), Empathy lsta[i - 1] = LCA(lsta[i], key[i - 1])
Remove a dot j after , The remaining points LCA' by :
LCA' = LCA(prea[j - 1], lsta[j + 1])
Special judgment is required when removing the first and last points
Code :( seek LCA Used interval RMQ, Euler Sequence )
#include <bits/stdc++.h>
#define mem(a, v) memset(a, v, sizeof(a))
const int N = 2e5 + 5;
using namespace std;
struct lcatree
{
int pos[N]; // Record the position of the node in the Euler sequence for the first time
int seq[N * 2]; // Record Euler sequence
int dep[N * 2]; // Record the depth of the corresponding subscript in the Euler sequence
int st[N * 2][20]; // st surface , Record the subscript with the smallest depth
int val[N];
int tot = 0;
bool vis[N];
vector<int> son[N]; // Record the subtree of each node
void dfs(int u, int d) // Construct Euler sequence ,u Represents the current node ,d Expressing depth
{
vis[u] = 1;
pos[u] = ++tot;
seq[tot] = u;
dep[tot] = d;
for (auto s : son[u])
{
if (vis[s]) continue;
dfs(s, d + 1);
seq[++tot] = u; // Every time you backtrack, you should add the current point to the Euler sequence again
dep[tot] = d;
}
}
void st_create() // establish st surface
{
for (int i = 1; i <= tot; i++)
st[i][0] = i;
int k = log2(tot), f1, f2;
for (int j = 1; j <= k; j++)
{
for (int i = 1; i <= tot - (1 << j) + 1; i++)
{
f1 = st[i][j - 1], f2 = st[i + (1 << (j - 1))][j - 1];
st[i][j] = dep[f1] < dep[f2] ? f1 : f2;
}
}
}
int get_lca(int u, int v)
{
int l = pos[u], r = pos[v];
if (l > r) swap(l, r);
int k = log2(r - l + 1);
int f1 = st[l][k], f2 = st[r - (1 << k) + 1][k];
return dep[f1] < dep[f2] ? seq[f1] : seq[f2]; // When returning, convert the subscript into seq The values in the array
}
void init(int n)
{
mem(vis, 0);
int fa;
for (int i = 1; i <= n; i++)
cin >> val[i];
for (int i = 2; i <= n; i++)
{
cin >> fa;
son[fa].push_back(i);
}
dfs(1, 0);
st_create();
}
} treea, treeb;
int key[N], prea[N], preb[N], lsta[N], lstb[N];
signed main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, k, ans = 0;
cin >> n >> k;
for (int i = 1; i <= k; i++)
cin >> key[i];
treea.init(n);
treeb.init(n);
prea[1] = key[1];
preb[1] = key[1];
for (int i = 2; i <= k; i++)
{
prea[i] = treea.get_lca(prea[i - 1], key[i]);
preb[i] = treeb.get_lca(preb[i - 1], key[i]);
}
lsta[k] = key[k];
lstb[k] = key[k];
for (int i = k - 1; i >= 1; i--)
{
lsta[i] = treea.get_lca(lsta[i + 1], key[i]);
lstb[i] = treeb.get_lca(lstb[i + 1], key[i]);
}
for (int i = 1; i <= k; i++)
{
if (i == 1)
{
if (treea.val[lsta[2]] > treeb.val[lstb[2]])
ans++;
}
else if (i == k)
{
if (treea.val[prea[k - 1]] > treeb.val[preb[k - 1]])
ans++;
}
else
{
int lcaa = treea.get_lca(prea[i - 1], lsta[i + 1]);
int lcab = treeb.get_lca(preb[i - 1], lstb[i + 1]);
if (treea.val[lcaa] > treeb.val[lcab])
ans++;
}
}
cout << ans;
return 0;
}
边栏推荐
- J9 number theory: how to avoid the trap of stepping on thunder?
- Sign up now | cloud native technology exchange meetup Guangzhou station has been opened, and I will meet you on August 6!
- 效率提升98%!高海拔光伏电站运维巡检背后的AI利器
- 8.1 Diffie Hellman key exchange
- How about the employment prospects of Russian translation? How to do a good job of Russian translation
- Become a test / development programmer, Xiao Zhang: reality is coming
- LeetCode 0139. 单词拆分
- SQL判断某列中是否包含中文字符、英文字符、纯数字,数据截取
- LeetCode_ 1005_ Maximized array sum after K negations
- 网上炒股,选择在哪里开户比较安全呢?
猜你喜欢

Kindergarten system based on SSM

Download and configuration of irrklang audio library

推荐效果不如意,不如试试飞桨图学习

LeetCode50天刷题计划(Day 5—— 最长回文子串 10.50-13:00)

隐私计算基础组件系列-混淆电路

ssm练习第三天_分页助手_安全框架

复现gallerycms字符长度限制短域名绕过

【Unity3D】摇杆

Maximum sum of continuous subarray of sword finger offer (2)

Relative path and absolute path
随机推荐
如何组装一个注册中心
PyQt5快速开发与实战 3.5 菜单栏与工具栏
Leetcode 50 day question brushing plan (day 3 - concatenate substrings of all words 10.00-13.20)
【Unity3D】摇杆
Distributed link tracking Jaeger's use in golang
数据仓库:详解维度建模之事实表
Linked list - the first common node of two linked lists
SSH based online mall
俄语翻译的就业前景怎样 如何做好俄语翻译工作
有一说一,阿里P7的薪资待遇是真的香
钉钉第三方服务商应用ISV应用开发及上架教程
How to switch nodejs versions at will?
Redis主从复制,读写分离,哨兵模式
Maximum sum of continuous subarray of sword finger offer (2)
Ten year structure five year life-06 impulse to leave
网上炒股,选择在哪里开户比较安全呢?
Download and configuration of irrklang audio library
IrrKlang音频库的下载和配置
链表-合并两个排序的列表
The Agile Manifesto has four values and twelve principles