P2256 No. 1 High School Sports Meet: 100 m Sprint

Background

Amid a crowd of couples showing PDA, Su the "xue shen" strides with firm (?) steps toward the $100$ m starting line. He notices that there are too many participants, and even the PE teacher is overwhelmed. The PE teacher spots Su, who is the class sports rep, and asks him for help. However, Su needs to warm up, or he might cramp halfway through the run. So he turns to you. If you help the teacher solve the problem, the teacher will give you $5$ points.

Description

Suppose there are $N$ ($2\leq N\leq 2\times 10^4$) competitors. The teacher will tell you the names of these $N$ competitors. Then the teacher will say $M$ ($1\leq M\leq 10^6$) sentences, each stating that student A and student B are in the same group. If student A and student B are in the same group, and student B and student C are also in the same group, then student A and student C are in the same group. After that, the teacher will ask you $K$ ($1\leq K\leq 10^6$) questions, each asking whether student X and student Y are in the same group. If they are, output `Yes.`; otherwise, output `No.`.

Input Format

The first line contains $N$ and $M$. The next $N$ lines each contain the name of one student. The next $M$ lines each contain two names, both guaranteed to have appeared in the above $N$ lines, indicating that these two competitors are in the same group. Then an integer $K$ is given. Then follow $K$ queries. Each query consists of two names, asking whether the two students are in the same group.

Output Format

For each of the teacher’s $K$ queries, output `Yes.` or `No.`.

Explanation/Hint

Translated by ChatGPT 5