Under construction

Still being built. Things may change

Grammy Sorting

Time limit1sMemory limit1024 MB

Summary
Given a connected graph with a permutation on vertices, use at most 10000 path rotations from A to make every vertex lie on a strictly increasing A-to-B path, or report -1.
Level

Hard10 of 10

Topics
Graph, Simulation, Implementation
Solved
No attempts yet

Problem

Grammy has a connected undirected graph GG with nn vertices numbered 1,2,…,n1, 2, \ldots, n. Among them are two special vertices AA and BB. Each vertex ii has a number p_ip\_i written on it, where p_1,p_2,…,p_np\_1, p\_2, \ldots, p\_n is a permutation of 1,2,…,n1, 2, \ldots, n.

Grammy thinks the numbers on the vertices are too chaotic. She wants to reorder them so that, for each vertex xx, there is a path that satisfies all of the following:

  • The path starts at AA and ends at BB.
  • The path contains vertex xx.
  • The numbers along the path are strictly increasing.

In each operation, Grammy can only choose a simple path that starts at AA and ends at an arbitrary vertex. She then shifts the numbers on that path one position toward the start and moves the first number to the last position. Formally, if the vertices on the chosen simple path hold numbers a1,a2,…,ak−1,aka_1, a_2, \ldots, a_{k-1}, a_k from start to end, then after the operation they hold a2,a3,…,ak,a1a_2, a_3, \ldots, a_k, a_1.

Grammy can perform no more than 10 00010\,000 operations. Determine whether she can reorder the numbers as required. If she can, output a solution.

Input

The first line contains four integers nn, mm, AA, BB (2≤n≤10002 \leq n \leq 1000, 1≤m≤20001 \leq m \leq 2000, 1≤A,B≤n1 \leq A, B \leq n, A≠BA \neq B).

The second line contains nn integers p_1,p_2,…,p_np\_1, p\_2, \ldots, p\_n (1≤p_i≤n1 \leq p\_i \leq n). It is guaranteed that p_1,p_2,…,p_np\_1, p\_2, \ldots, p\_n is a permutation.

Each of the next mm lines contains two integers u_iu\_i and v_iv\_i (1≤u_i,v_i≤n1 \leq u\_i, v\_i \leq n, u_i≠v_iu\_i \neq v\_i), meaning there is a bidirectional edge between u_iu\_i and v_iv\_i. The graph is connected, and there is at most one edge between any pair of vertices.

Output

If Grammy cannot reorder the numbers as required, output -1.

Otherwise, output an integer opop (0≤op≤10 0000 \leq op \leq 10\,000) on the first line, the number of operations. On each of the next opop lines, output an integer kk, the number of vertices on the chosen simple path, followed by kk integers x1,x2,…,xkx_1, x_2, \ldots, x_k (x1=Ax_1 = A, 1≤xi≤n1 \leq x_i \leq n), the vertices of the path. The xix_i must be distinct and form a path in GG.

You do not need to minimize opop. If there are multiple solutions, output any one of them.

Examples2

  1. Example 1

    Input
    5 6 1 2
    1 2 3 4 5
    1 3
    2 3
    1 4
    2 4
    1 5
    3 5
    
    Expected output
    7
    4 1 3 2 4
    3 1 3 2
    3 1 3 5
    4 1 3 2 4
    3 1 3 2
    2 1 3
    1 1
    
  2. Example 2

    Input
    4 3 1 2
    1 4 2 3
    1 4
    2 4
    3 4
    
    Expected output
    -1