Guessing Game
Time limit2sMemory limit512 MB
Given difference constraints of the form a_i + b_j <= c or >= c, decide whether integer lists a and b satisfying all of them exist.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Union-find, Implementation
- Solved
- No attempts yet
Problem
Jaehyun has two integer lists and . Jeffrey wants to know these numbers, but Jaehyun won't reveal them directly. So Jeffrey asks a series of questions of the form "How big is ?" Even then Jaehyun refuses to give the exact value; he answers only "It's at least " (meaning ) or "It's at most " (meaning ). After collecting every answer, Jeffrey still cannot reconstruct any consistent numbers no matter how hard he tries, and starts to suspect that Jaehyun lied on some of the answers. Given Jaehyun's answers, determine whether integer lists consistent with all of them exist, or whether it can be proven that Jaehyun definitely lied.
Input
The input contains multiple test cases. Each test case begins with a line of three positive integers , , and — the lengths of Jaehyun's two lists and the number of questions Jeffrey asked — satisfying and . Each of the next lines has the form i j <= c or i j >= c: the former means and the latter means , where . The input ends with a line 0 0 0, which is not processed.
Output
For each test case, print a single line: Possible if there exist integers and consistent with all of Jaehyun's answers, or Impossible if the answers can be proven contradictory (that is, Jaehyun definitely lied).