Under construction

Still being built. Things may change

Wonderland Chase

Memory limit1024 MB

Summary
Given a graph with Alice and the Queen on junctions, find how many moves the Queen needs to catch Alice, or report SAFE after 10^9 moves.
Level

Medium7 of 10

Topics
Graph, BFS, Game theory
Solved
No attempts yet

Problem

Alice is trapped in Wonderland's labyrinth, being chased by the Queen of Hearts and her herald! The labyrinth is a set of J\mathbf{J} junctions numbered 11 through J\mathbf{J}, connected by C\mathbf{C} bidirectional corridors.

Alice and the Queen of Hearts take turns making moves. A move, by either of them, is either staying at the current junction or moving to another junction connected to it by a corridor.

The Queen's herald announces the Queen's next move in advance. Before anyone moves, he announces the Queen's first move. Then Alice moves first. After that, each time the Queen moves, she must respect the previous announcement, and then she decides her next move so the herald can announce it. Alice hears the announcements, so she always knows the Queen's next move before making her own.

If Alice and the Queen are at the same junction after either of them moves, Alice is caught. Otherwise, the pursuit continues. After 10910^9 total moves (half for Alice and half for the Queen), if Alice and the Queen are still in different junctions, the Queen gives up and Alice is safe.

Alice chooses her moves to escape. If she cannot escape, she chooses her moves to maximize the total number of moves until she is caught. The Queen chooses her moves to catch Alice in as few total moves as possible.

Given the layout of the labyrinth and the starting junctions of the Queen and Alice, determine whether Alice is caught by the Queen and, if so, after how many moves.

Input

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. Each test case starts with a line containing four integers J\mathbf{J}, C\mathbf{C}, A\mathbf{A}, and Q\mathbf{Q}: the number of junctions, the number of corridors, the junction where Alice starts, and the junction where the Queen starts. Then C\mathbf{C} lines follow. The ii-th of these lines contains two integers U_i\mathbf{U\_i} and V_i\mathbf{V\_i}, indicating that the ii-th corridor bidirectionally connects junctions U_i\mathbf{U\_i} and V_i\mathbf{V\_i}.

Output

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1). yy is SAFE if Alice can avoid being caught for 10910^9 total moves. Otherwise, yy is the total number of moves (including Alice's and the Queen's) that it takes for the Queen to catch Alice.

Constraints

  • 1≤T≤1001 \le \mathbf{T} \le 100.
  • 1≤A≤J1 \le \mathbf{A} \le \mathbf{J}.
  • 1≤Q≤J1 \le \mathbf{Q} \le \mathbf{J}.
  • A≠Q\mathbf{A} \ne \mathbf{Q}.
  • 1≤U_i<V_i≤J1 \le \mathbf{U\_i} \lt \mathbf{V\_i} \le \mathbf{J}, for all ii.
  • (U_i,V_i)≠(U_j,V_j)(\mathbf{U\_i}, \mathbf{V\_i}) \ne (\mathbf{U\_j}, \mathbf{V\_j}), for all i≠ji \ne j.

Hints

Sample Case #1 is the one pictured in the problem statement. Alice's optimal first move is to move to junction 44.

Sample Case #2 is the same as Sample Case #1 but the Queen starts at junction 22. The Queen can catch Alice by first announcing a move to junction 44. If Alice were to move to junction 44, she would be caught in 22 moves. Alice can evade capture for an extra 22 moves by staying put and waiting until the Queen then moves to junction 55, where she is located.

In Sample Case #3, the Queen cannot reach Alice no matter what she does.

In Sample Case #4, the Queen can begin by announcing that she will move to Alice's current junction. Alice has to move before then. If Alice moves to where the Queen already is, she gets caught immediately. If Alice remains in place, she gets caught when the Queen moves. The second option is better, since it requires 22 total moves (Alice's and the Queen's) instead of 11.

Examples1

  1. Example 1

    Input
    4
    5 5 5 1
    1 2
    1 3
    2 4
    3 4
    4 5
    5 5 5 2
    1 2
    1 3
    2 4
    3 4
    4 5
    3 1 2 3
    1 3
    2 1 1 2
    1 2
    
    Expected output
    Case #1: SAFE
    Case #2: 4
    Case #3: SAFE
    Case #4: 2