Under construction

Still being built. Things may change

One Stroke Drawing

Time limit2sMemory limit256 MB

Summary
Decide whether given line segments form a shape drawable in one stroke without retracing any segment.
Level

Medium4 of 10

Topics
Graph, Union-find
Solved
No attempts yet

Problem

A programmer saw an interesting shape one day. He wondered whether he could draw it on paper with a pencil in one continuous line, tried for a few minutes, and gave up. He decided to write a program that answers the question instead.

The shape is given as a collection of straight line segments listed in no particular order. Two different segments touch only at their endpoints. Determine whether the whole shape can be drawn without lifting the pencil and without going back over a segment that is already drawn.

Input

The input contains several shapes. Each shape starts with a line holding the number of segments NN, where 0<N<10000 < N < 1000. The next NN lines each hold four integers aa, bb, cc, dd, describing the segment from (a,b)(a, b) to (c,d)(c, d). At most one of a=ca = c and b=db = d is true, and −1000<a,b,c,d<1000-1000 < a, b, c, d < 1000.

The input ends with a shape that has N=0N = 0. Do not process that shape.

Output

For each shape print one line: Possible if the shape can be drawn in one stroke, Impossible otherwise. Print the words without quotes.

Examples2

  1. Example 1

    Input
    1
    0 0 1 1
    2
    0 0 1 0
    1 1 1 0
    2
    0 0 1 0
    2 2 2 3
    9
    0 0 1 0
    0 0 0 1
    1 0 1 1
    0 1 1 1
    0 0 1 1
    0 0 -5 -5
    -5 -5 5 -5
    5 -5 5 5
    5 5 1 1
    0
    
    Expected output
    Possible
    Possible
    Impossible
    Possible
    
  2. Example 2

    Input
    3
    0 0 1 0
    1 0 0 1
    0 1 0 0
    0
    
    Expected output
    Possible