At the beginning of this topic I thought it was a collection, and later found more simple than the collection, is a family tree, and then find the depth difference between two nodes.
#include <stdio.h>int data[30]; void Make_set () {for (int i=0;i<30;i++) data[i]=-1;} int find (int a,int b) {int re=1; while (Data[a]!=-1) {if (data[a]==b) return re; A=data[a]; re++; } return-1;} int main () {int n,m; Char a[4]; while (scanf ("%d%d", &n,&m)} {if (m==0&&n==0) break; Make_set (); for (int i=0;i<n;i++) {scanf ("%s", a); data[a[1]-' A ']=data[a[2]-' a ']=a[0]-' a '; } for (int i=0;i<m;i++) {scanf ("%s", a); int X=find (a[0]-' A ', a[1]-' a '); int Y=find (a[1]-' A ', a[0]-' a '); int re = x>y?x:y; if (re<=0) {printf ("-\n"); } else{if (x>0) {if (re==1) printf ("parent\n"); else{for (int i=2;i<re;i++) {printf ("great-"); } printf ("grandparent\n"); }} else if (y>0) {if (re==1) printf ("child\n"); else{for (int i=2;i<re;i++) {printf ("great-"); } printf ("grandchild\n"); }}}}} return 0;}
Nine degrees 1035-tree-Find immediate family