PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000031. Đổi tên tài khoản (Change Usernames)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đổi tên tài khoản (Change Usernames)

Change Usernames

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

There are NN users of an online service. User ii currently owns username SiS_i and wants to change it to TiT_i. All current usernames are distinct, all target usernames are distinct, and every user wants a name different from their current one.

The service processes username changes one at a time, not simultaneously. Every user must change their name exactly once, directly from SiS_i to TiT_i. A change is allowed only if its target name is not held by any user at that moment. Temporary usernames and multiple changes by the same user are not allowed.

Determine whether the requests can be ordered so that all NN users successfully obtain their desired names.

Input

The first line contains NN. Each of the following NN lines contains strings Si,TiS_i,T_i.

Output

Print Yes if a valid order exists; otherwise print No.

Subtasks

  • Subtask 1 (20 points): 1≤N≤301\le N\le30; 1≤∣Si∣,∣Ti∣≤81\le |S_i|,|T_i|\le8; usernames contain only a–z, Si≠TiS_i\ne T_i; initial usernames are distinct and target usernames are distinct.
  • Subtask 2 (30 points): 1≤N≤50001\le N\le5000; 1≤∣Si∣,∣Ti∣≤81\le |S_i|,|T_i|\le8; usernames contain only a–z, Si≠TiS_i\ne T_i; initial usernames are distinct and target usernames are distinct.
  • Subtask 3 (50 points): 1≤N≤1051\le N\le10^5; 1≤∣Si∣,∣Ti∣≤81\le |S_i|,|T_i|\le8; usernames contain only a–z, Si≠TiS_i\ne T_i; initial usernames are distinct and target usernames are distinct.

Examples

Input

3
a b
b c
c a

Output

No

Explanation

The three desired names form a dependency cycle: each name must be released by another participant in the cycle, so none can change first.