[TOJ 3660] family relationship (map ing and query set), toj3660
Description
Given the relationship between several family members, determine whether two people belong to the same family. That is, two people can directly or indirectly contact each other through these relationships.
Input
There are multiple groups of input data. The first behavior of each group of data is a positive integer n (1 <= n <= 100), indicating that there are 100 link descriptions, followed by n rows, the description of each line is as follows:
P1 p2 c
P1, p2, and c are a string of texts, indicating the name of each person. p1 and p2 are the father and mother of c.
The last line contains two strings a and B, which are the names of the two persons to be judged.
Each person's name consists of uppercase and lowercase letters and cannot exceed 80 characters in length.
If n is 0, the input ends.
Output
If a and B are in the same family, Yes is output.
Otherwise, No
Sample Input
2
Barbara Bill Ted
Nancy Ted John
John Barbara
3
Lois Frank Jack
Florence Bill Fred
Annie Fred James
James Jack
0
Sample output
Yes
No
# Include <iostream> # include <algorithm> # include <map> using namespace std; int p [10005]; int find (int r) {if (p [r]! = R) p [r] = find (p [r]); return p [r];} int join (int x, int y) {int fx = find (x), fy = find (y); if (fx! = Fy) p [fx] = fy;} int main () {string a, B, c; int n; while (cin> n, n) {for (int I = 0; I <= 10000; I ++) p [I] = I; map <string, int> m; int cnt = 1; while (n --) {cin> a> B> c; if (m [a] = 0) m [a] = cnt ++; // ing, if this name does not appear, it is mapped to 1, 2, 3, 4 ...... If (m [B] = 0) m [B] = cnt ++; if (m [c] = 0) m [c] = cnt ++; join (m [a], m [B]); join (m [a], m [c]);}/* for (int I = 1; I <cnt; I ++) // cout <p [I] <""; cout <endl; for (int I = 1; I <cnt; I ++) cout <find (p [I]) <""; cout <endl; */cin> a> B; if (m [a]! = 0 & m [B]! = 0 & find (m [a]) = find (m [B]) // cout <"Yes" <endl; else cout <"No" <endl;} return 0 ;}