atcoder#ABC285D. [ABC285D] Change Usernames

[ABC285D] Change Usernames

Score : 400400 points

Problem Statement

You run a web service with NN users.

The ii-th user with a current handle SiS_i wants to change it to TiT_i. Here, S1,S_1,\ldots, and SNS_N are pairwise distinct, and so are T1,T_1,\ldots, and TNT_N.

Determine if there is an appropriate order to change their handles to fulfill all of their requests subject to the following conditions:

  • you change only one user's handle at a time;
  • you change each user's handle only once;
  • when changing the handle, the new handle should not be used by other users at that point.

Constraints

  • 1N1051 \leq N \leq 10^5
  • SiS_i and TiT_i are strings of length between 11 and 88 (inclusive) consisting of lowercase English letters.
  • SiTiS_i \neq T_i
  • SiS_i are pairwise distinct.
  • TiT_i are pairwise distinct.

Input

The input is given from Standard Input in the following format:

NN

S1S_1 T1T_1

S2S_2 T2T_2

\vdots

SNS_N TNT_N

Output

Print Yes if they can change their handles to fulfill all of their requests subject to the conditions; print No otherwise.

2
b m
m d
Yes

The 11-st user with a current handle b wants to change it to m. The 22-nd user with a current handle m wants to change it to d.

First, you change the 22-nd user's handle from m to d; then you change the 11-st user's handle from b to m. This way, you can achieve the objective.

Note that you cannot change the 11-st user's handle to m at first, because it is used by the 22-nd user at that point.

3
a b
b c
c a
No

The 11-st user with a current handle a wants to change it to b. The 22-nd user with a current handle b wants to change it to c. The 33-rd user with a current handle c wants to change it to a.

We cannot change their handles subject to the conditions.

5
aaa bbb
yyy zzz
ccc ddd
xxx yyy
bbb ccc
Yes