2436. Are you my friend?

单点时限: 3.0 sec

内存限制: 256 MB

How many friends do you have? This is a great problem for us. At first, we have to discuss about definition of the friend. One of the friend’s definition is defined as some two persons have a nodding acquaintance with each other. Another friend’s definition is defined as some two persons goes to play together. On the other hand, the friend is often used in the relation of countries like United States and friends. Hmmm, it’s difficult. So we make a definition about friend between two persons.

  • This two persons are friends if a person knows another person.

  • Consider three persons A, B, C. A and C are friends if A and B are friends and if B and C are friends.

However, the situation where you cannot understand whether some person is friend often occurred. Because this definition makes a lot of friends. So you have to make a program to judge some two persons are friends or not.

输入格式

Input consists from multiple test cases. The first line of each test case contains single positive integer n less than or equal to 500. Following n lines contains a person’s name. The length of a person’s name is always less than 30. The next line contains single positive integer m less than or equal to 5000. Following m lines contains two names separated by a single space which indicates these two persons knows each other. The next line contains single positive integer q less than or equal to 2000. Following q lines contains two names separated by a single space. You have to judge about these two persons are friends or not. Input is terminated by EOF.

You can assume that all name consists from alpha-numeric character.

输出格式

For each test case, you are to print “Case X:” where X is the number of the test case starting with 1 for the first test case and incrementing by 1 for successive test case. For each query, you should output “Yes” if given two persons are friends, otherwise output “No”. You should output a blank line between two test cases.

样例

Input
5
A
B
C
D
E
3
A B
B C
D E
4
A B
A C
E D
A E
Output
Case 1:
Yes
Yes
Yes
No

17 人解决,39 人已尝试。

18 份提交通过,共有 121 份提交。

6.6 EMB 奖励。

创建: 15 年,11 月前.

修改: 7 年,2 月前.

最后提交: 3 年,11 月前.

来源: ECNU 选拔 2008

题目标签