Home > Back-end >  Cattle from zhejiang university machine test: find immediate relatives. Program segment error, ask f
Cattle from zhejiang university machine test: find immediate relatives. Program segment error, ask f

Time:10-02

 # define _CRT_SECURE_NO_WARNINGS 
# include
# include

using namespace std;

Const int MAXN=32;
Int child [MAXN];

Void Initial () {
for (int i=0; I The child [I]=I;
}
}

Bool FindChild (int x, int y, int & generation) {
While (x!=y && x! Child [x])={
Generation++;
X=child [x];
}
If (x==y) {
return true;
}
The else {
return false;
}
}

Int main () {
Int n, m;
String STR.
While (the scanf (" % d % d ", & n, & m)! EOF)={
Initial ();
for (int i=0; I Cin> STR.
The child [STR [1] - 'A']=STR [0] - 'A';
The child [STR [2] - 'A']=STR [0] - 'A';
}
While (m -) {
Cin> STR.
Int a=STR [0] - 'a';
Int b=STR [1] - 'A';
Int the g1=0, g2=0;
If (FindChild (a, b, g1)) {
If (g1==1) {
Printf (" parent "\ n");
}
The else {
While (g1> 2) {
Printf (" great - ");
G1 -;
}
Printf (" grandparent \ n ");
}
}
Else if (FindChild (b, a, g2)) {
If (g2==1) {
Printf (" child \ n ");
}
The else {
While (g2> 2) {
Printf (" great - ");
G2 -;
}
Printf (" grandchild \ n ");
}
}
The else {
Printf (" \ n ");
}
}
}
return 0;
}
  • Related