atcoder#ABC285D. [ABC285D] Change Usernames
[ABC285D] Change Usernames
Score : points
Problem Statement
You run a web service with users.
The -th user with a current handle wants to change it to . Here, , and are pairwise distinct, and so are , and .
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
- and are strings of length between and (inclusive) consisting of lowercase English letters.
- are pairwise distinct.
- are pairwise distinct.
Input
The input is given from Standard Input in the following format:
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 -st user with a current handle b
wants to change it to m
.
The -nd user with a current handle m
wants to change it to d
.
First, you change the -nd user's handle from m
to d
;
then you change the -st user's handle from b
to m
. This way, you can achieve the objective.
Note that you cannot change the -st user's handle to m
at first, because it is used by the -nd user at that point.
3
a b
b c
c a
No
The -st user with a current handle a
wants to change it to b
.
The -nd user with a current handle b
wants to change it to c
.
The -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