当前位置:网站首页>“蔚来杯“2022牛客暑期多校训练营3 A.Ancestor LCA+暴力计数
“蔚来杯“2022牛客暑期多校训练营3 A.Ancestor LCA+暴力计数
2022-07-28 15:27:00 【HeartFireY】
A.Ancestor
题目分析
给出两棵编号 1 − n 1-n 1−n的树 A , B A,B A,B, A , B A,B A,B树上每个节点均有一个权值,给出 k k k个关键点的编号 x 1 … x n x_1…x_n x1…xn,问有多少种方案使得去掉恰好一个关键点使得剩余关键点在树 A A A上 L C A LCA LCA的权值大于树 B B B上 L C A LCA LCA的权值。
处理出所有关键节点的前缀 L C A LCA LCA和后缀 L C A LCA LCA,然后枚举所有的关键节点求 L C A LCA LCA后 C h e c k Check Check计数即可。
Code
#include <bits/stdc++.h>
#pragma gcc optimize("O2")
#pragma g++ optimize("O2")
#define int long long
#define endl '\n'
using namespace std;
const int N = 2e5 + 10, MOD = 862118861;
int a[N], b[N], key[N];
vector<int> g1[N], g2[N];
struct LCA{
struct edge{
int to, len, next; } E[N];
int cnt, last[N], fa[N], top[N], deep[N], siz[N], son[N], val[N];
void addedge(int a, int b, int len = 0){
E[++cnt] = (edge){
b, len, last[a]}, last[a] = cnt;
}
void dfs1(int x){
deep[x] = deep[fa[x]] + 1;
siz[x] = 1;
for (int i = last[x]; i; i = E[i].next){
int to = E[i].to;
if (fa[x] != to && !fa[to]){
val[to] = E[i].len;
fa[to] = x;
dfs1(to);
siz[x] += siz[to];
if (siz[son[x]] < siz[to]) son[x] = to;
}
}
}
void dfs2(int x){
if (x == son[fa[x]]) top[x] = top[fa[x]];
else top[x] = x;
for (int i = last[x]; i; i = E[i].next)
if (fa[E[i].to] == x && fa[E[i].to] != E[i].to) dfs2(E[i].to);
}
void init(int root) {
dfs1(root), dfs2(root); }
int query(int x, int y){
for (; top[x] != top[y]; deep[top[x]] > deep[top[y]] ? x = fa[top[x]] : y = fa[top[y]]);
return deep[x] < deep[y] ? x : y;
}
}ta, tb;
int pa[N], pb[N], sa[N], sb[N];
inline void solve(){
int n = 0, k = 0; cin >> n >> k;
for(int i = 1; i <= k; i++) cin >> key[i];
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 2; i <= n; i++){
int fa; cin >> fa;
ta.addedge(fa, i), ta.addedge(i, fa);
}
for(int i = 1; i <= n; i++) cin >> b[i];
for(int i = 2; i <= n; i++){
int fa; cin >> fa;
tb.addedge(fa, i), tb.addedge(i, fa);
}
ta.init(1), tb.init(1);
pa[1] = pb[1] = key[1], sa[k] = sb[k] = key[k];
for(int i = 2; i <= k; i++)
pa[i] = ta.query(pa[i - 1], key[i]), pb[i] = tb.query(pb[i - 1], key[i]);
for(int i = k - 1; i >= 1; i--)
sa[i] = ta.query(sa[i + 1], key[i]), sb[i] = tb.query(sb[i + 1], key[i]);
int ans = 0;
for(int i = 1; i <= k; i++){
int qa = 0, qb = 0;
if(i == 1){
qa = sa[i + 1], qb = sb[i + 1];
} else if(i == k){
qa = pa[i - 1], qb = pb[i - 1];
} else {
qa = ta.query(pa[i - 1], sa[i + 1]);
qb = tb.query(pb[i - 1], sb[i + 1]);
}
if(a[qa] > b[qb]) ans++;
}
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout << fixed << setprecision(12);
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
边栏推荐
- Vm501 development kit development version single vibrating wire sensor acquisition module geotechnical engineering monitoring
- Redis series 4: sentinel (sentinel mode) with high availability
- 动态规划 --- 数位统计DP
- Qt学习之安装
- Leetcode topic
- LwIP development | socket | DNS domain name resolution
- 李宏毅《机器学习》丨5. Tips for neural network design(神经网络设计技巧)
- I can only sell the company after the capital has been "cut off" for two years
- Reentrant and non reentrant
- laravel
猜你喜欢
随机推荐
资本「断供」两年,我只能把公司卖了
Rosen's QT journey 102 listmodel
Baidu editor ueeditor, when editing too much content, the toolbar is not visible, which is not convenient for editing or uploading problems
Vm501 development kit development version single vibrating wire sensor acquisition module geotechnical engineering monitoring
IT远程运维是什么意思?远程运维软件哪个好?
MySQL view event status statements and modification methods
Dynamic programming -- digital statistics DP
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
SCI scientific paper writing Growth Camp (full version)
ANSYS二次开发 - MFC界面调用ADPL文件
PHP计算坐标距离
大地坐标系转换火星坐标系
JS bidirectional linked list 01
李宏毅《机器学习》丨4. Deep Learning(深度学习)
李宏毅《机器学习》丨5. Tips for neural network design(神经网络设计技巧)
Basic structure and operation principle of solar street lamp
A program for judging the result of cyclic input
食品安全 | 这两类瓜果宜改善便秘 孕妇人群尤其建议
HyperMesh自动保存(增强版)插件使用说明
LabVIEW Linx toolkit controls Arduino equipment (expansion-1)








