Fzu 2,057 Family Spectrum (DFS)

Source: Internet
Author: User

problem 2,057 Family Spectraaccept:135 submit:364
Time limit:1000 mSec Memory limit:32768 KB problem description As a result of the implementation of family planning, we and future generations will be only children, that is, every couple will have only one child. So give you xxxx year someone's tree genealogy, can you point out any two of them relationship? Input

Input data The first line of an integer t indicates that there is a T group of data.

The first row of each group of data is a positive integer n (2 < n < 10000, and n is odd), indicating that there are n family members in the genealogy.

Next N/2 line, three integers a B c per line, indicates that A's father is B,a's mother is c. The data guarantee gives a tree, and the family member is numbered 1 to N.

The next line is a positive integer m (0 < M < 100), which indicates a M inquiry.

The next M-line, two integers x y (x!=y) per line, indicates the relationship that asks X Y.

Output

For each query, output a line.

If X is the ancestor of Y, the output is:

1 2

If Y is the ancestor of X, the output is:

0 2

If none of the above is the case, the output is:

Relative

In the first two cases, S represents a string consisting of an uppercase letter F and M, F for the father, and M for the mother, which indicates that the former is the latter's XXX. For example:

0 FMM says X is the mother of the father of Y's mother.

1 MFMF says Y is the mother's father of X's mother's father.

And so on

Sample Input193 6 8 2 4 538 9 Sample Output0 MF1 mmrelative SOURCEFOJ Award Month-November 2011

DFS Plus path printing

#include <cstdio>#include<iostream>#include<cstring>#include<cmath>#include<queue>#include<stdlib.h>#include<algorithm>using namespacestd;Const intmaxn=10000+5;//1 Father 2 motherstructnode{intL,r;} A[MAXN];intStr[maxn],lenth;voidPrintintstr[]) {     for(intI=0; i<lenth;i++)    {        if(str[i]==1) printf ("F"); if(str[i]==2) printf ("M"); }}BOOLDFS (intCurintEintCNT) {    if(cur==e) {lenth=cnt;return true;} Else if(a[cur].l==0&& a[cur].r==0)return false; Else{str[cnt]=1; if(DFS (a[cur].l,e,cnt+1))return true; STR[CNT]=2; if(DFS (a[cur].r,e,cnt+1))return true; return false; }}intMain () {intkase,n,num,m,x,y,sx; BOOLFlag; CIN>>Kase;  while(kase--) {memset (A,0,sizeof(a)); memset (str,0,sizeof(str)); scanf ("%d",&N);  for(intI=1; i<=n/2; i++) {scanf ("%d",&num); scanf ("%d%d",&a[num].l,&A[NUM].R); } scanf ("%d",&m);  for(intI=1; i<=m;i++) {scanf ("%d%d",&x,&y); if(DFS (Y,x,0) {printf ("0");            Print (str); }            Else if(DFS (x, Y,0) {printf ("1");            Print (str); }            Elseprintf ("Relative"); printf ("\ n"); memset (str,0,sizeof(str)); }    }    return 0;}
View Code

Fzu 2,057 Family Spectrum (DFS)

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.