倍增法LCA + DP #includecstdio#includecstring#includealgorithmusing namespace std;const int N = 100010;int val[N];int p[N][18];int main(){int t,T,n,q,i,j;int num,v;scanf("%d",T);for(t=1;t=T;t++){ scanf("%d %d",n,q); memset(p,-1,sizeof(p))
倍增法LCA + DP
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 100010;
int val[N];
int p[N][18];
int main(){
int t,T,n,q,i,j;
int num,v;
scanf("%d",&T);
for(t=1;t<=T;t++){
scanf("%d %d",&n,&q);
memset(p,-1,sizeof(p));
val[0]=1;
for(i=1;i<n;i++){
scanf("%d %d",&p[i][0],&val[i]);
for(j=1;j<18;j++){
p[i][j]=p[p[i][j-1]][j-1];
if(p[i][j]==-1)break;
}
}
printf("Case %d:\n",t);
for(i=1;i<=q;i++){
scanf("%d %d",&v,&num);
for(j=17;j>=0;j--){
if(p[v][j]!=-1 && val[p[v][j]]>=num)
v=p[v][j];
}
printf("%d\n",v);
}
}
return 0;
}