Implementation
1 |
|
MakeSet
1 | def makeSet(a, n): |
Find
1 | def find(i): |
Union
1 | def union(x, y): |
Example problem
Description
If a family is too large, it’s really not easy to judge whether two people are relatives. Give a relatives diagram and find out whether any given two people have relatives. Provisions: X and y are relatives, y and Z are relatives, then x and Z are relatives. If X and y are relatives, then x’s relatives are relatives of y, and y’s relatives are relatives of X.
Input
The first line: three integers n, m, p, (n <= 5000, m <= 5000, P <= 5000), respectively, denote n individuals and m relatives, and ask P about their relatives.
The following M lines: Mi, Mj, 1 <= Mi, Mj <= N, two numbers per line, indicating that Mi and Mj are related.
Next, line p: Pi and Pj are two numbers per line. Ask if Pi and Pj are related.
Output
P lines, one’Yes’or’No’ per line. The answer to the $i^{th}$ question is “have” or “don’t have” relatives.