#P1407. [国家集训队] 稳定婚姻
[国家集训队] 稳定婚姻
Description
We know the marital status of couples. Denote the man of the -th couple as and the woman as . If some man and some woman once dated (whether in college, high school, or even kindergarten, with ), then when either party has problems with his or her spouse (that is, with or with ), they may elope. Suppose and his spouse do not get along, then and rekindle their relationship, and then , feeling upset after being cheated on, contacts his first love ... A chain of divorces follows like dominoes. If, under the premise that and divorce, these people can still eventually be paired into couples, then we call marriage unsafe; otherwise, marriage is safe.
Given the required information, your task is to determine whether each marriage is safe.
Input Format
The first line contains a positive integer , the number of couples.
Each of the following lines contains two strings, the names of these couples (female first, then male), separated by a space.
The -th line contains a positive integer , the number of couples who once dated each other.
Each of the following lines contains two strings, the names of these couples who once dated each other (female first, then male), separated by a space.
Output Format
The output contains lines. The -th line is Safe (if marriage is safe) or Unsafe (if marriage is unsafe).
2
Melanie Ashley
Scarlett Charles
1
Scarlett Ashley
Safe
Safe
2
Melanie Ashley
Scarlett Charles
2
Scarlett Ashley
Melanie Charles
Unsafe
Unsafe
Hint
For of the testdata, .
For of the testdata, , .
For of the testdata, all name strings contain only English letters, are case-sensitive, and have length no greater than . Each relationship appears only once in the input. The last lines of the input file will not contain any names that did not appear before. All names are distinct. , .
Translated by ChatGPT 5
京公网安备 11011102002149号