Under construction

Still being built. Things may change

Locks and Keys

Time limit2sMemory limit128 MB

Summary
Determine reachability on a tree with colored locks and single-use keys held one at a time, requiring a search over reachable key/room states.
Level

Medium7 of 10

Topics
DFS, Graph, Simulation
Solved
No attempts yet

Problem

The wizard Jaehwan is trapped in a maze of VV rooms and V−1V-1 doors. Each door connects two rooms in both directions, and it is always possible to travel from any room to any other room (so the maze is a tree). There are CC locks in CC distinct colors and CC keys in the same CC colors. Each door may be locked by at most one lock, and each room may hold at most one key; every color has exactly one lock and exactly one key.

Jaehwan is a wizard and could normally open locks by magic, but he left his spellbook behind, so right now he cannot use magic at all.

He is currently in room XX and wants to reach room YY, where the spellbook is. In one step he can move from his current room to an adjacent room through a door. If a door is locked, he can open it and pass only while holding a key of the same color as the lock. Once a door has been opened it stays open, so he may pass through it freely afterwards.

Jaehwan can hold only one key at a time. He may not set a key down in another room to reuse it later, and opening a locked door consumes the key (it disappears).

Given the structure of the maze and the positions of the CC keys and CC locks, determine whether Jaehwan can travel from room XX to room YY.

Input

The input consists of several test cases.

The first line of each test case contains four integers VV, CC, XX, and YY: the number of rooms VV (1≤V≤15001 \le V \le 1500), the number of locks (and keys) CC (0≤C<V0 \le C < V), and the numbers of the start room XX and the destination room YY. Rooms are numbered from 00 to V−1V-1.

The next CC numbers give, in increasing order of color (color 00 through color C−1C-1), the room that holds the key of that color.

The following V−1V-1 lines each describe a door as AA BB LL. If 0≤L<C0 \le L < C, the door between rooms AA and BB is locked by the lock of color LL; if L=−1L = -1, the door is not locked.

The last line of the input is 0 0 0 0.

Output

For each test case, print a single line: Possible if Jaehwan can travel from room XX to room YY, and Impossible otherwise.

Examples1

  1. Example 1

    Input
    1 0 0 0
    
    3 1 0 2
    1
    0 1 -1
    0 2 0
    
    3 2 0 2
    1 2
    0 1 1
    0 2 0
    
    5 3 0 4
    2 0 3
    0 1 0
    0 2 -1
    1 3 1
    2 4 2
    
    0 0 0 0
    
    Expected output
    Possible
    Possible
    Impossible
    Possible