Problem F
Seven Kingdoms
Jon Dayne is the ruler of a huge country called Seven Kingdoms. He has two sisters, Arya and Sansa, and wants to give some cities of Seven Kingdoms to them. He will rule the remaining cities or if no city remains, goes to the Wall, a colossal fortification along the northern border of the Seven Kingdoms, to be the Lord commander. Arya is the Lady of Winterfell and Sansa is the Lady of King’s Landing. The cities in Seven Kingdoms (including Winterfell and King’s Landing) are connected to each other with a network of roads (although some cities may be disconnected from the other cities, because they are either located on an island or they are currently at war with these other cities). There is no direct road between Winterfell and King’s Landing and they do not share a common neighbour city.
Jon wants to assign a collection of cities to each one of his sisters such that each city in a collection is connected with a direct road to all other cities in that collection and the remaining cities, not in these two collections, are also connected with a direct road to each other. The collection assigned to Arya must include Winterfell and the collection assigned to Sansa must include King’s Landing. Jon needs your help to determine whether this is possible and if this is possible, you should tell him the cities in each collection.
Input
The input consists of a single test case. The first line contains two integers $n$ and $m$, where $n$ ($2 \le n \le 2\, 000$) is the number of cities, and $m$ ($0 \leq m \leq m(m-1)/2$) is the number of roads. Each of the next $m$ lines contains two integers $x_ i$ and $y_ i$ ($1 \le x_ i, y_ i \le n$) describing one road, where $x_ i$ and $y_ i$ are the distinct cities the road connects. Two cities are connected by at most one road. Winterfell is city $1$ and King’s Landing is city $2$ in the road network.
Output
If it is not possible to partition the cities in the way explained, display the word impossible. Otherwise, display two lines: the first containing the cities in the collection assigned to Arya and the second containing the collection of cities assigned to Sansa. If there are many such collections, any one of them is acceptable.
Sample Input 1 | Sample Output 1 |
---|---|
9 11 1 4 5 4 1 5 6 2 6 7 7 2 3 8 3 9 8 9 6 8 5 9 |
1 4 5 2 6 7 |
Sample Input 2 | Sample Output 2 |
---|---|
9 10 1 4 5 4 1 5 6 2 6 7 7 2 3 8 3 9 6 8 5 9 |
impossible |