#DSU0000031. Đổi tên tài khoản (Change Usernames)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đổi tên tài khoản (Change Usernames)
Change Usernames
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
There are users of an online service. User currently owns username and wants to change it to . 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 to . 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 users successfully obtain their desired names.
Input
The first line contains . Each of the following lines contains strings .
Output
Print Yes if a valid order exists; otherwise print No.
Subtasks
- Subtask 1 (20 points): ; ; usernames contain only
a–z, ; initial usernames are distinct and target usernames are distinct. - Subtask 2 (30 points): ; ; usernames contain only
a–z, ; initial usernames are distinct and target usernames are distinct. - Subtask 3 (50 points): ; ; usernames contain only
a–z, ; 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.