Gods
Time limit2sMemory limit1024 MB
Split n gods into two lines so that no quarreling pair shares a line and no two attack segments cross, or print -1 if impossible.
- Level
Hard8 of 10
- Topics
- Graph, DFS, Implementation
- Solved
- No attempts yet
Problem
At a feast in Asgard, the gods talked to each other. Under the influence of mead, they quarreled. A fight was about to break out, and the gods realized they wanted to fight only the ones they had argued with, and did not want to quarrel with any other god.
Odin, the cleverest god in Asgard, saw that it would be very convenient if the gods stood in two lines facing each other, so that no two gods who had quarreled were in the same line. Thor developed his father's idea and introduced the concept of an 'attack line'. An attack line is the segment connecting the positions of two gods who quarreled. Thor further proposed that it would be even more convenient if the attack lines of different gods did not intersect, because then the gods themselves would certainly not cross paths during the battle. All the gods approved. Only the positions remain to be taken, and then the fight can begin.
As an honored citizen of Midgard, you have the chance to help the gods. Write a program that splits the gods into two lines so that Thor's refinement holds.
Input
The first line contains (), the number of gods in Asgard. Each of the next lines contains integers. The first integer is (), the number of gods who quarreled with god , followed by the numbers of those gods. If god quarreled with god , then god quarreled with god . The sum of all does not exceed .
Output
If a solution exists, print two numbers and on the first line: the number of gods in the first line and the number of gods in the second line. On the next two lines, print the numbers of the gods in the first and second lines, from left to right. Otherwise, print .