牛客多校Ancestor(lca,集合的lca)
题目描述NIO is playing a game about trees.
The game has two trees A,BA, BA,B each with NNN vertices. The vertices in each tree are numbered from 111 to NNN and the iii-th vertex has the weight viv_ivi. The root of each tree is vertex 1. Given KKK key numbers x1,…,xkx_1,\dots,x_kx1,…,xk, find the number of solutions that remove exactly one number so that the weight of the lowest common ancestor of the vertices in A with the remaining key numbers is greater than the weight of the lowest common ancestor of the vertices in B with the remaining key numbers.
输入描述:
The first line has two positive integers N,K(2≤K≤N≤105)N,K (2 \leq K \leq N \leq 10^5)N,K(2≤K≤N≤105).
The second line has KKK unique positive integers x1,…,xK(xi≤N)x_1,\dots,x_K (x_i \leq N)x1,…,xK(xi≤N).
The third line has NNN positive integers ai(ai≤109)a_i (a_i \leq 10^9)ai(ai≤109) represents the weight of vertices in A.
The fourth line has N−1N - 1N−1 positive integers {pai}\{pa_i\}{pai}, indicating that the number of the father of vertices i+1i+1i+1 in tree A is paipa_ipai.
The fifth line has nnn positive integers bi(bi≤109)b_i (b_i \leq 10^9)bi(bi≤109) represents the weight of vertices in B.
The sixth line has N−1N - 1N−1 positive integers {pbi}\{pb_i\}{pbi}, indicating that the number of the father of vertices i+1i+1i+1 in tree B is pbipb_ipbi. 输出描述:
One integer indicating the answer. 示例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.) 思路:
1,lca的板子题稍微进阶点,参看lca的文章
2,用两棵树存各自的前缀后缀lca,然后调用即可
代码:
int n,k;
int vea,veb;
int lg;
int x;
void dd(){
for(int i=1;i<=n;++i)
lg=lg+(1<<lg==i);
}
struct node{
int pp,las;
int dep,vis,fa;
vector<int>g;
void add(int u,int v){
g.emplace_back(v);
}
void dfs(int now ,int pre){
dep=dep+1;
fa=pre;
for(int i=1;(1<<i)<=dep;++i){
fa=fa];
}
for(int i=0;i<g.size();++i){
if(pre==g)continue;
dfs(g,now);
}
}
int lca(int x,int y){
if(dep<dep)swap(x,y);
while(dep>dep)
x=fa-dep]-1];
if(x==y)return x;
for(int k=lg];k>=0;--k){
if(fa!=fa){
x=fa;
y=fa;
}
}
return fa;
}
void dolca(){
dfs(1,-1);
pp=x;
for(int i=2;i<=k;++i){
pp=lca(pp,x);
}
las=x;
for(int i=k-1;i>=1;--i){
las=lca(las,x);
}
}
int asklca(int w){
if(w==1)return las;
if(w==k)return pp;
return lca(pp,las);
}
}treea,treeb;//分treea和treeb两个对象
void solve(){
cin>>n>>k;//用struct封装一下,函数重用
for(int i=1;i<=k;++i)cin>>x;
for(int i=1;i<=n;++i)cin>>vea;
for(int i=2;i<=n;++i){
int pa;cin>>pa;
treea.add(pa,i);
}
for(int i=1;i<=n;++i)cin>>veb;
for(int i=2;i<=n;++i){
int pa;cin>>pa;
treeb.add(pa,i);
}
dd();
treea.dolca();
treeb.dolca();
int ans=0;
// for(int i=1;i<=k;++i)cout<<treea.las<<' ';
for(int i=1;i<=k;++i){
int aa=treea.asklca(i);
int bb=treeb.asklca(i);
// cout<<aa<<' '<<bb<<'\n';
if(vea>veb)ans++;
}cout<<ans<<'\n';
}
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]