当前位置:网站首页>(2022 Niuke multi school III) a-ancestor (LCA)
(2022 Niuke multi school III) a-ancestor (LCA)
2022-07-27 07:22:00 【AC__ dream】
subject :
Examples 1 Input :
5 3
5 4 3
6 6 3 4 6
1 2 2 4
7 4 5 7 7
1 1 3 2Examples 1 Output :
1Examples 2 Input :
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 5Examples 2 Output :
2The question : Give two trees numbered 1~n The tree of A,B,A Trees and B Each node in the tree has a weight , give k Key point numbers x1,x2……,xk, Ask how many ways to remove a key point and make the remaining key points in the tree A Upper LCA The weight of is greater than that of the tree B On LCA A weight .
Be careful : One key point is removed here, but it is not considered , It's not true that you can't go through this point , For example, remove No 1 Special points , It means solving on the original tree x2……,xk Of LCA.
analysis :LCA Is the common ancestor on the tree , Can't LCA My little friend can have a look here : Recent public ancestor (LCA)( On the tree )_AC__dream The blog of -CSDN Blog _ Recent public ancestor
We can The sequence of key points after preprocessing is A Trees and B Prefix on tree LCA And suffixes LCA
Variables in code :
lcaprea[i] Express a Middle front i Special points among the points LCA value ,lcasufa[i] Express a Midpoint i~n Special points in LCA value
lcapreb[i] Express b Middle front i Special points among the points LCA value ,lcasufb[i] Express b Midpoint i~n Special points in LCA value
For example, get rid of number i The remaining special points are A Tree and B Upper LCA, Then it is to solve x1,x2……,xi-1,xi+1,……,xk stay A Tree and B On the tree LCA, Because we have preprocessed it LCA Prefix , that x1,x2……,xi-1,xi+1,……,xk stay A On the tree LCA Namely lcaprea[i-1] and lcasufa[i+1] stay A The nearest common ancestor on the tree ,x1,x2……,xi-1,xi+1,……,xk stay B On the tree LCA Namely lcapreb[i-1] and lcasufb[i+1] stay B The nearest common ancestor on the tree . Then we can directly enumerate all the special points for judgment .
Here is the code :
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
using namespace std;
const int N=1e6+10;
bool vis[N];
int wa[N],wb[N],fa[N][21],fb[N][21];
int h1[N],e1[N*10],ne1[N*10],idx;
int h2[N],e2[N*10],ne2[N*10],d1[N],d2[N];
int lcaprea[N],lcasufa[N];//lcaprea[i] Express a Middle front i Special points among the points lca value ,lcasufa[i] Express a Midpoint i~n Special points in lca value
int lcapreb[N],lcasufb[N];//lcapreb[i] Express b Middle front i Special points among the points lca value ,lcasufb[i] Express b Midpoint i~n Special points in lca value
void add(int x,int y,int p)
{
if(p==1)
{
e1[idx]=y;
ne1[idx]=h1[x];
h1[x]=idx++;
}
else
{
e2[idx]=y;
ne2[idx]=h2[x];
h2[x]=idx++;
}
}
void dfs1(int x,int father)
{
d1[x]=d1[father]+1;// Calculate the depth
fa[x][0]=father;
for(int i=1;i<=20;i++)
fa[x][i]=fa[fa[x][i-1]][i-1];
for(int i=h1[x];i!=-1;i=ne1[i])
{
int j=e1[i];
if(j==father) continue;
dfs1(j,x);
}
}
void dfs2(int x,int father)
{
d2[x]=d2[father]+1;// Calculate the depth
fb[x][0]=father;
for(int i=1;i<=20;i++)
fb[x][i]=fb[fb[x][i-1]][i-1];
for(int i=h2[x];i!=-1;i=ne2[i])
{
int j=e2[i];
if(j==father) continue;
dfs2(j,x);
}
}
int lca(int x,int y,int op)
{
if(x==0) return y;
if(y==0) return x;
if(op==1)
{
if(d1[x]<d1[y]) swap(x,y);
for(int i=20;i>=0;i--)// You need to x and y Move to the same height
if(d1[fa[x][i]]>=d1[y])
x=fa[x][i];
if(x==y) return x;
for(int i=20;i>=0;i--)
if(fa[x][i]!=fa[y][i])
{
x=fa[x][i];
y=fa[y][i];
}
return fa[x][0];
}
else
{
if(d2[x]<d2[y]) swap(x,y);
for(int i=20;i>=0;i--)// You need to x and y Move to the same height
if(d2[fb[x][i]]>=d2[y])
x=fb[x][i];
if(x==y) return x;
for(int i=20;i>=0;i--)
if(fb[x][i]!=fb[y][i])
{
x=fb[x][i];
y=fb[y][i];
}
return fb[x][0];
}
}
int main()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=k;i++)
{
int t;
scanf("%d",&t);
vis[t]=true;
}
for(int i=1;i<=n;i++)
{
scanf("%d",&wa[i]);
h1[i]=h2[i]=-1;
}
for(int i=2;i<=n;i++)
{
int t;
scanf("%d",&t);
add(t,i,1);
}
for(int i=1;i<=n;i++)
scanf("%d",&wb[i]);
for(int i=2;i<=n;i++)
{
int t;
scanf("%d",&t);
add(t,i,2);
}
dfs1(1,-1);
dfs2(1,-1);
int lca1=-1,lca2=-1;
for(int i=1;i<=n;i++)
{
if(vis[i])
{
if(lca1==-1) lca1=lca2=i;
else
{
lca1=lca(lca1,i,1);
lca2=lca(lca2,i,2);
}
}
}
int t=0;
for(int i=1;i<=n;i++)
{
if(vis[i])
{
if(!t) t=i;
else t=lca(t,i,1);
}
lcaprea[i]=t;
}
t=0;
for(int i=n;i>=1;i--)
{
if(vis[i])
{
if(!t) t=i;
else t=lca(t,i,1);
}
lcasufa[i]=t;
}
t=0;
for(int i=1;i<=n;i++)
{
if(vis[i])
{
if(!t) t=i;
else t=lca(t,i,2);
}
lcapreb[i]=t;
}
t=0;
for(int i=n;i>=1;i--)
{
if(vis[i])
{
if(!t) t=i;
else t=lca(t,i,2);
}
lcasufb[i]=t;
}
int ans=0;
for(int i=1;i<=n;i++)
if(vis[i])
{
int ta=lca(lcaprea[i-1],lcasufa[i+1],1);
int tb=lca(lcapreb[i-1],lcasufb[i+1],2);
if(wa[ta]>wb[tb]) ans++;
}
printf("%d",ans);
return 0;
}
边栏推荐
- Codeworks round 809 (Div. 2) (6/6) (Kruskal reconstruction tree)
- py2exe qt界面风格变成了win98解决方案
- flink cdc 抽取oracle的数据,会不断的占用oracle的内存吗,最后引发ora-040
- Overall dichotomy?
- Codeforces Round #787 (Div. 3)(7/7)
- MySQL limit paging query optimization practice
- Esp8266 (esp-12f) third party library use -- sparkfun_ Apds9960 (gesture recognition)
- “蔚来杯“2022牛客暑期多校训练营1
- Jmeter:接口自动化测试-BeanShell对数据库数据和返回数据比较
- C语言程序设计 | 程序编译与预处理
猜你喜欢

高级IO提纲

Internal class -- just read this article~

pytorch笔记:TD3

Instruction set x digital technology accelerates the digital transformation of government and enterprises, and builds Unicorn enterprise alliance in DT field

Which C4d cloud rendering platform to cooperate with?

35. Search insert position

The possibility of metauniverse from the perspective of technical principles: how Omniverse "built" Mars

一款开源 OA 办公自动化系统

Advanced IO outline

火狐浏览器,访问腾讯云服务器的时候,出现建立安全连接失败的问题。
随机推荐
Usage of string class
yhb_sysbench
火狐浏览器,访问腾讯云服务器的时候,出现建立安全连接失败的问题。
MySQL2
网络入门——vlan及trunk概述
算法--斐波那契数列(Kotlin)
Pan Aimin, chairman of instruction set, attended the 2022 ecug con to speak for China's technical forces
functools模块
在rhel7.3中编译和使用log4cxx
36 - new promise method: allsettled & any & race
Advanced IO outline
Will Flink CDC constantly occupy Oracle's memory by extracting Oracle's data, and finally cause oracle-040
Instruction set x digital technology accelerates the digital transformation of government and enterprises, and builds Unicorn enterprise alliance in DT field
12. Integer to Roman
35. Search insert position
Synchronized锁
Jmeter: interface automation test - BeanShell compares database data and return data
【QT】capture.obj:-1: error: LNK2019: 无法解析的外部符号 __imp_htons(解决方法)
Automatically generate UML sequence diagram according to text (draw.io format)
Oracle数据库问题