favorite We need a little bit of your help to keep things running, click on this banner to learn more

Shortest paths - interesting graph problems


Once upon a time, there arose a huge discussion among the dwarves in Dwarfland. The government wanted to introduce an identity card for all inhabitants.

Most dwarves accept to be small, but they do not like to be measured. Therefore, the government allowed them to substitute the field "height" in their personal identity card with a field "relative dwarf size". For producing the ID cards, the dwarves were being interviewed about their relative sizes. For some reason, the government suspects that at least one of the interviewed dwarves must have lied.

Can you help find out if the provided information proves the existence of at least one lying dwarf?


Consists of:

  • one line with an integer n (1n105), where n is the number of statements;
  • n lines describing the relations between the dwarves. Each relation is described by one line with "s1 < s2" or "s1 > s2", telling whether dwarf s1 is smaller or taller than dwarf s2. s1 and s2 are two different dwarf names.

A dwarf name consists of at most 20 letters from "A" to "Z" and "a" to "z". A dwarf name does not contain spaces. The number of dwarves does not exceed 104.


Print "impossible" if the statements are not consistent, otherwise print "possible".

Time limit 1 second
Memory limit 128 MiB
Input example #1
Dori > Balin
Balin > Kili
Dori < Kili
Output example #1
Input example #2
Dori > Balin
Balin > Kili
Dori > Kili
Output example #2
Source 2016 German Collegiate Programming Contest (GCPC), Problem A