luogu#P16377. [NordicOI 2026] Name Change
[NordicOI 2026] Name Change
Problem Description
Your friend's name is currently the string . They wish to change their name to . Unfortunately, they live in a country where you aren't allowed to change your name. Such trivial issues won't stop them: they have done some "investigative" work on the government's website and found a way to switch the letters at some pairs of positions. More exactly, they have found pairs and are able to swap characters at positions and (-indexed). They are able to do this an arbitrary number of times.
They now wonder whether it is possible to transform to by performing some sequence of swaps.
Input Format
The first line contains two integers and , the length of and , and the number of swaps that are available.
The second line of input contains the string . consists of exactly lowercase English characters .
The third line of input contains the string . consists of exactly lowercase English characters .
The final lines each contain the pair of integers , meaning that your friend is able to swap the characters at positions and . Each pair of indices shows up at most once in the input.
Output Format
If can be transformed into using some sequence of swaps, print Yes. Otherwise, print No.
3 1
abc
acb
2 3
Yes
6 6
nordic
cdinor
1 6
2 6
2 3
3 5
4 5
1 4
Yes
6 6
kattis
sikatt
1 3
1 4
1 5
3 4
3 5
4 5
No
Hint
Scoring
Your solution will be tested on a set of test groups. To earn points for a group, you must pass all test cases in that group.
| Group | Points | Constraints |
|---|---|---|
| , for all swaps and is sorted[1]. | ||
| for all swaps . | ||
| , is sorted and if swaps and are permitted, then the swap is also permitted[2]. | ||
| , is sorted and if swaps and are permitted, then the swap is also permitted. | ||
| , if swaps and are permitted, then the swap is also permitted. | ||
| If swaps and are permitted, then the swap is also permitted. | ||
| is sorted. | ||
| No additional constraints. | ||
Explanation of Samples
In sample , the letters at positions and of can be swapped to obtain . Thus, the answer is Yes. This sample satisfies the constraint that for all swaps, so it could appear in subtask . It does not satisfy that is sorted, so it could not appear in subtask .
In sample , one sequence of swaps to go from to is the following

Therefore, the answer is Yes. Note that in this sample, is sorted, and it could thus appear in subtasks , and . It does not satisfy enough constraints to appear in other subtasks.
In sample , it is impossible to go from to . One way to see this is that the sixth letter is not part of any swap, and there is a mismatch: has an in this position and has a . Thus, no matter which swaps are made, the letters at this position will always differ. This sample satisfies the constraint that if swaps and are permitted, then the swap is also permitted. Thus, it could appear in subtasks , , and (not and , as is not sorted).
A string is considered sorted if there is no pair of adjacent letters where the left one comes later in the alphabet than the right one. ↩︎
Note that this condition applies even to indirect swaps: if two indices and can be swapped via an indirect sequence of swaps , then the direct swap must also be permitted. ↩︎