示例1
输入
5 3 5 4 3 6 6 3 4 6 1 2 2 4 7 4 5 7 7 1 1 3 2
输出
1
说明
In first case, the key numbers are 5,4,3. Remove key number 5, the lowest common ancestors of the vertices in A with the remaining key numbers is 2, in B is 3. Remove key number 4, the lowest common ancestors of the vertices in A with the remaining key numbers is 2, in B is 1. Remove key number 3, the lowest common ancestors of the vertices in A with the remaining key numbers is 4, in B is 1. Only remove key number 5 satisfies the requirement.
示例2
输入
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 5
输出
2
备注:
The lowest common ancestor (LCA) (also called least common ancestor) of two nodes v and w in a tree or directed acyclic graph (DAG) T is the lowest (i.e. deepest) node that has both v and w as descendants, where we define each node to be a descendant of itself (so if v has a direct connection from w, w is the lowest common ancestor).(From Wiki.)
题目大意:
解题思路:预处理这k个关键点的前缀LCA和后缀LCA,那么 假设去掉点i,那么剩余所有点的LCA就能等于LCA(pre[i-1],last[i+1]),其中pre[i-1]表示前i-1个点的LCA,last[i+1]表示从第i+1个点到第K个点的LCA,对这剩余的两部分求个LCA就是去掉点i之后所有点的LCA。思路看起来非常简单,但是确实不好想,比赛的时候就是死活想不起来QAQ,菜死了。。。
上代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int N=1e5+10;
int n,k;
int pre[N],last[N];
int va[N],vb[N],ha[N],ea[N<<1],wa[N<<1],nea[N<<1],idxa,hb[N],eb[N<<1],wb[N<<1],neb[N<<1],idxb,x[N];
int deptha[N],faa[N][31],depthb[N],fab[N][31];
int prea[N],lasta[N],preb[N],lastb[N];
void adda(int u,int v)
{
ea[idxa]=v;
nea[idxa]=ha[u];
ha[u]=idxa++;
}
void addb(int u,int v)
{
eb[idxb]=v;
neb[idxb]=hb[u];
hb[u]=idxb++;
}
void bfsa(int root)
{
memset(deptha,0x3f3f3f3f,sizeof deptha);
deptha[0]=0;//作为一个哨兵,用于处理跳出界的情况
deptha[root]=1;
queue<int>q;
q.push(root);
while(q.size())
{
int t=q.front();
q.pop();
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;
q.push(j);
faa[j][0]=t;
for(int k=1;k<=27;k++)
faa[j][k]=faa[faa[j][k-1]][k-1];
}
}
}
}
void bfsb(int root)
{
memset(depthb,0x3f3f3f3f,sizeof depthb);
depthb[0]=0;//作为一个哨兵,用于处理跳出界的情况
depthb[root]=1;
queue<int>q;
q.push(root);
while(q.size())
{
int t=q.front();
q.pop();
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;
q.push(j);
fab[j][0]=t;
for(int k=1;k<=27;k++)
fab[j][k]=fab[fab[j][k-1]][k-1];
}
}
}
}
int lca(int a,int b,int depth[],int fa[][31])
{
if(depth[a]<depth[b])
swap(a,b);
//先让深度大的结点往上跳
for(int i=27;i>=0;i--)
if(depth[fa[a][i]]>=depth[b])
a=fa[a][i];
if(a==b)
return a;
//两个点一块往上跳
for(int i=27;i>=0;i--)
{
if(fa[a][i]!=fa[b][i])//为了便于计算,我们最终要跳到最近公共祖先的下一个结点,所以跳跃之后两个点不能是一个点
{
a=fa[a][i];
b=fa[b][i];
}
}
return fa[a][0];
}
void do_lca()
{
prea[1]=x[1];
for(int i=2;i<=k;i++)
prea[i]=lca(prea[i-1],x[i],deptha,faa);
lasta[k]=x[k];
for(int i=k-1;i>=1;i--)
lasta[i]=lca(lasta[i+1],x[i],deptha,faa);
preb[1]=x[1];
for(int i=2;i<=k;i++)
preb[i]=lca(preb[i-1],x[i],depthb,fab);
lastb[k]=x[k];
for(int i=k-1;i>=1;i--)
lastb[i]=lca(lastb[i+1],x[i],depthb,fab);
}
int get_lca(int i,int pre[],int last[],int flag)
{
if(i==1)
return last[i+1];
if(i==k)
return pre[i-1];
if(flag==1)
return lca(pre[i-1],last[i+1],deptha,faa);
else
return lca(pre[i-1],last[i+1],depthb,fab);
}
int main()
{
cin>>n>>k;
memset(ha,-1,sizeof ha);
memset(hb,-1,sizeof hb);
for(int i=1;i<=k;i++)
cin>>x[i];
for(int i=1;i<=n;i++)
cin>>va[i];
for(int i=1;i<n;i++)
{
int u,v;
cin>>u;
adda(u,i+1);
adda(i+1,u);
}
for(int i=1;i<=n;i++)
cin>>vb[i];
for(int i=1;i<n;i++)
{
int u,v;
cin>>u;
addb(u,i+1);
addb(i+1,u);
}
bfsa(1);
bfsb(1);
do_lca();
int ans=0;
for(int i=1;i<=k;i++)
{
int la=get_lca(i,prea,lasta,1);
int lb=get_lca(i,preb,lastb,2);
if(va[la]>vb[lb])
ans++;
}
cout<<ans<<endl;
return 0;
}