[TOJ 3660] family relationship (map ing and query set), toj3660

Source: Internet
Author: User

[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 ;}

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.