The description of the topic is very recent public ancestors , and the fact that the problem is not difficult, is the trouble point (code can actually be simplified), I wrote more judgments.
The recent common ancestor LCA of both to find out the distance between the two and the LCA
Analysis: Given A and B, if the LCA (b) = = A or b, then they must be choking, is a father-son, Sun and other relations.
If LCA (A, B) > A and B, if the generational of a is high (using depth to represent generational), and DISA = 1, then they are uncles, nephews, etc.
Furthermore, cousin cousin of the relationship, the code is explained in more detail, the code is as follows:
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;#defineN 32767structnode{intL,r,pa,deep;} Node[n+Ten];voidDfsintXintd) { if(x > N)return; NODE[X].L= x*2+1; NODE[X].R= x*2+2; Node[x].deep=D; if(x==0) NODE[X].PA =x; Else if(%2==0) NODE[X].PA = x/2-1; ElseNODE[X].PA = x/2; DFS (Node[x].l,d+1); DFS (Node[x].r,d+1);}intLCA (intAintb) { while(Node[a].deep >node[b].deep) {a=NODE[A].PA; } while(Node[b].deep >node[a].deep) {b=NODE[B].PA; } while(A! =b) {a=NODE[A].PA; b=NODE[B].PA; } returnA;}intMain () {//freopen ("A.in.cpp", "R", stdin);Dfs0,1);///preprocessing each node's father and depth intA,B,LCA,DISA,DISB; stringtmp; Chargender; while(~SCANF ("%d%d%c",&a,&b,&gender)) { if(a==-1&& b==-1) Break; LCA= LCA (A, b);///Calculate the LCA and distanceDisa = Node[a].deep-Node[lca].deep; DISB= Node[b].deep-Node[lca].deep;//printf ("LCA (%d,%d) =%d\n", A,B,LCA);//printf ("Lca-deep =%d\n", node[lca].deep);//printf ("Disa =%d Disb =%d\n", DISA,DISB); if(A = = b) printf ("self\n"); Else if(Disa = = Disb && Disa = =1)///Sibling Relationship { if(Gender = ='F') printf ("sister\n"); Elseprintf"brother\n"); } Else if(LCA = = a)///Direct Relationship { if(Gender = ='F') TMP ="daughter"; ElseTMP ="son"; if(DISB = =1) cout<<tmp<<Endl; Else if(DISB = =2) cout<<"Grand"<<tmp<<Endl; Else if(DISB = =3) cout<<"Great-grand"<<tmp<<Endl; Else if(DISB = =4) cout<<"Great-great-grand"<<tmp<<Endl; Elseprintf"kin\n"); } Else if(LCA = =b) {if(Gender = ='F') TMP ="Mother"; ElseTMP ="Father"; if(Disa = =1) cout<<tmp<<Endl; Else if(Disa = =2) cout<<"Grand"<<tmp<<Endl; Else if(Disa = =3) cout<<"Great-grand"<<tmp<<Endl; Else if(Disa = =4) cout<<"Great-great-grand"<<tmp<<Endl; Elseprintf"kin\n"); } Else if(NODE[A].PA = = LCA && Node[a].deep < Node[b].deep)///nephew Relationship { if(Gender = ='F') TMP ="niece"; ElseTMP ="nephew"; if(DISB = =2) cout<<tmp<<Endl; Else if(DISB = =3) cout<<"Grand"<<tmp<<Endl; Else if(DISB = =4) cout<<"Great-grand"<<tmp<<Endl; Else if(DISB = =5) cout<<"Great-great-grand"<<tmp<<Endl; Elseprintf"kin\n"); } Else if(NODE[B].PA = = LCA && node[a].deep >node[b].deep) { if(Gender = ='F') TMP ="Aunt"; ElseTMP ="Uncle"; if(Disa = =2) cout<<tmp<<Endl; Else if(Disa = =3) cout<<"Grand"<<tmp<<Endl; Else if(Disa = =4) cout<<"Great-grand"<<tmp<<Endl; Else if(Disa = =5) cout<<"Great-great-grand"<<tmp<<Endl; Elseprintf"kin\n"); } Else if(Disa >=2&& DISB >=2)///Cousin Cousin{tmp="cousin"; intMax =Max (DISA,DISB); intMin =min (DISA,DISB); intCha = max-min;///This difference helps us determine the offspring . if(Min <=4&& Cha <=3) { if(Min = =2) cout<<"1st"<<tmp; Else if(Min = =3) cout<<"2nd"<<tmp; Else if(Min = =4) cout<<"3rd"<<tmp; if(Cha =0) cout<<Endl; Else if(Cha =1) cout<<"once removed"<<Endl; Else if(Cha =2) cout<<"twice removed"<<Endl; Else if(Cha =3) cout<<"Thrice removed"<<Endl; } Elsecout<<"Kin"<<Endl; } Elsecout<<"Kin"<<Endl; } return 0;}
Uvalive 7291Kinfolk (Recent public ancestor)