Too Many Edges
Time limit15sMemory limit2048 MB
Given a DAG G' that contains up to 200 extra edges beyond the original DAG G, find the longest path in G by querying which edges belong to G within a limited query budget.
- Level
Hard9 of 10
- Topics
- Graph, Dynamic programming, Topological sort
- Solved
- No attempts yet
Statement
This is an interactive problem. You have to use the flush operation right after printing each line. For example, you can use fflush(stdout) in C or C++, System.out.flush() in Java, flush(output) in Pascal, and sys.stdout.flush() in Python.
You are an ordinary employee of a data analysis department. Your colleagues made trouble again. They downloaded a large graph from a remote server and analyzed it. Then their program added new edges to the graph without making a backup. Now they want the longest path in the original graph. Downloading the graph again takes too long, so you must recover the answer.
The original graph on the remote server is a directed acyclic graph . All edges have unit length. Your input is a modified graph , where and is a directed acyclic graph.
Your program must output , the length of the longest path in . The length of a path is the number of edges in it.
To test whether an edge belongs to the original graph, your program can query the remote server. To query whether edge belongs to , print ? u v. Flush the output after each query. The server responds with 1 if the edge is in and 0 otherwise.
When you know the answer, print ! ans, where . Flush the output and terminate the program normally.
Your program may make at most queries (not counting the answer line), although and are unknown to you.
Input
The first line contains two integers and (; ): the number of vertices and edges of .
Each of the next lines contains two integers and () describing a directed edge in . No edge appears more than once. It is guaranteed that .
The following lines contain the responses to your queries, each 0 or 1. The -th of these lines responds to your -th query. After queries, the server stops responding. The server sends a response only after your program prints a query and flushes the output.
Output
Print each query as ? u v on its own line, with . Flush the output after every line. When you find the answer, print ! ans on its own line and terminate the program.