Nine degrees 1035-tree-Find immediate family

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.