CF2196C2.Interactive Graph (Hard Version)

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is the hard version of the problem. The difference between the versions is that in this version, you can ask no more than n+mn + m questions, and n≤30n \leq 30. You can hack only if you solved all versions of this problem.

This is an interactive problem.

The jury has thought of a directed acyclic graph without loops and multiple edges, which has nn vertices and mm edges.

Your task is to determine which edges are in this graph. To do this, you can ask questions of the form: what does the kk-th path look like in the lexicographically∗^{\text{∗}} sorted list of all paths in the graph.

A path in the graph is a sequence of vertices u1,u2,…,ulu_{1}, u_{2}, \dots, u_{l}, such that for any i<li \lt l, there exists an edge (ui,ui+1u_{i}, u_{i + 1}) in the graph.

Your task is to accomplish this by asking no more than n+mn + m questions.

∗^{\text{∗}}A sequence aa is lexicographically smaller than a sequence bb if and only if one of the following holds:

  • aa is a prefix of bb, but a≠ba \ne b; or
  • in the first position where aa and bb differ, the sequence aa has a smaller element than the corresponding element in bb.

这是该问题的困难版本。两个版本的区别在于,在本版本中,你最多只能提出 n+mn + m 个问题,且满足 n≤30n \leq 30。仅当你已解决该问题的所有版本时,才可进行 Hack。

这是一个交互式问题。

出题人构思了一个不含自环和重边的有向无环图(DAG),该图具有 nn 个顶点和 mm 条边。

你的任务是确定该图中具体包含哪些边。为此,你可以提出如下形式的问题:图中所有路径按字典序∗^{\text{∗}}排序后,第 kk 条路径是什么?

图中的一条路径是指一个顶点序列 u1,u2,…,ulu_{1}, u_{2}, \dots, u_{l},使得对任意 i<li \lt l,图中均存在一条从 uiu_i 指向 ui+1u_{i+1} 的边。

你需要在至多 n+mn + m 次提问内完成该任务。

∗^{\text{∗}} 序列 aa 字典序小于序列 bb,当且仅当满足以下条件之一:

  • aa 是 bb 的真前缀(即 aa 是 bb 的前缀,但 a≠ba \ne b);或
  • 在 aa 与 bb 首次出现不同元素的位置上,aa 中对应位置的元素小于 bb 中对应位置的元素。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤101 \le t \le 10). The description of the test cases follows.

Each test case consists of a single line with an integer nn (1≤n≤301 \le n \le 30) — the number of vertices in the graph.

The jury guarantees that the given graph does not contain cycles or multiple edges.

Note that mm is unknown to you!

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤101 \le t \le 10)。随后是各测试用例的描述。

每个测试用例由一行组成,其中包含一个整数 nn(1≤n≤301 \le n \le 30)——表示图中顶点的数量。

评测组保证所给图不含环或重边。

注意:mm 对你而言是未知的!

输入输出样例

  • 输入#1

    3
    5
    
    1 1
    
    2 1 2
    
    3 1 2 4
    
    3 1 2 5
    
    2 1 3
    
    3 1 3 4
    
    3 1 3 5
    
    1 2
    
    1 3
    
    1 4
    
    1 5
    
    1
    
    0
    
    2
    
    1 1
    
    1 2
    
    2 2 1

    输出#1

    ? 1
    
    ? 2
    
    ? 3
    
    ? 4
    
    ? 5
    
    ? 6
    
    ? 7
    
    ? 8
    
    ? 11
    
    ? 14
    
    ? 15
    
    ! 6
    1 3
    1 2
    2 4
    3 4
    2 5
    3 5
    
    ? 2
    
    ! 0
    
    ? 1
    
    ? 2
    
    ? 3
    
    ! 1
    2 1

说明/提示

The graph for the first test case.

In this graph, there are 1515 paths, which are arranged in lexicographic order as follows:

  • 11
  • 1→21 \to 2
  • 1→2→41 \to 2 \to 4
  • 1→2→51 \to 2 \to 5
  • 1→31 \to 3
  • 1→3→41 \to 3 \to 4
  • 1→3→51 \to 3 \to 5
  • 22
  • 2→42 \to 4
  • 2→52 \to 5
  • 33
  • 3→43 \to 4
  • 3→53 \to 5
  • 44
  • 55

第一个测试用例的图。

在该图中,共有 1515 条路径,按字典序排列如下:

  • 11
  • 1→21 \to 2
  • 1→2→41 \to 2 \to 4
  • 1→2→51 \to 2 \to 5
  • 1→31 \to 3
  • 1→3→41 \to 3 \to 4
  • 1→3→51 \to 3 \to 5
  • 22
  • 2→42 \to 4
  • 2→52 \to 5
  • 33
  • 3→43 \to 4
  • 3→53 \to 5
  • 44
  • 55

输入解题思路,AI测评打分。不知道怎么写?

首页