题目大意:
- 给定两颗节点编号都为1~n的树A和B,每个节点都有一个权重。
- 再给K个节点编号 ,求在两颗树中删除K中任意一个节点后剩余的k-1个节点中最近公共祖先节点的权重A中大于B课可行方案的数量。
解题思路:
-
首先我们要先了解一下LCA算法,LCA算法就是能在log(n) 的时间内求出两个点的最近公共祖先节点。
-
LCA 算法首先要预处理出来两个数组,f[i,j]表示i这个节点往上走2的j次方所能走到的节点,depth[i] 表示i这个节点的深度。求f[i,j]可以用倍增的思想来做,depth数组可以用宽搜来求出每个点的深度。
-
我们如果要求K个点最近公共祖先节点,只需要求这K个点中深度最大和深度最小的这两个节点的最小公共祖先节点。
-
然后我们可以重载一下sort函数,把这K个节点按照深度从小到大排好序;然后依次枚举我们要删除的节点的编号,只要我们选择删除的不是最小或最大的深度的节点我们就只需要求最小和最大深度的最近公共祖先节点然后比较它们的权重即可;如果删除的是最小的或最大深度的节点,那我们就退一步,我们就可以选第二小或者第二大深度的节点然后按照上述方法求出即可。
完整代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=100010,M=N*2;
//邻接表储存树A,B;
int ha[N],ea[M],nea[M],idxa;
int hb[N],eb[M],neb[M],idxb;
//LCA 倍增需要用到的数组
int fa[N][31],deptha[N];
int fb[N][31],depthb[N];
//队列
int qa[N],qb[N];
//权重
int wa[N],wb[N];
// 关键点
int ka[N],kb[N];
int stA[N],stB[N];
int n,m;
int cnt=0;
void bfs(int root,int u)
{
if(u==1)
{
memset(deptha,0x3f,sizeof deptha);
int qa[N];
int hh=0,tt=0;
qa[0]=root;
deptha[0]=0,deptha[root]=1;
stA[root]=1;
int cnt=1;
while(hh<=tt)
{
int t=qa[hh++];
for(int i=ha[t];i!=-1;i=nea[i])
{
int j=ea[i];
if(deptha[j]>deptha[t]+1)
{
deptha[j]=deptha[t]+1;
qa[++tt]=j;
fa[j][0]=t;
stA[j]=++cnt;
for(int k=1;k<=30;k++)
{
fa[j][k]=fa[fa[j][k-1]][k-1];
}
}
}
}
}
else{
memset(depthb,0x3f,sizeof depthb);
int hh=0,tt=0;
qb[0]=root;
depthb[0]=0,depthb[root]=1;
stB[root]=1;
int cnt=1;
while(hh<=tt)
{
int t=qb[hh++];
for(int i=hb[t];i!=-1;i=neb[i])
{
int j=eb[i];
if(depthb[j]>depthb[t]+1)
{
depthb[j]=depthb[t]+1;
qb[++tt]=j;
fb[j][0]=t;
stB[j]=++cnt;
for(int k=1;k<=30;k++)
{
fb[j][k]=fb[fb[j][k-1]][k-1];
}
}
}
}
}
}
void add(int a,int b,int u)
{
if(u==0)
{
ea[idxa]=b;
nea[idxa]=ha[a];
ha[a]=idxa++;
}
else{
eb[idxb]=b;
neb[idxb]=hb[a];
hb[a]=idxb++;
}
}
bool cmpA(int a,int b)
{
return stA[a]<stA[b];
}
bool cmpB(int a,int b)
{
return stB[a]<stB[b];
}
int lcaA(int x, int y) {
if (deptha[x] > deptha[y]) swap(x, y);
int tmp = deptha[y] - deptha[x], ans = 0;
for (int j = 0; tmp; ++j, tmp >>= 1)
if (tmp & 1) y = fa[y][j];
if (y == x) return x;
for (int j = 30; j >= 0 && y != x; --j) {
if (fa[x][j] != fa[y][j]) {
x = fa[x][j];
y = fa[y][j];
}
}
return fa[x][0];
}
int lcaB(int x, int y) {
if (depthb[x] > depthb[y]) swap(x, y);
int tmp = depthb[y] - depthb[x], ans = 0;
for (int j = 0; tmp; ++j, tmp >>= 1)
if (tmp & 1) y = fb[y][j];
if (y == x) return x;
for (int j = 30; j >= 0 && y != x; --j) {
if (fb[x][j] != fb[y][j]) {
x = fb[x][j];
y = fb[y][j];
}
}
return fa[x][0];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>m;
memset(ha,-1,sizeof ha);
memset(hb,-1,sizeof hb);
for(int i=1;i<=m;i++)
{
cin>>ka[i];
kb[i]=ka[i];
}
for(int i=1;i<=n;i++)
{
cin>>wa[i];
}
for(int i=1;i<=n-1;i++)
{
int b;
cin>>b;
add(i+1,b,0);
add(b,i+1,0);
}
for(int i=1;i<=n;i++) cin>>wb[i];
for(int i=1;i<=n-1;i++)
{
int b;
cin>>b;
add(i+1,b,1);
add(b,i+1,1);
}
bfs(1,1);
bfs(1,2);
sort(ka+1,ka+1+m,cmpA);
sort(kb+1,kb+1+m,cmpB);
int ans=0;
for(int i=1;i<=m;i++)
{
int xa=ka[1],ya=ka[m];
int xb=kb[1],yb=kb[m];
if(i==1) xa=ka[2];
else if(i==m) ya=ka[m-1];
if(xb==ka[i]) xb=kb[2];
else if(yb==ka[i]) yb=kb[m-1];
int lcaa=lcaA(xa,ya);
int lcab=lcaB(xb,yb);
if(wa[lcaa]>wb[lcab]) ans+=1;
}
cout<<ans<<endl;
return 0;
}