pipioj 1265: 最近公共祖先

http://www.pipioj.online/problem.php?id=1265

 1 #define bug(x) cout<<#x<<" is "<<x<<endl
 2 #define IO std::ios::sync_with_stdio(0)
 3 #define ull unsigned long long
 4 #include <bits/stdc++.h>
 5 #define iter ::iterator
 6 #define pa pair<int,ll>
 7 #define pp pair<int,pa>
 8 using namespace  std;
 9 #define ll long long
10 #define mk make_pair
11 #define pb push_back
12 #define se second
13 #define fi first
14 #define ls o<<1
15 #define rs o<<1|1
16 ll mod=998244353;
17 const int N=2e5+5;
18 
19 vector<int>g[N];
20 
21 int a[N],b[N],c[N];
22 int l[N],r[N],fa[N];
23 
24 int tot=1,cnt=0;
25 
26 void dfs(int x){
27     if(tot>cnt)return;
28     if(x==-1){
29         return;
30     }
31     
32     l[x]=a[++tot];
33     fa[a[tot]]=x;
34     dfs(a[tot]);
35     r[x]=a[++tot];
36     fa[a[tot]]=x;
37     dfs(a[tot]);
38 }
39 int main(){
47     IO;
48     int x;
49     while(cin>>x){
50         a[++cnt]=x;
51     }
52     fa[a[1]]=-1;
53     dfs(a[1]);
54     int y,n1=0,n2=0;
55     y=a[cnt-1];
56     while(y!=-1){
57         b[++n1]=y;
58         y=fa[y];
59     }
60     y=a[cnt];
61     while(y!=-1){
62         c[++n2]=y;
63         y=fa[y];
64     }
65     if(n1>n2){
66         for(int i=1;i<=n2;i++){
67             if(b[i+n1-n2]==c[i]){
68                 cout<<c[i]<<endl;
69                 break;
70             }
71         }
72     }
73     else{
74         for(int i=1;i<=n1;i++){
75             if(b[i]==c[i+n2-n1]){
76                 cout<<b[i]<<endl;
77                 break;
78             }
79         }
80     }
81     /*for(int i=1;i<=8;i++){
82         printf("i=%d: %d  %d\n",i,l[i],r[i]);
83     }*/
84     
85 
86 }

 非递归版

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef int ElemType; 
 4 
 5 typedef struct Node{
 6     ElemType data;
 7     Node* lson;
 8     Node* rson;     
 9     Node(ElemType data, Node* lson, Node* rson) {
10         this->data = data;
11         this->lson = lson;
12         this->rson = rson;
13     }   
14 }*Tree;
15 
16 stack<Tree>s,s1,s2;
17 
18 Tree build(){
19     int x;
20     scanf("%d",&x);
21     if(x==-1)return NULL;
22     Tree T=(Tree)malloc(sizeof(Node));
23     T->data=x;
24     T->lson=build();
25     T->rson=build();
26     return T;
27 }
28 
29 void preorder(Tree T){
30     if(T){
31         printf("%d ",T->data);
32         preorder(T->lson);
33         preorder(T->rson);
34     }
35     
36 }
37 
38 int get_ancester(Tree T,int x,int y){
39     Tree p=T;
40     Tree r=NULL;
41     while(p||!s.empty()){
42         if(p){
43             s.push(p);
44             p=p->lson;
45         }
46         else{
47             p=s.top();
48             if(p->data==x)s1=s;
49             if(p->data==y)s2=s;
50             if(!s1.empty()&&!s2.empty())break;
51             if(p->rson&&p->rson!=r)p=p->rson;
52             else{
53                 s.pop();
54                 r=p;
55                 p=NULL;
56             }
57         }
58     }
59     if(s1.empty()||s2.empty())return -1;
60     int n=s1.size(),m=s2.size();
61     //int t=n<m?n:m;
62     if(n<m)while(m>n)m--,s2.pop();
63     else while(n>m)n--,s1.pop();
64     while(!s1.empty()){
65         if(s1.top()==s2.top())break;
66         s1.pop();
67         s2.pop();
68     }
69     printf("%d\n",s1.top()->data);
70     /*while(!s1.empty()){
71         printf("%d ",s1.top()->data);
72         s1.pop();
73     }*/
74     return 1;
75 
76 }
77 
78 
79 int main(){
80     /*Node* v7 = new Node(7, NULL, NULL);
81     Node* v1 = new Node(1, NULL, NULL);
82     Node* v3 = new Node(3, NULL, NULL);
83     Node* v2 = new Node(2, v1, v3);
84     Node* v5 = new Node(5, NULL, NULL);
85     Node* v6 = new Node(6, v5, v7);
86     Node* v4 = new Node(4, v2, v6);*/
87 
88     //Tree T=v4;
89     //get_ancester(T,1,8);
90     Tree T;
91     T=build();
92     int x,y;
93     scanf("%d%d",&x,&y);
94     get_ancester(T,x,y);
95     //preorder(T);
96 
97 }

 

posted @ 2020-09-23 21:19  Venux  阅读(229)  评论(0编辑  收藏  举报