fzu2057--dfs--genealogy

Source: Internet
Author: User
Tags uppercase letter

Because 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 Input

1 9 3 6 7 5 8 9 1 2 3 2 4 5 3 8 2 1 7 3 9

Sample Output

0 MF 1 MM Relative
/* Pro Table Head: Node number E: The number of the edge T[i].next: The number of edges of all connected points with the node U record status with path at DFS */#include <cstdio> #include <cstring># Include<algorithm> #include <vector>using namespace std;const int MAX = 10000 + 10;int Head[max];int Path[MAX] ; int E, ans;int g[max];struct edge{int V, W, next;}     t[max];void Add (int u, int v, int w) {t[e].v = v;    T[E].W = W;    T[e].next = Head[u];   Head[u] = e++;}    int dfs (int u, int fd, int step) {if (U = = fd) {ans = step;    for (int i = 0; i < step; i++) g[i] = Path[i];    return true;        } for (int i = head[u]; i =-1; i = t[i].next) {int v = T[I].V;        Path[step] = T[I].W;        if (Dfs (v, FD, step + 1)) return true; } return false;}    int main () {int n, m, A, B, C;int t;scanf ("%d", &t), while (t--) {memset (head,-1, sizeof (head));    memset (t, 0, sizeof (t));     scanf ("%d", &n);    E = 0;        for (int i = 1; I <= n/2; i++) {scanf ("%d%d%d", &a, &b, &c); Add (A, B,0);    Add (A, C, 1);    } scanf ("%d", &m);    int x, y;        for (int i = 1; I <= m; i++) {scanf ("%d%d", &x, &y);            if (Dfs (x, y, 0)) {printf ("1"); for (int j = 0; J < ans; J + +) {printf ("%c", G[j]?            ' M ': ' F ');            }} else if (Dfs (y, x, 0)) {printf ("0"); for (int j = 0; J < ans; J + +) {printf ("%c", G[j]?            ' M ': ' F ');        }} else {printf ("Relative");    } puts ("");         }} return 0;}

  

fzu2057--dfs--genealogy

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.