ok,又水了一道题
#include<stdio.h>
#include<stdlib.h>
#define max 1001
#define swap(a,b) a ^= b,b ^= a,a ^= b;
typedef struct Tnode *Tree;
struct Tnode{
int data[max];
int size=0;
};
int NCA(int p1, int p2){
while(p1!=p2){
if(p1>p2) {swap(p1,p2)}//保证p1在p2上
while(p2>p1){
p2/=2;
}
}
return p1;
}
int main()
{
int N,k;
scanf("%d",&N);
Tree t=(Tree)malloc(sizeof(struct Tnode));
t->data[0]=0;
for(t->size=1;t->size<=N;t->size++){
scanf("%d",&t->data[t->size]);
}
t->size--;
int p1,p2;
scanf("%d %d",&p1,&p2);
if(!t->data[p1]) printf("ERROR: T[%d] is NULL",p1);
else if(!t->data[p2]) printf("ERROR: T[%d] is NULL",p2);
else{
printf("%d %d",NCA(p1,p2),t->data[NCA(p1,p2)]);
}
return 0;
}