luogu#P16377. [NordicOI 2026] Name Change

[NordicOI 2026] Name Change

Problem Description

Your friend's name is currently the string SS. They wish to change their name to TT. 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 MM pairs (i,j)(i,j) and are able to swap characters at positions ii and jj (11-indexed). They are able to do this an arbitrary number of times.

They now wonder whether it is possible to transform SS to TT by performing some sequence of swaps.

Input Format

The first line contains two integers NN and M(1N,M2105)M\,(1 \leq N, M \leq 2 \cdot 10^5), the length of SS and TT, and the number of swaps that are available.

The second line of input contains the string SS. SS consists of exactly NN lowercase English characters a-z\texttt{a-z}.

The third line of input contains the string TT. TT consists of exactly NN lowercase English characters a-z\texttt{a-z}.

The final MM lines each contain the pair of integers i,j(1i<jN)i,j\,(1 \leq i < j \leq N), meaning that your friend is able to swap the characters at positions ii and jj. Each pair of indices (i,j)(i,j) shows up at most once in the input.

Output Format

If SS can be transformed into TT 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
11 55 N,M100N,M \le 100, j=i+1j=i+1 for all swaps (i,j)(i,j) and TT is sorted[1].
22 1414 j=i+1j=i+1 for all swaps (i,j)(i,j).
33 1010 N,M100N,M \le 100, TT is sorted and if swaps (i,j)(i,j) and (j,k)(j,k) are permitted, then the swap (i,k)(i,k) is also permitted[2].
44 1111 N,M2000N,M \le 2000, TT is sorted and if swaps (i,j)(i,j) and (j,k)(j,k) are permitted, then the swap (i,k)(i,k) is also permitted.
55 1717 N,M2000N,M \le 2000, if swaps (i,j)(i,j) and (j,k)(j,k) are permitted, then the swap (i,k)(i,k) is also permitted.
66 1818 If swaps (i,j)(i,j) and (j,k)(j,k) are permitted, then the swap (i,k)(i,k) is also permitted.
77 N,M2000N,M \le 2000
88 TT is sorted.
99 1010 No additional constraints.

Explanation of Samples

In sample 11, the letters at positions 22 and 33 of abc\texttt{abc} can be swapped to obtain acb\texttt{acb}. Thus, the answer is Yes. This sample satisfies the constraint that j=i+1j=i+1 for all swaps, so it could appear in subtask 22. It does not satisfy that TT is sorted, so it could not appear in subtask 11.

In sample 22, one sequence of swaps to go from nordic\texttt{nordic} to cdinor\texttt{cdinor} is the following

Therefore, the answer is Yes. Note that in this sample, TT is sorted, and it could thus appear in subtasks 77, 88 and 99. It does not satisfy enough constraints to appear in other subtasks.

In sample 33, it is impossible to go from SS to TT. One way to see this is that the sixth letter is not part of any swap, and there is a mismatch: SS has an s\texttt{s} in this position and TT has a t\texttt{t}. Thus, no matter which swaps are made, the letters at this position will always differ. This sample satisfies the constraint that if swaps (i,j)(i,j) and (j,k)(j,k) are permitted, then the swap (i,k)(i,k) is also permitted. Thus, it could appear in subtasks 55, 66, 77 and 99 (not 33 and 44, as TT is not sorted).


  1. 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. ↩︎

  2. Note that this condition applies even to indirect swaps: if two indices ss and tt can be swapped via an indirect sequence of swaps (s,a),(a,b),,(f,t)(s,a), (a,b), \dots , (f, t), then the direct swap (s,t)(s,t) must also be permitted. ↩︎