Differentiating Games
Contest: VK Cup 2019-2020 - Final Round (Engine)
Difficulty rating: 3400
Tags: games, interactive

This is an interactive problem

Ginny is taking an exam on game theory. The professor is tired of hearing the same answers over and over again, so he offered Ginny to play a game instead of a standard exam.

As known from the course, a combinatorial game on a graph with multiple starting positions is a game with a directed graph and multiple starting vertices holding a token each. Two players take turns moving one of the tokens along the graph edges on each turn. The player who can't make a move loses the game. If both players can play an infinitely long game without losing, a draw is called.

For the exam, the professor drew an acyclic directed graph and chose one of its vertices. Ginny needs to guess the vertex the professor chose. To do so, Ginny can choose a multiset of vertices $$$S$$$ several times and ask the professor: "If I put one token in each vertex of the given graph for each occurrence of the vertex in the multiset $$$S$$$, and then one more in the selected vertex, what would be the result of the combinatorial game?".

Having given the task, the professor left the room to give Ginny some time to prepare for the game. Ginny thinks that she's being tricked because the problem is impossible to solve. Therefore, while the professor is away, she wants to add or remove several edges from the graph. Even though the original graph was acyclic, edges could be added to the graph to make cycles appear.

Interaction format:
In this task, interaction consists of several phases.

In the first phase, the interactor gives as an input to your program three integers $$$N$$$ ($$$1 \le N \le 1000$$$), $$$M$$$ ($$$0 \le M \le 100\,000$$$), $$$T$$$ ($$$1 \le T \le 2000$$$): the number of vertices and edges in the initial graph, and the number of times Ginny has to guess the chosen vertex. The next $$$M$$$ lines contain pairs of vertices $$$a_i$$$ $$$b_i$$$ ($$$1 \le a_i, b_i \le N$$$): beginning and end of corresponding graph edges. The graph is guaranteed to be acyclic and all of its edges to be distinct.

The solution should print an integer $$$K$$$ ($$$0 \le K \le 4242$$$): the number of edges to change in the graph. The next $$$K$$$ lines should contain either "+ $$$a_i$$$ $$$b_i$$$" or "- $$$a_i$$$ $$$b_i$$$": the beginning and the end of an edge that Ginny has to add or remove accordingly. You are allowed to add preexisting edges to the graph. Operations are performed in the order of appearance, so Ginny is allowed to remove an edge added by the solution. You can only remove an existing edge. The operations can create cycles in the graph.

The next $$$T$$$ phases are dedicated to guessing the chosen vertices. In each phase, the solution can make at most 20 queries and then print the answer. To query a multiset $$$S$$$, the solution should print "? $$$|S|~S_1~S_2~\dots~S_{|S|}$$$". The total size of all multisets in a single phase should not exceed 20. The interactor will reply with one of the following words:

- "Win", if the winner of a combinatorial game with tokens in multiset $$$S$$$ and the selected vertex is the first player.
- "Lose", if the winner of a combinatorial game with tokens in multiset $$$S$$$ and the selected vertex is the second player.
- "Draw", if a combinatorial game with tokens in multiset $$$S$$$ and selected vertex ends in a draw.
- "Slow", if the solution made a 21-st request, or the total size of all multisets in a single phase exceeded 20. In this case, the solution should terminate and receive Wrong Answer verdict.

As soon as the selected vertex is guessed, that solution should print "! v". If the chosen vertex is guessed correctly, the interactor will print Correct and the solution should either move on to the next phase of guessing or finish its execution if it's the last phase. Otherwise, the interactor will print Wrong, which means that the solution should terminate and will receive the Wrong Answer verdict.

The interactor can change the chosen vertex based on graph changes and solution actions, but at every given moment of time, at least one vertex that corresponds to all given interactor answers will exist.

Hack format

Hacks have the following extra limitations:

- $$$T = 1$$$
- you need to specify a single vertex, chosen by the interactor.

Hack test format. The first line of input contains three integers $$$N~M~1$$$. The next $$$M$$$ lines on input contain edge description in the same format as in the input. The next line contains a single integer $$$v$$$: the number of the chosen vertex. The hack will be successful even if the solution guesses the vertex right, but the vertex will not be the single one to match all performed queries.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
In the sample test, the empty lines represent waiting for the input by the other side of the interaction. The real interactor will not print empty lines, and the solution should not print them either.

The image above illustrates the sample test. Added edges are coloured in red, and the removed edges are drawn with a dotted line. Three guessing phases denote different ways of getting the answer.

- If the solution will query just the chosen vertex, the interactor will return the result of the game in that vertex. The first player loses only if the chosen vertex has the number $$$1$$$.
- If we add a single vertex $$$2$$$ to the chosen vertex, then if the chosen vertex is either $$$1$$$ or $$$2$$$, the game should end in a draw. If vertex number $$$3$$$ is chosen, then the first player wins.
- If we place three tokens in vertex $$$1$$$ and two tokens in vertex $$$3$$$, then the game will end in a draw only if vertex $$$2$$$ is chosen. If the professor chose vertex $$$3$$$, the first player will win, if the professor chose vertex $$$1$$$, then the second player will win.

In the first test, the interactor will behave as if the chosen vertices are the same as those in the example above. However, if you will try to guess the answer before it limits the options to one single vertex, the solution will get "Wrong Answer", even if you print the same answers. That's because the interactor is allowed to change the chosen vertex if it's consistent with the previous query answers.

Example input:
3 2 3
1 2
2 3

Lose

Correct

Win

Correct

Draw

Correct

Example output:
6
+ 2 2
- 1 2
+ 2 3
- 2 2
+ 3 1
+ 2 2
? 0

! 1

? 1 2

! 3

? 5 1 3 1 3 1

! 2

Editorial:
Let's first solve the problem for an empty graph. To do this we need to build a construction with not too many edges, where all vertices are pairwise not equivalent.
For acyclic graph, there is not such construction. All Grundy functions of vertices should be different, so they must be integers from 0 to $$$n-1$$$. The only such graph is full, and it has quadratic number of edges. Moreover, if there is a part from which no cycles are reachable, it must be full acyclic graph, so it can't be too big.
So, how can other graph look? No vertices can be equivalent to any of vertices in acyclic part. The easiest way to achieve it is to add a loop to every vertex. Let's note, that after doing so all this vertices can't be losing in sum with any other game, because we can just go through loop and don't change anything. So, this games differ from each other be set of games in sum with which they would be winning, not draw. One can see, that such a game would be winning with any acyclic game, it have a move to, because we can go to it, and this will be move to acyclic game with Grundy function 0. The game would be draw with any other acyclic game (and with not acyclic too, but this is not important for solution). This is true, because if we don't move in game with loop, resulting position is not winning. Therefore, to win we must go out from loop vertex. But than we come into acyclic game with non-zero Grundy function, and it's winning, so we can't win. And we can go through loop, achieving a draw.
So, we need to build a graph, with acyclic part of full graph on $$$k$$$ vertices, and all other vertices should have a loop and some edges to this $$$k$$$ vertices, and all vertices should have different set of edges. If done, all vertices would be pairwise distinguishable by $$$k$$$ queries for each acyclic part vertex. If any of them is lost, this vertex is chosen. If all of them is winning or draw, than the vertex with loop, which have an edge if and only if sum with this vertex is winning, is chosen. In opposite to acyclic case, we can distinguish exponentially many vertices.
Returning to the initial problem, one can get $$$k = 20$$$, and get all subsets of size 0, 1 and 2, and required part of subsets of size $$$3$$$. In such a graph there would be $$$190 + 980 + 1 \cdot 0 + 20 \cdot 1 + 190 \cdot 2 + 769 \cdot 3 = 3877$$$ edges. The only remaining part is to understand what to do with existing edges.
One can not, then in built construction any edges between vertices with loops can be added, because they change nothing. If position is winning, then after first move they become unreachable, and doesn't change anything. If it was draw, they can't change anything too, because they are led to vertex with loop, which can't be losing.
Let's use 20 vertices, which have no edges outside of this set as vertices of acyclic part. Initial graph was acyclic, so we can find such vertices. Let's add loop to all other vertices. Now, we need to modify graph in a way that all sets of edges to acyclic part would be different. Let's go through vertices in any order, and change minimal possible number of edges, so, that this vertex set would not be equal to any of previous. For first vertex we won't do anything. For next 20 vertices, we would need to change at most 1 edge. For next 190 vertices, we would need to change at most 2 edges, and at most 3 for others. This led us to the same 3877 edges to change.
In general case we can do either $$$O(nlogn)$$$ edges changes and $$$O(logn)$$$ queries or $$$O((d+1)*n)$$$ edges changes and $$$O(\sqrt[d]{d!n})$$$ queries. For this problem we need d = 3.