A - Yet Another AB Problem

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

A, B からなる長さ N の文字列 S,T が与えられます.S の左から i 番目の文字を S_i と表します.

あなたは以下の操作を好きな回数(0 回でもよい)繰り返すことができます.

  • 1\leq i < j \leq N を満たす整数 i,j を選ぶ. S_iA で, S_jB で置き換える.

ST に一致させることが可能か判定し,可能な場合必要な最小の操作回数を求めてください.

制約

  • 2 \leq N \leq 2 \times 10^5
  • S,TA, B からなる長さ N の文字列
  • 入力される数値は全て整数

入力

入力は以下の形式で標準入力から与えられる.

N
S
T

出力

ST に一致させることが不可能な場合 -1 を出力せよ.

一致させることが可能な場合,必要な最小の操作回数を出力せよ.


入力例 1

5
BAABA
AABAB

出力例 1

2

i=1,j=3 として操作を行うと SAABBA に変化します.

次に,i=4,j=5 として操作を行うと SAABAB に変化します.

よって 2 回の操作で ST と一致させることが可能です.また,これが必要な最小の操作回数であることが証明できるので答えは 2 です.


入力例 2

2
AB
BA

出力例 2

-1

何回操作を行っても ST と一致させることは不可能であることが証明できます.

Score: 400 points

Problem Statement

You are given two strings S and T of length N consisting of A and B. Let S_i denote the i-th character from the left of S.

You can repeat the following operation any number of times, possibly zero:

  • Choose integers i and j such that 1\leq i < j \leq N. Replace S_i with A and S_j with B.

Determine if it is possible to make S equal T. If it is possible, find the minimum number of operations required.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • Each of S and T is a string of length N consisting of A and B.
  • All input numbers are integers.

Input

The input is given from Standard Input in the following format:

N
S
T

Output

If it is impossible to make S equal T, print -1.

Otherwise, print the minimum number of operations required to do so.


Sample Input 1

5
BAABA
AABAB

Sample Output 1

2

Performing the operation with i=1 and j=3 changes S to AABBA.

Performing the operation with i=4 and j=5 changes S to AABAB.

Thus, you can make S equal T with two operations. It can be proved that this is the minimum number of operations required, so the answer is 2.


Sample Input 2

2
AB
BA

Sample Output 2

-1

It can be proved that no matter how many operations you perform, you cannot make S equal T.

B - Arithmetic Progression Subsequence

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

1 以上 \textbf{10} 以下の整数からなる長さ N の数列 A が与えられます.

1\leq l \leq r\leq N を満たす整数組 (l,r) であって,以下の条件を満たすものを良い組と呼びます.

  • 数列 (A_l,A_{l+1},\ldots,A_r) は長さ 3 の等差数列を(連続とは限らない)部分列として含む.より厳密には,l \leq i < j < k\leq r を満たす整数組 (i,j,k) であって, A_j - A_i = A_k - A_j なるものが存在する.

良い組の個数を求めてください.

制約

  • 3 \leq N \leq 10^5
  • 1\leq A_i \leq 10
  • 入力される数値は全て整数

入力

入力は以下の形式で標準入力から与えられる.

N
A_1 \ldots A_N

出力

答えを出力せよ.


入力例 1

5
5 3 4 1 5

出力例 1

3

良い組は (l,r)=(1,4),(1,5),(2,5)3 つです.

例えば,数列 (A_1,A_2,A_3,A_4)(5,3,1) という長さ 3 の等差数列を部分列として含むので (1,4) は良い組です.


入力例 2

3
1 2 1

出力例 2

0

良い組が存在しない場合もあります.


入力例 3

9
10 10 1 3 3 7 2 2 5

出力例 3

3

Score: 500 points

Problem Statement

You are given a sequence A of length N consisting of integers between 1 and \textbf{10}, inclusive.

A pair of integers (l,r) satisfying 1\leq l \leq r\leq N is called a good pair if it satisfies the following condition:

  • The sequence (A_l,A_{l+1},\ldots,A_r) contains a (possibly non-contiguous) arithmetic subsequence of length 3. More precisely, there is a triple of integers (i,j,k) with l \leq i < j < k\leq r such that A_j - A_i = A_k - A_j.

Find the number of good pairs.

Constraints

  • 3 \leq N \leq 10^5
  • 1\leq A_i \leq 10
  • All input numbers are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the answer.


Sample Input 1

5
5 3 4 1 5

Sample Output 1

3

There are three good pairs: (l,r)=(1,4),(1,5),(2,5).

For example, the sequence (A_1,A_2,A_3,A_4) contains an arithmetic subsequence of length 3, which is (5,3,1), so (1,4) is a good pair.


Sample Input 2

3
1 2 1

Sample Output 2

0

There may be cases where no good pairs exist.


Sample Input 3

9
10 10 1 3 3 7 2 2 5

Sample Output 3

3
C - Prefix Mex Sequence

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

有限個の非負整数からなる数列 X に対して,\mathrm{mex}(X)X に含まれない最小の非負整数と定義します.例えば,\mathrm{mex}((0,0, 1,3)) = 2, \mathrm{mex}(( 1) ) = 0, \mathrm{mex}(() ) = 0 です.

各要素が 0 または 1 である長さ N の数列 S=(S_1,\ldots,S_N) が与えられます.

0 以上 M 以下の整数からなる長さ N の数列 A=(A_1,A_2,\ldots,A_N) であって,以下の条件を満たすものの個数を 998244353 で割ったあまりを求めてください. 

  • i(1\leq i\leq N) について,S_i=1 ならば A_i = \mathrm{mex}((A_1,A_2,\ldots,A_{i-1}))S_i=0 ならば A_i \neq \mathrm{mex}((A_1,A_2,\ldots,A_{i-1}))

制約

  • 1 \leq N \leq 5000
  • 0\leq M\leq 10^9
  • S_i0 または 1
  • 入力される数値は全て整数

入力

入力は以下の形式で標準入力から与えられる.

N M
S_1 \ldots S_N

出力

答えを出力せよ.


入力例 1

4 2
1 0 0 1

出力例 1

4

条件を満たす数列は以下の 4 個です.

  • (0,0,0,1)
  • (0,0,2,1)
  • (0,2,0,1)
  • (0,2,2,1)

入力例 2

10 1000000000
0 0 1 0 0 0 1 0 1 0

出力例 2

587954969

個数を 998244353 で割ったあまりを求めることに注意してください.

Score: 600 points

Problem Statement

For a sequence X composed of a finite number of non-negative integers, we define \mathrm{mex}(X) as the smallest non-negative integer not in X. For example, \mathrm{mex}((0,0, 1,3)) = 2, \mathrm{mex}((1)) = 0, \mathrm{mex}(()) = 0.

You are given a sequence S=(S_1,\ldots,S_N) of length N where each element is 0 or 1.

Find the number, modulo 998244353, of sequences A=(A_1,A_2,\ldots,A_N) of length N consisting of integers between 0 and M, inclusive, that satisfy the following condition:

  • For each i (1\leq i\leq N), A_i = \mathrm{mex}((A_1,A_2,\ldots,A_{i-1})) if S_i=1, and A_i \neq \mathrm{mex}((A_1,A_2,\ldots,A_{i-1})) if S_i=0.

Constraints

  • 1 \leq N \leq 5000
  • 0 \leq M \leq 10^9
  • S_i is 0 or 1.
  • All input numbers are integers.

Input

The input is given from Standard Input in the following format:

N M
S_1 \ldots S_N

Output

Print the answer.


Sample Input 1

4 2
1 0 0 1

Sample Output 1

4

The following four sequences satisfy the conditions:

  • (0,0,0,1)
  • (0,0,2,1)
  • (0,2,0,1)
  • (0,2,2,1)

Sample Input 2

10 1000000000
0 0 1 0 0 0 1 0 1 0

Sample Output 2

587954969

Be sure to find the count modulo 998244353.

D - Triangle Card Game

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

Alice と Bob でゲームをします.

はじめ,Alice, Bob はそれぞれ N 枚のカードを持っていて,Alice が持っている i 番目のカードには整数 A_i が,Bob が持っている i 番目のカードには整数 B_i が書かれてます.

ゲームは以下の手順で行われます.

  • 何も書かれていない黒板を用意する.
  • Alice が持っているカードを一枚食べ,食べたカードに書かれた整数を黒板に書く.
  • 次に,Bob が持っているカードを一枚食べ,食べたカードに書かれた整数を黒板に書く.
  • 最後に,Alice が持っているカードを一枚食べ,食べたカードに書かれた整数を黒板に書く.

黒板に書かれた 3 個の整数を 3 辺の長さとする(非退化な)三角形が存在すれば Alice の勝ちで,そうでないとき Bob の勝ちです.

両者が最適な行動をするとき,どちらが勝つか判定してください.

T 個のテストケースが与えられるので,それぞれについて答えてください.

制約

  • 1 \leq T \leq 10^5
  • 2\leq N\leq 2\times 10^5
  • 1\leq A_i,B_i\leq 10^9
  • 入力される数値は全て整数
  • 1 つの入力に含まれるテストケースについて,N の総和は 2\times 10^5 以下

入力

入力は以下の形式で標準入力から与えられる.

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

各ケースは以下の形式で与えられる.

N
A_1 \ldots A_N
B_1 \ldots B_N

出力

T 行出力せよ.i 行目 (1 \leq i \leq T) には, i 番目のテストケースについて,Alice が勝つ場合 Alice を,Bob が勝つ場合 Bob を出力せよ.


入力例 1

3
3
1 2 3
4 5 6
4
6 1 5 10
2 2 4 5
10
3 1 4 1 5 9 2 6 5 3
2 7 1 8 2 8 1 8 2 8

出力例 1

Bob
Alice
Alice

1 番目のテストケースでは,例えばゲームは以下のように進行します.

  • Alice が 2 を書かれたカードを食べ,黒板に 2 を書く.
  • Bob が 4 を書かれたカードを食べ,黒板に 4 を書く.
  • Alice が 1 を書かれたカードを食べ,黒板に 1 を書く.
  • 黒板に書かれた数は 2,4,1 であり,3 辺の長さが 2,4,1 であるような三角形は存在しないので Bob の勝ちとなる.

このテストケースについて,上記の手順が必ずしも両者にとって最適な行動とは限りませんが,両者が最適な行動をした場合勝利するのは Bob であることが示せます.

Score: 700 points

Problem Statement

Alice and Bob will play a game.

Initially, Alice and Bob each have N cards, with the i-th card of Alice having the integer A_i written on it, and the i-th card of Bob having the integer B_i written on it.

The game proceeds as follows:

  • Prepare a blackboard with nothing written on it.
  • Alice eats one of her cards and writes the integer from the eaten card on the blackboard.
  • Next, Bob eats one of his cards and writes the integer from the eaten card on the blackboard.
  • Finally, Alice eats one more of her cards and writes the integer from the eaten card on the blackboard.

If it is possible to form a (non-degenerate) triangle with the side lengths of the three integers written on the blackboard, Alice wins; otherwise, Bob wins.

Determine who wins when both players act optimally.

Solve each of the T given test cases.

Constraints

  • 1 \leq T \leq 10^5
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq 10^9
  • All input values are integers.
  • The sum of N over all test cases in a single input is at most 2 \times 10^5.

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

Each case is given in the following format:

N
A_1 \ldots A_N
B_1 \ldots B_N

Output

Print T lines. The i-th line (1 \leq i \leq T) should contain Alice if Alice wins for the i-th test case, and Bob if Bob wins.


Sample Input 1

3
3
1 2 3
4 5 6
4
6 1 5 10
2 2 4 5
10
3 1 4 1 5 9 2 6 5 3
2 7 1 8 2 8 1 8 2 8

Sample Output 1

Bob
Alice
Alice

In the first test case, for example, the game could proceed as follows:

  • Alice eats the card with 2 written on it and writes 2 on the blackboard.
  • Bob eats the card with 4 written on it and writes 4 on the blackboard.
  • Alice eats the card with 1 written on it and writes 1 on the blackboard.
  • The numbers written on the blackboard are 2, 4, 1. There is no triangle with side lengths 2, 4, 1, so Bob wins.

For this test case, the above process is not necessarily optimal for the players, but it can be shown that Bob will win if both players act optimally.

E - BDFS

Time Limit: 8 sec / Memory Limit: 1024 MB

配点 : 800

問題文

整数 N,P が与えられます.

頂点に 1 から N の番号が付いた N 頂点 N 辺のグラフがあります.i 番目の辺は頂点 i と頂点 i+1 を双方向に結んでいます.ここで頂点 N+1 は頂点 1 を意味します.

以下のアルゴリズムを行い,長さ N の数列 D=(D_1,D_2,\ldots,D_N) を得ます.

  • 長さ N の整数列 DD=(D_1,\ldots,D_N)=(-1,\ldots,-1) で定める.また,数のペアの列 QQ=((1,0)) で定める.Q が空でない限り,以下の処理を繰り返す.

    • Q の先頭の要素を (v,d) とする.先頭の要素を削除する.
    • D_v = -1 の場合,D_v := d とし,頂点 v に隣接して D_x=-1 を満たすような各頂点 x に対して以下の処理を行う.ただし条件を満たす x が複数存在する場合,頂点番号の小さい順に処理を行う.

      1. 確率 \frac{P}{100}Q先頭(x,d+1) を追加する.
      2. Q の先頭への (x,d+1) の追加を行わなかった場合,Q末尾(x,d+1) を追加する.

最終的に得られる D の要素の総和の期待値を \text{mod } 998244353 で求めてください.

T 個のテストケースが与えられるので,それぞれについて答えてください.

期待値 \text{mod } 998244353 の定義 求める期待値は必ず有理数になることが証明できます. また,この問題の制約下では,その値を既約分数 \frac{P}{Q} で表したときに Q998244353 で割り切れないことが保証されます. このとき R\times Q \equiv P\pmod{998244353} を満たすような 0 以上 998244352 以下の整数 R が一意に定まります.この R を 答えてください.

制約

  • 1 \leq T \leq 10^4
  • 3 \leq N \leq 10^{18}
  • 1\leq P \leq 99
  • 入力される数値は全て整数

入力

入力は以下の形式で標準入力から与えられる.

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

各ケースは以下の形式で与えられる.

N P

出力

T 行出力せよ.i 行目 (1 \leq i \leq T) には, i 番目のテストケースの答えを出力せよ.


入力例 1

3
3 50
4 1
1000000000000000000 70

出力例 1

499122179
595552585
760296751

1 番目のテストケースでは,例えば以下のようにアルゴリズムは動きます.

  • はじめ,D=(-1,-1,-1), Q=((1,0)) である.Q の先頭の要素 (1,0) を削除する.
  • D_1 = -1 なので,D_1 := 0 とする.頂点 1 に隣接して D_x= -1 を満たすような頂点 x2,3 が考えられる.
  • Q の先頭に (2,1) を追加する.Q の末尾に (3,1) を追加する.Q=((2,1),(3,1)) となる.
  • Q の先頭の要素 (2,1) を削除する.
  • D_2 = -1 なので,D_2 := 1 とする.頂点 2 に隣接して D_x= -1 を満たすような頂点 x3 が考えられる.
  • Q の先頭に (3,2) を追加する.Q=((3,2),(3,1)) となる.
  • Q の先頭の要素 (3,2) を削除する.
  • D_3 = -1 なので,D_3 := 2 とする.頂点 3 に隣接して D_x= -1 を満たすような頂点 x は存在しないので何もしない.
  • Q の先頭の要素 (3,1) を削除する.
  • D_3 =2 なので何もしない.
  • Q が空になったので処理を終了する.

この場合,最終的に D=(0,1,2) が得られます.アルゴリズムが上記の動作をする確率は \frac{1}{8} であり,D の要素の総和の期待値は \frac{5}{2} です.

Score: 800 points

Problem Statement

You are given integers N and P.

There is a graph with N vertices and N edges, where each vertex is labeled 1 to N. The i-th edge connects vertices i and i+1 bidirectionally. Here, vertex N+1 refers to vertex 1.

Perform the following algorithm to obtain a sequence D=(D_1,D_2,\ldots,D_N) of length N:

  • Set an integer sequence D of length N to D=(D_1,\ldots,D_N)=(-1,\ldots,-1). Also, set a sequence Q of number pairs to Q=((1,0)). Repeat the following process while Q is not empty:

    • Let (v,d) be the first element of Q. Remove this element.
    • If D_v = -1, then set D_v := d, and for each vertex x adjacent to vertex v such that D_x=-1, perform the following process. If there are multiple such x that satisfy the condition, process them in ascending order of vertex number:

      1. With probability \frac{P}{100}, add (x,d+1) to the front of Q.
      2. If (x,d+1) was not added to the front of Q, add it to the end of Q.

Find the expected value of the sum of the elements of the final sequence D obtained, modulo 998244353.

Solve each of the T test cases given.

Definition of expected value \text{mod } 998244353 It can be proved that the expected value to be found is always a rational number. Furthermore, the constraints of this problem guarantee that if that value is expressed as an irreducible fraction \frac{P}{Q}, then Q is not divisible by 998244353. Here, there is a unique integer R between 0 and 998244352, inclusive, such that R\times Q \equiv P\pmod{998244353}. Provide this R as the answer.

Constraints

  • 1 \leq T \leq 10^4
  • 3 \leq N \leq 10^{18}
  • 1\leq P \leq 99
  • All input numbers are integers.

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

Each case is given in the following format:

N P

Output

Print T lines. The i-th line (1 \leq i \leq T) should contain the answer for the i-th test case.


Sample Input 1

3
3 50
4 1
1000000000000000000 70

Sample Output 1

499122179
595552585
760296751

In the first test case, the algorithm may operate as follows:

  • Initially, D=(-1,-1,-1) and Q=((1,0)). Remove the first element (1,0) from Q.
  • D_1 = -1, so set D_1 := 0. The vertices x adjacent to vertex 1 such that D_x= -1 are 2 and 3.
  • Add (2,1) to the front of Q. Add (3,1) to the end of Q. Now Q=((2,1),(3,1)).
  • Remove the first element (2,1) from Q.
  • D_2 = -1, so set D_2 := 1. The vertex x adjacent to vertex 2 such that D_x= -1 is 3.
  • Add (3,2) to the front of Q. Now Q=((3,2),(3,1)).
  • Remove the first element (3,2) from Q.
  • D_3 = -1, so set D_3 := 2. There are no vertices x adjacent to vertex 3 such that D_x= -1, so do nothing.
  • Remove the first element (3,1) from Q.
  • D_3 =2, so do nothing.
  • Q is now empty, so the process ends.

In this case, the final sequence obtained is D=(0,1,2). The probability that the algorithm operates as described above is \frac{1}{8}, and the expected sum of the elements of D is \frac{5}{2}.

F - Edge Deletion 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

頂点に 1 から N の番号が付いた N 頂点の木が与えられます.木の i 番目の辺は頂点 u_i と頂点 v_i を双方向に結んでいます.

(1,2,\ldots,N) の順列 P=(P_1,\ldots,P_N) に対し,数列 A(P) を以下で定義します.

  • A(P) は初め空である.全ての頂点 iP_i を書き込む.
  • i=1,2,\ldots,N の順に以下を行う.
    • 頂点 i が孤立点の場合,0A(P) の末尾に追加する.そうでない場合,頂点 i に隣接する頂点であって,書かれている整数が最も小さいものを選ぶ.選んだ頂点に書かれた整数を A(P) の末尾に追加し,頂点 i と選んだ頂点を結ぶ辺を削除する.

A(P) として考えられる数列のうち,辞書順最小のものを求めてください.

T 個のテストケースが与えられるので,それぞれについて答えてください.

制約

  • 1 \leq T \leq 10^5
  • 2 \leq N \leq 2 \times 10^5
  • 1\leq u_i,v_i\leq N
  • 与えられるグラフは木である
  • 入力される数値は全て整数
  • 1 つの入力に含まれるテストケースについて,N の総和は 2\times 10^5 以下

入力

入力は以下の形式で標準入力から与えられる.

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

各ケースは以下の形式で与えられる.

N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

出力

T 行出力せよ.i 行目 (1 \leq i \leq T) には, i 番目のテストケースの答えを出力せよ.


入力例 1

3
5
1 2
2 3
2 4
4 5
8
8 6
7 2
2 1
3 7
5 6
1 6
4 3
7
7 1
5 2
1 2
6 5
4 1
5 3

出力例 1

1 2 0 1 3
1 2 2 3 1 4 0 0
1 2 2 0 3 0 4

1 番目のテストケースでは,P=(4,1,2,3,5) に対し,A(P)=(1,2,0,1,3) は以下の方法で得られます.

  • 頂点 1 に隣接する頂点であって,書かれている整数が最も小さいものは頂点 2 である.A(P) の末尾に P_2=1 を追加し,頂点 1 と頂点 2 を結ぶ辺を削除する.

  • 頂点 2 に隣接する頂点であって,書かれている整数が最も小さいものは頂点 3 である.A(P) の末尾に P_3=2 を追加し,頂点 2 と頂点 3 を結ぶ辺を削除する.

  • 頂点 3 は孤立点なので,A(P) の末尾に 0 を追加する.

  • 頂点 4 に隣接する頂点であって,書かれている整数が最も小さいものは頂点 2 である.A(P) の末尾に P_2=1 を追加し,頂点 4 と頂点 2 を結ぶ辺を削除する.

  • 頂点 5 に隣接する頂点であって,書かれている整数が最も小さいものは頂点 4 である.A(P) の末尾に P_4=3 を追加し,頂点 5 と頂点 4 を結ぶ辺を削除する.

これが A(P) として考えられる数列のうち,辞書順最小であることが証明できます.

Score: 1000 points

Problem Statement

You are given a tree with N vertices numbered 1 to N. The i-th edge of the tree connects vertices u_i and v_i bidirectionally.

For a permutation P=(P_1,\ldots,P_N) of (1,2,\ldots,N), we define the sequence A(P) as follows:

  • A(P) is initially empty. Write P_i on each vertex i.
  • For i=1,2,\ldots,N in this order, perform the following:
    • If vertex i is an isolated vertex, append 0 to the end of A(P). Otherwise, select the adjacent vertex with the smallest written integer. Append the integer written on the selected vertex to the end of A(P) and remove the edge connecting vertex i and the selected vertex.

Find the lexicographically smallest sequence among all possible A(P).

Solve each of the T given test cases.

Constraints

  • 1 \leq T \leq 10^5
  • 2 \leq N \leq 2 \times 10^5
  • 1\leq u_i,v_i\leq N
  • The given graph is a tree.
  • All input numbers are integers.
  • The sum of N over all test cases in a single input is at most 2 \times 10^5.

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

Each case is given in the following format:

N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

Output

Print T lines. The i-th line (1 \leq i \leq T) should contain the answer for the i-th test case.


Sample Input 1

3
5
1 2
2 3
2 4
4 5
8
8 6
7 2
2 1
3 7
5 6
1 6
4 3
7
7 1
5 2
1 2
6 5
4 1
5 3

Sample Output 1

1 2 0 1 3
1 2 2 3 1 4 0 0
1 2 2 0 3 0 4

In the first test case, for P=(4,1,2,3,5), one can obtain A(P)=(1,2,0,1,3) as follows:

  • The vertex adjacent to vertex 1 with the smallest written integer is vertex 2. Append P_2=1 to the end of A(P) and remove the edge connecting vertices 1 and 2.

  • The vertex adjacent to vertex 2 with the smallest written integer is vertex 3. Append P_3=2 to the end of A(P) and remove the edge connecting vertices 2 and 3.

  • Vertex 3 is an isolated vertex, so append 0 to the end of A(P).

  • The vertex adjacent to vertex 4 with the smallest written integer is vertex 2. Append P_2=1 to the end of A(P) and remove the edge connecting vertices 4 and 2.

  • The vertex adjacent to vertex 5 with the smallest written integer is vertex 4. Append P_4=3 to the end of A(P) and remove the edge connecting vertices 5 and 4.

It can be proved that this is the lexicographically smallest sequence among all possible A(P).